# NAG FL Interfacef04amf (real_​gen_​lsqsol)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

f04amf calculates the accurate least squares solution of a set of $m$ linear equations in $n$ unknowns, $m\ge n$ and rank $\text{}=n$, with multiple right-hand sides, $AX=B$, using a $QR$ factorization and iterative refinement.

## 2Specification

Fortran Interface
 Subroutine f04amf ( a, lda, x, ldx, b, ldb, m, n, ir, eps, qr, ldqr, e, y, z, r, ipiv,
 Integer, Intent (In) :: lda, ldx, ldb, m, n, ir, ldqr Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: ipiv(n) Real (Kind=nag_wp), Intent (In) :: a(lda,n), b(ldb,ir), eps Real (Kind=nag_wp), Intent (Inout) :: x(ldx,ir), qr(ldqr,n) Real (Kind=nag_wp), Intent (Out) :: alpha(n), e(n), y(n), z(n), r(m)
#include <nag.h>
 void f04amf_ (const double a[], const Integer *lda, double x[], const Integer *ldx, const double b[], const Integer *ldb, const Integer *m, const Integer *n, const Integer *ir, const double *eps, double qr[], const Integer *ldqr, double alpha[], double e[], double y[], double z[], double r[], Integer ipiv[], Integer *ifail)
The routine may be called by the names f04amf or nagf_linsys_real_gen_lsqsol.

## 3Description

To compute the least squares solution to a set of $m$ linear equations in $n$ unknowns $\left(m\ge n\right)$ $AX=B$, f04amf first computes a $QR$ factorization of $A$ with column pivoting, $AP=QR$, where $R$ is upper triangular, $Q$ is an $m×m$ orthogonal matrix, and $P$ is a permutation matrix. ${Q}^{\mathrm{T}}$ is applied to the $m×r$ right-hand side matrix $B$ to give $C={Q}^{\mathrm{T}}B$, and the $n×r$ solution matrix $X$ is calculated, to a first approximation, by back-substitution in $RX=C$. The residual matrix $S=B-AX$ is calculated using additional precision, and a correction $D$ to $X$ is computed as the least squares solution to $AD=S$. $X$ is replaced by $X+D$ and this iterative refinement of the solution is repeated until full machine accuracy has been obtained.
Wilkinson J H and Reinsch C (1971) Handbook for Automatic Computation II, Linear Algebra Springer–Verlag

## 5Arguments

1: $\mathbf{a}\left({\mathbf{lda}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: the $m×n$ matrix $A$.
2: $\mathbf{lda}$Integer Input
On entry: the first dimension of the array a as declared in the (sub)program from which f04amf is called.
Constraint: ${\mathbf{lda}}\ge {\mathbf{m}}$.
3: $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{ir}}\right)$Real (Kind=nag_wp) array Output
On exit: the $n×r$ solution matrix $X$.
4: $\mathbf{ldx}$Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which f04amf is called.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
5: $\mathbf{b}\left({\mathbf{ldb}},{\mathbf{ir}}\right)$Real (Kind=nag_wp) array Input
On entry: the $m×r$ right-hand side matrix $B$.
6: $\mathbf{ldb}$Integer Input
On entry: the first dimension of the array b as declared in the (sub)program from which f04amf is called.
Constraint: ${\mathbf{ldb}}\ge {\mathbf{m}}$.
7: $\mathbf{m}$Integer Input
On entry: $m$, the number of rows of the matrix $A$, i.e., the number of equations.
Constraint: ${\mathbf{m}}\ge 1$.
8: $\mathbf{n}$Integer Input
On entry: $n$, the number of columns of the matrix $A$, i.e., the number of unknowns.
Constraint: $0\le {\mathbf{n}}\le {\mathbf{m}}$.
9: $\mathbf{ir}$Integer Input
On entry: $r$, the number of right-hand sides.
10: $\mathbf{eps}$Real (Kind=nag_wp) Input
On entry: must be set to the value of the machine precision.
11: $\mathbf{qr}\left({\mathbf{ldqr}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: details of the $QR$ factorization.
12: $\mathbf{ldqr}$Integer Input
On entry: the first dimension of the array qr as declared in the (sub)program from which f04amf is called.
Constraint: ${\mathbf{ldqr}}\ge {\mathbf{m}}$.
13: $\mathbf{alpha}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: the diagonal elements of the upper triangular matrix $R$.
14: $\mathbf{e}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Workspace
15: $\mathbf{y}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Workspace
16: $\mathbf{z}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Workspace
17: $\mathbf{r}\left({\mathbf{m}}\right)$Real (Kind=nag_wp) array Workspace
18: $\mathbf{ipiv}\left({\mathbf{n}}\right)$Integer array Output
On exit: details of the column interchanges.
19: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ 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$
The rank of a is less than n. The problem does not have a unique solution.
${\mathbf{ifail}}=2$
The iterative refinement fails to converge. The matrix $A$ is too ill-conditioned.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

Although the correction process is continued until the solution has converged to full machine accuracy, all the figures in the final solution may not be correct since the correction $D$ to $X$ is itself the solution to a linear least squares problem. For a detailed error analysis see page 116 of Wilkinson and Reinsch (1971).

## 8Parallelism and Performance

f04amf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

The time taken by f04amf is approximately proportional to ${n}^{2}\left(3m-n\right)$, provided $r$ is small compared with $n$.

## 10Example

This example calculates the accurate least squares solution of the equations
 $1.1x1+0.9x2=2.2 1.2x1+1.0x2=2.3 1.0x1+1.0x2=2.1$

### 10.1Program Text

Program Text (f04amfe.f90)

### 10.2Program Data

Program Data (f04amfe.d)

### 10.3Program Results

Program Results (f04amfe.r)