﻿ e04pc Method
e04pc solves a linear least squares problem subject to fixed lower and upper bounds on the variables.

# Syntax

C#
```public static void e04pc(
int itype,
int m,
int n,
double[,] a,
double[] b,
double[] bl,
double[] bu,
double tol,
double[] x,
out double rnorm,
out int nfree,
double[] w,
int[] indx,
out int ifail
)```
Visual Basic
```Public Shared Sub e04pc ( _
itype As Integer, _
m As Integer, _
n As Integer, _
a As Double(,), _
b As Double(), _
bl As Double(), _
bu As Double(), _
tol As Double, _
x As Double(), _
<OutAttribute> ByRef rnorm As Double, _
<OutAttribute> ByRef nfree As Integer, _
w As Double(), _
indx As Integer(), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void e04pc(
int itype,
int m,
int n,
array<double,2>^ a,
array<double>^ b,
array<double>^ bl,
array<double>^ bu,
double tol,
array<double>^ x,
[OutAttribute] double% rnorm,
[OutAttribute] int% nfree,
array<double>^ w,
array<int>^ indx,
[OutAttribute] int% ifail
)```
F#
```static member e04pc :
itype : int *
m : int *
n : int *
a : float[,] *
b : float[] *
bl : float[] *
bu : float[] *
tol : float *
x : float[] *
rnorm : float byref *
nfree : int byref *
w : float[] *
indx : int[] *
ifail : int byref -> unit
```

#### Parameters

itype
Type: System..::..Int32
On entry: provides the choice of returning a regularized solution if the matrix is not of full rank.
${\mathbf{itype}}=0$
Specifies that a regularized solution is to be computed.
${\mathbf{itype}}=1$
Specifies that no regularization is to take place.
Suggested value: unless there is a definite need for a minimal length solution we recommend that ${\mathbf{itype}}=1$ is used.
Constraint: ${\mathbf{itype}}=0$ or $1$.
m
Type: System..::..Int32
On entry: $m$, the number of linear equations.
Constraint: ${\mathbf{m}}\ge 0$.
n
Type: System..::..Int32
On entry: $n$, the number of variables.
Constraint: ${\mathbf{n}}\ge 0$.
a
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, dim2]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{m}}$
Note: the second dimension of the array a must be at least ${\mathbf{n}}$.
On entry: the $m$ by $n$ matrix $A$.
On exit: if ${\mathbf{itype}}=1$, a contains the product matrix $QA$, where $Q$ is an $m$ by $m$ orthogonal matrix generated by e04pc; otherwise a is unchanged.
b
Type: array<System..::..Double>[]()[][]
An array of size [m]
On entry: the right-hand side vector $b$.
On exit: if ${\mathbf{itype}}=1$, the product of $Q$ times the original vector $b$, where $Q$ is as described in parameter a; otherwise b is unchanged.
bl
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: ${\mathbf{bl}}\left[\mathit{i}-1\right]$ and ${\mathbf{bu}}\left[\mathit{i}-1\right]$ must specify the lower and upper bounds, ${l}_{i}$ and ${u}_{i}$ respectively, to be imposed on the solution vector ${x}_{i}$.
Constraint: ${\mathbf{bl}}\left[\mathit{i}-1\right]\le {\mathbf{bu}}\left[\mathit{i}-1\right]$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
bu
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: ${\mathbf{bl}}\left[\mathit{i}-1\right]$ and ${\mathbf{bu}}\left[\mathit{i}-1\right]$ must specify the lower and upper bounds, ${l}_{i}$ and ${u}_{i}$ respectively, to be imposed on the solution vector ${x}_{i}$.
Constraint: ${\mathbf{bl}}\left[\mathit{i}-1\right]\le {\mathbf{bu}}\left[\mathit{i}-1\right]$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
tol
Type: System..::..Double
On entry: tol specifies a parameter used to determine the relative linear dependence of a column vector for a variable moved from its initial value. It determines the computational rank of the matrix. Increasing its value from  will increase the likelihood of additional elements of $x$ being set to zero. It may be worth experimenting with increasing values of tol to determine whether the nature of the solution, $x$, changes significantly. In practice a value of  is recommended (see x02aj).
If on entry , then  is used.
Suggested value: ${\mathbf{tol}}=0.0$
x
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: the solution vector $x$.
rnorm
Type: System..::..Double%
On exit: the Euclidean norm of the residual vector $b-Ax$.
nfree
Type: System..::..Int32%
On exit: indicates the number of components of the solution vector that are not at one of the constraints.
w
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: contains the dual solution vector. The magnitude of ${\mathbf{w}}\left[i-1\right]$ gives a measure of the improvement in the objective value if the corresponding bound were to be relaxed so that ${x}_{i}$ could take different values.
A value of ${\mathbf{w}}\left[i-1\right]$ equal to the special value $-999.0$ is indicative of the matrix $A$ not having full rank. It is only likely to occur when ${\mathbf{itype}}=1$. However a matrix may have less than full rank without ${\mathbf{w}}\left[i-1\right]$ being set to $-999.0$. If ${\mathbf{itype}}=1$ then the values contained in w (other than those set to $-999.0$) may be unreliable; the corresponding values in indx may likewise be unreliable. If you have any doubts set ${\mathbf{itype}}=0$. Otherwise the values of ${\mathbf{w}}\left[i-1\right]$ have the following meaning:
${\mathbf{w}}\left[i-1\right]=0$
if ${x}_{i}$ is unconstrained.
${\mathbf{w}}\left[i-1\right]<0$
if ${x}_{i}$ is constrained by its lower bound.
${\mathbf{w}}\left[i-1\right]>0$
if ${x}_{i}$ is constrained by its upper bound.
${\mathbf{w}}\left[i-1\right]$
may be any value if ${l}_{i}={u}_{i}$.
indx
Type: array<System..::..Int32>[]()[][]
An array of size [n]
On exit: the contents of this array describe the components of the solution vector as follows:
${\mathbf{indx}}\left[\mathit{i}-1\right]$, for $\mathit{i}=1,2,\dots ,{\mathbf{nfree}}$
These elements of the solution have not hit a constraint; i.e., ${\mathbf{w}}\left[i-1\right]=0$.
${\mathbf{indx}}\left[\mathit{i}-1\right]$, for $\mathit{i}={\mathbf{nfree}}+1,\dots ,k$
These elements of the solution have been constrained by either the lower or upper bound.
${\mathbf{indx}}\left[\mathit{i}-1\right]$, for $\mathit{i}=k+1,\dots ,{\mathbf{n}}$
These elements of the solution are fixed by the bounds; i.e., ${\mathbf{bl}}\left[i-1\right]={\mathbf{bu}}\left[i-1\right]$.
Here $k$ is determined from nfree and the number of fixed components. (Often the latter will be $0$, so $k$ will be ${\mathbf{n}}-{\mathbf{nfree}}$.)
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

