NAG Library Routine Document

f04amf (real_gen_lsqsol)

1
Purpose

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

2
Specification

Fortran Interface
Subroutine f04amf ( a, lda, x, ldx, b, ldb, m, n, ir, eps, qr, ldqr, alpha, e, y, z, r, ipiv, ifail)
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)
C Header Interface
#include <nagmk26.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)

3
Description

To compute the least squares solution to a set of m linear equations in n unknowns mn AX=B, f04amf first computes a QR factorization of A with column pivoting, AP=QR, where R is upper triangular, Q is an m by m orthogonal matrix, and P is a permutation matrix. QT is applied to the m by r right-hand side matrix B to give C=QTB, and the n by 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.

4
References

Wilkinson J H and Reinsch C (1971) Handbook for Automatic Computation II, Linear Algebra Springer–Verlag

5
Arguments

1:     aldan – Real (Kind=nag_wp) arrayInput
On entry: the m by n matrix A.
2:     lda – IntegerInput
On entry: the first dimension of the array a as declared in the (sub)program from which f04amf is called.
Constraint: ldam.
3:     xldxir – Real (Kind=nag_wp) arrayOutput
On exit: the n by r solution matrix X.
4:     ldx – IntegerInput
On entry: the first dimension of the array x as declared in the (sub)program from which f04amf is called.
Constraint: ldxn.
5:     bldbir – Real (Kind=nag_wp) arrayInput
On entry: the m by r right-hand side matrix B.
6:     ldb – IntegerInput
On entry: the first dimension of the array b as declared in the (sub)program from which f04amf is called.
Constraint: ldbm.
7:     m – IntegerInput
On entry: m, the number of rows of the matrix A, i.e., the number of equations.
Constraint: m1.
8:     n – IntegerInput
On entry: n, the number of columns of the matrix A, i.e., the number of unknowns.
Constraint: 0nm.
9:     ir – IntegerInput
On entry: r, the number of right-hand sides.
10:   eps – Real (Kind=nag_wp)Input
On entry: must be set to the value of the machine precision.
11:   qrldqrn – Real (Kind=nag_wp) arrayOutput
On exit: details of the QR factorization.
12:   ldqr – IntegerInput
On entry: the first dimension of the array qr as declared in the (sub)program from which f04amf is called.
Constraint: ldqrm.
13:   alphan – Real (Kind=nag_wp) arrayOutput
On exit: the diagonal elements of the upper triangular matrix R.
14:   en – Real (Kind=nag_wp) arrayWorkspace
15:   yn – Real (Kind=nag_wp) arrayWorkspace
16:   zn – Real (Kind=nag_wp) arrayWorkspace
17:   rm – Real (Kind=nag_wp) arrayWorkspace
18:   ipivn – Integer arrayOutput
On exit: details of the column interchanges.
19:   ifail – IntegerInput/Output
On entry: ifail must be set to 0, -1 or 1. 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 -1 or 1 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 -1 or 1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6
Error Indicators and Warnings

If on entry 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:
ifail=1
The rank of a is less than n. The problem does not have a unique solution.
ifail=2
The iterative refinement fails to converge. The matrix A is too ill-conditioned.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
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.
ifail=-999
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

7
Accuracy

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).

8
Parallelism 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.

9
Further Comments

The time taken by f04amf is approximately proportional to n23m-n, provided r is small compared with n.

10
Example

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.1
Program Text

Program Text (f04amfe.f90)

10.2
Program Data

Program Data (f04amfe.d)

10.3
Program Results

Program Results (f04amfe.r)