# NAG Library Routine Document

## 1Purpose

e02gaf calculates an ${l}_{1}$ solution to an over-determined system of linear equations.

## 2Specification

Fortran Interface
 Subroutine e02gaf ( m, a, lda, b, x, iter,
 Integer, Intent (In) :: m, lda, nplus2 Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: irank, iter, iwork(m) Real (Kind=nag_wp), Intent (In) :: toler Real (Kind=nag_wp), Intent (Inout) :: a(lda,nplus2), b(m) Real (Kind=nag_wp), Intent (Out) :: x(nplus2), resid
#include <nagmk26.h>
 void e02gaf_ (const Integer *m, double a[], const Integer *lda, double b[], const Integer *nplus2, const double *toler, double x[], double *resid, Integer *irank, Integer *iter, Integer iwork[], Integer *ifail)

## 3Description

Given a matrix $A$ with $m$ rows and $n$ columns $\left(m\ge n\right)$ and a vector $b$ with $m$ elements, the routine calculates an ${l}_{1}$ solution to the over-determined system of equations
 $Ax=b.$
That is to say, it calculates a vector $x$, with $n$ elements, which minimizes the ${l}_{1}$ norm (the sum of the absolute values) of the residuals
 $rx=∑i=1mri,$
where the residuals ${r}_{i}$ are given by
 $ri=bi-∑j=1naijxj, i=1,2,…,m.$
Here ${a}_{ij}$ is the element in row $i$ and column $j$ of $A$, ${b}_{i}$ is the $i$th element of $b$ and ${x}_{j}$ the $j$th element of $x$. The matrix $A$ need not be of full rank.
Typically in applications to data fitting, data consisting of $m$ points with coordinates $\left({t}_{i},{y}_{i}\right)$ are to be approximated in the ${l}_{1}$ norm by a linear combination of known functions ${\varphi }_{j}\left(t\right)$,
 $α1ϕ1t+α2ϕ2t+⋯+αnϕnt.$
This is equivalent to fitting an ${l}_{1}$ solution to the over-determined system of equations
 $∑j=1nϕjtiαj=yi, i=1,2,…,m.$
Thus if, for each value of $i$ and $j$, the element ${a}_{ij}$ of the matrix $A$ in the previous paragraph is set equal to the value of ${\varphi }_{j}\left({t}_{i}\right)$ and ${b}_{i}$ is set equal to ${y}_{i}$, the solution vector $x$ will contain the required values of the ${\alpha }_{j}$. Note that the independent variable $t$ above can, instead, be a vector of several independent variables (this includes the case where each ${\varphi }_{i}$ is a function of a different variable, or set of variables).
The algorithm is a modification of the simplex method of linear programming applied to the primal formulation of the ${l}_{1}$ problem (see Barrodale and Roberts (1973) and Barrodale and Roberts (1974)). The modification allows several neighbouring simplex vertices to be passed through in a single iteration, providing a substantial improvement in efficiency.
Barrodale I and Roberts F D K (1973) An improved algorithm for discrete ${l}_{1}$ linear approximation SIAM J. Numer. Anal. 10 839–848
Barrodale I and Roberts F D K (1974) Solution of an overdetermined system of equations in the ${l}_{1}$-norm Comm. ACM 17(6) 319–320

## 5Arguments

1:     $\mathbf{m}$ – IntegerInput
On entry: the number of equations, $m$ (the number of rows of the matrix $A$).
Constraint: ${\mathbf{m}}\ge n\ge 1$.
2:     $\mathbf{a}\left({\mathbf{lda}},{\mathbf{nplus2}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: ${\mathbf{a}}\left(\mathit{i},\mathit{j}\right)$ must contain ${a}_{\mathit{i}\mathit{j}}$, the element in the $\mathit{i}$th row and $\mathit{j}$th column of the matrix $A$, for $\mathit{i}=1,2,\dots ,m$ and $\mathit{j}=1,2,\dots ,n$. The remaining elements need not be set.
On exit: contains the last simplex tableau generated by the simplex method.
3:     $\mathbf{lda}$ – IntegerInput
On entry: the first dimension of the array a as declared in the (sub)program from which e02gaf is called.
Constraint: ${\mathbf{lda}}\ge {\mathbf{m}}+2$.
4:     $\mathbf{b}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: ${\mathbf{b}}\left(\mathit{i}\right)$ must contain ${b}_{\mathit{i}}$, the $\mathit{i}$th element of the vector $b$, for $\mathit{i}=1,2,\dots ,m$.
On exit: the $\mathit{i}$th residual ${r}_{\mathit{i}}$ corresponding to the solution vector $x$, for $\mathit{i}=1,2,\dots ,m$.
5:     $\mathbf{nplus2}$ – IntegerInput
On entry: $n+2$, where $n$ is the number of unknowns (the number of columns of the matrix $A$).
Constraint: $3\le {\mathbf{nplus2}}\le {\mathbf{m}}+2$.
6:     $\mathbf{toler}$ – Real (Kind=nag_wp)Input
On entry: a non-negative value. In general toler specifies a threshold below which numbers are regarded as zero. The recommended threshold value is ${\epsilon }^{2/3}$ where $\epsilon$ is the machine precision. The recommended value can be computed within the routine by setting toler to zero. If premature termination occurs a larger value for toler may result in a valid solution.
Suggested value: $0.0$.
7:     $\mathbf{x}\left({\mathbf{nplus2}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: ${\mathbf{x}}\left(\mathit{j}\right)$ contains the $\mathit{j}$th element of the solution vector $x$, for $\mathit{j}=1,2,\dots ,n$. The elements ${\mathbf{x}}\left(n+1\right)$ and ${\mathbf{x}}\left(n+2\right)$ are unused.
8:     $\mathbf{resid}$ – Real (Kind=nag_wp)Output
On exit: the sum of the absolute values of the residuals for the solution vector $x$.
9:     $\mathbf{irank}$ – IntegerOutput
On exit: the computed rank of the matrix $A$.
10:   $\mathbf{iter}$ – IntegerOutput
On exit: the number of iterations taken by the simplex method.
11:   $\mathbf{iwork}\left({\mathbf{m}}\right)$ – Integer arrayWorkspace
12:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value  is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value  is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
An optimal solution has been obtained, but may not be unique.
${\mathbf{ifail}}=2$
Premature termination due to rounding errors. Try using larger value of toler: ${\mathbf{toler}}=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{lda}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lda}}\ge {\mathbf{m}}+2$.
On entry, ${\mathbf{nplus2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nplus2}}\ge 3$.
On entry, ${\mathbf{nplus2}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: $3\le {\mathbf{nplus2}}\le {\mathbf{m}}+2$.
${\mathbf{ifail}}=4$
More than $1000*n$ iterations were performed. e02gaf has terminated without calculating a solution. The output data from the routine is as computed on the last good iteration. Consider increasing the value of toler. Alternatively, $A$ may be ill conditioned—try scaling its columns.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

Experience suggests that the computational accuracy of the solution $x$ is comparable with the accuracy that could be obtained by applying Gaussian elimination with partial pivoting to the $n$ equations satisfied by this algorithm (i.e., those equations with zero residuals). The accuracy therefore varies with the conditioning of the problem, but has been found generally very satisfactory in practice.

## 8Parallelism and Performance

e02gaf is not threaded in any implementation.

The effects of $m$ and $n$ on the time and on the number of iterations in the Simplex Method vary from problem to problem, but typically the number of iterations is a small multiple of $n$ and the total time taken is approximately proportional to $m{n}^{2}$.
It is recommended that, before the routine is entered, the columns of the matrix $A$ are scaled so that the largest element in each column is of the order of unity. This should improve the conditioning of the matrix, and also enable the argument toler to perform its correct function. The solution $x$ obtained will then, of course, relate to the scaled form of the matrix. Thus if the scaling is such that, for each $j=1,2,\dots ,n$, the elements of the $j$th column are multiplied by the constant ${k}_{j}$, the element ${x}_{j}$ of the solution vector $x$ must be multiplied by ${k}_{j}$ if it is desired to recover the solution corresponding to the original matrix $A$.

## 10Example

Suppose we wish to approximate a set of data by a curve of the form
 $y=Ket+Le-t+M$
where $K$, $L$ and $M$ are unknown. Given values ${y}_{i}$ at $5$ points ${t}_{i}$ we may form the over-determined set of equations for $K$, $L$ and $M$
 $exiK+e-xiL+M=yi, i=1,2,…,5.$
e02gaf is used to solve these in the ${l}_{1}$ sense.

### 10.1Program Text

Program Text (e02gafe.f90)

### 10.2Program Data

Program Data (e02gafe.d)

### 10.3Program Results

Program Results (e02gafe.r)