Given an $m$ by $n$ matrix $A$, an $n$-vector $l$ of lower bounds, an $n$-vector $u$ of upper bounds, and an $m$-vector $b$, e04pc computes an $n$-vector $x$ that solves the least squares problem $Ax=b$ subject to ${x}_{i}$ satisfying ${l}_{i}\le {x}_{i}\le {u}_{i}$.
A facility is provided to return a ‘regularized’ solution, which will closely approximate a minimal length solution whenever $A$ is not of full rank. A minimal length solution is the solution to the problem which has the smallest Euclidean norm.
The algorithm works by applying orthogonal transformations to the matrix and to the right hand side to obtain within the matrix an upper triangular matrix $R$. In general the elements of $x$ corresponding to the columns of $R$ will be the candidate non-zero solutions. If a diagonal element of $R$ is small compared to the other members of $R$ then this is undesirable. $R$ will be nearly singular and the equations for $x$ thus ill-conditioned. You may specify the tolerance used to determine the relative linear dependence of a column vector for a variable moved from its initial value.

# References

Lawson C L and Hanson R J (1974) Solving Least Squares Problems Prentice–Hall

# Error Indicators and Warnings

Note: e04pc may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDA) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}=1$
One of the following input errors has occurred:
• ${\mathbf{lda}}<{\mathbf{m}}$,
• ${\mathbf{m}}<0$,
• ${\mathbf{n}}<0$,
• ${l}_{i}>{u}_{i}$ for at least one value of $i$.
${\mathbf{ifail}}=2$
The routine failed to converge in $3{\mathbf{n}}$ iterations. This is not expected. Please contact NAG.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-4000$
Invalid dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

Orthogonal rotations are used.

# Parallelism and Performance

None.

If either m or n is zero on entry then e04pc sets ${\mathbf{ifail}}={0}$ and simply returns without setting any other output parameters.

# Example

The example minimizes ${‖Ax-b‖}_{2}$ where
 $A=0.050.050.25-0.250.250.250.05-0.050.350.351.75-1.751.751.750.35-0.350.30-0.300.300.300.40-0.400.400.40$
and
 $b=1.02.03.04.05.06.0T$
subject to $1\le x\le 5$.

Example program (C#): e04pce.cs

Example program data: e04pce.d

Example program results: e04pce.r