Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_matop_real_gen_pseudinv (f01bl)

## Purpose

nag_matop_real_gen_pseudinv (f01bl) calculates the rank and pseudo-inverse of an m$m$ by n$n$ real matrix, mn$m\ge n$, using a QR$QR$ factorization with column interchanges.

## Syntax

[a, aijmax, irank, inc, ifail] = f01bl(t, a, 'm', m, 'n', n)
[a, aijmax, irank, inc, ifail] = nag_matop_real_gen_pseudinv(t, a, 'm', m, 'n', n)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 22: m has been made optional
.

## Description

Householder's factorization with column interchanges is used in the decomposition F = QU$F=QU$, where F$F$ is A$A$ with its columns permuted, Q$Q$ is the first r$r$ columns of an m$m$ by m$m$ orthogonal matrix and U$U$ is an r$r$ by n$n$ upper-trapezoidal matrix of rank r$r$. The pseudo-inverse of F$F$ is given by X$X$ where
 X = UT(UUT) − 1QT. $X=UT(UUT)-1QT.$
If the matrix is found to be of maximum rank, r = n$r=n$, U$U$ is a nonsingular n$n$ by n$n$ upper-triangular matrix and the pseudo-inverse of F$F$ simplifies to X = U1QT$X={U}^{-1}{Q}^{\mathrm{T}}$. The transpose of the pseudo-inverse of A$A$ is overwritten on A$A$.

## References

Peters G and Wilkinson J H (1970) The least squares problem and pseudo-inverses Comput. J. 13 309–316
Wilkinson J H and Reinsch C (1971) Handbook for Automatic Computation II, Linear Algebra Springer–Verlag

## Parameters

### Compulsory Input Parameters

1:     t – double scalar
The tolerance used to decide when elements can be regarded as zero (see Section [Further Comments]).
2:     a(lda,n) – double array
lda, the first dimension of the array, must satisfy the constraint ldam$\mathit{lda}\ge {\mathbf{m}}$.
The m$m$ by n$n$ rectangular matrix A$A$.

### Optional Input Parameters

1:     m – int64int32nag_int scalar
2:     n – int64int32nag_int scalar
Default: For m, the first dimension of the array a. The second dimension of the array a.
m$m$ and n$n$, the number of rows and columns in the matrix A$A$.
Constraint: mn${\mathbf{m}}\ge {\mathbf{n}}$.

lda d u ldu du

### Output Parameters

1:     a(lda,n) – double array
ldam$\mathit{lda}\ge {\mathbf{m}}$.
The transpose of the pseudo-inverse of A$A$.
2:     aijmax(n) – double array
aijmax(i)${\mathbf{aijmax}}\left(i\right)$ contains the element of largest modulus in the reduced matrix at the i$i$th stage. If r < n$r, then only the first r + 1$r+1$ elements of aijmax have values assigned to them; the remaining elements are unused. The ratio aijmax(1) / aijmax(r)${\mathbf{aijmax}}\left(1\right)/{\mathbf{aijmax}}\left(r\right)$ usually gives an indication of the condition number of the original matrix (see Section [Further Comments]).
3:     irank – int64int32nag_int scalar
r$r$, the rank of A$A$ as determined using the tolerance t.
4:     inc(n) – int64int32nag_int array
The record of the column interchanges in the Householder factorization.
5:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
ifail = 2${\mathbf{ifail}}=2$
Invalid tolerance, due to
 (i) t is negative, irank = − 1${\mathbf{irank}}=-1$; (ii) t too large, irank = 0${\mathbf{irank}}=0$; (iii) t too small, irank > 0${\mathbf{irank}}>0$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, m < n${\mathbf{m}}<{\mathbf{n}}$.

## Accuracy

For most matrices the pseudo-inverse is the best possible having regard to the condition of A$A$ and the choice of t. Note that only the singular value decomposition method can be relied upon to give maximum accuracy for the precision of computation used and correct determination of the condition of a matrix (see Wilkinson and Reinsch (1971)).
The computed factors Q$Q$ and U$U$ satisfy the relation QU = F + E$QU=F+E$ where
 ‖E‖2 < cε‖A‖2 + η×sqrt((m − r)(n − r)) $‖E‖2
in which c$c$ is a modest function of m$m$ and n$n$, η$\eta$ is the value of t, and ε$\epsilon$ is the machine precision.

The time taken by nag_matop_real_gen_pseudinv (f01bl) is approximately proportional to mnr$mnr$.
The most difficult practical problem is the determination of the rank of the matrix (see pages 314–315 of Peters and Wilkinson (1970)); only the singular value decomposition method gives a reliable indication of rank deficiency (see pages 134–151 of Wilkinson and Reinsch (1971) and nag_lapack_dgesvd (f08kb)). In nag_matop_real_gen_pseudinv (f01bl) a tolerance, t, is used to recognize ‘zero’ elements in the remaining matrix at each step in the factorization. The value of t should be set at n$n$ times the bound on possible errors in individual elements of the original matrix. If the elements of A$A$ vary widely in their orders of magnitude, of course this presents severe difficulties. Sound decisions can only be made by somebody who appreciates the underlying physical problem.
If the condition number of A$A$ is 10p${10}^{p}$ we expect to get p$p$ figures wrong in the pseudo-inverse. An estimate of the condition number is usually given by aijmax(1) / aijmax(r)${\mathbf{aijmax}}\left(1\right)/{\mathbf{aijmax}}\left(r\right)$.

## Example

```function nag_matop_real_gen_pseudinv_example
t = 2.99772935794301e-15;
a = [7, -2, 4, 9, 1.8;
3, 8, -4, 6, 1.3;
9, 6, 1, 5, 2.1;
-8, 7, 5, 2, 0.6;
4, -1, 2, 8, 1.3;
1, 6, 3, -5, 0.5];
[aOut, aijmax, irank, inc, ifail] = nag_matop_real_gen_pseudinv(t, a)
```
```

aOut =

0.0178   -0.0216    0.0520    0.0237    0.0072
-0.0118    0.0434   -0.0813    0.0357   -0.0014
0.0472    0.0294    0.0139   -0.0138    0.0077
-0.0566    0.0291    0.0474    0.0305    0.0050
-0.0037   -0.0138    0.0166    0.0357    0.0035
0.0384    0.0343    0.0576   -0.0571    0.0073

aijmax =

9.0000
9.3101
8.7461
5.6832
0.0000

irank =

4

inc =

4
2
4
4
0

ifail =

0

```
```function f01bl_example
t = 2.99772935794301e-15;
a = [7, -2, 4, 9, 1.8;
3, 8, -4, 6, 1.3;
9, 6, 1, 5, 2.1;
-8, 7, 5, 2, 0.6;
4, -1, 2, 8, 1.3;
1, 6, 3, -5, 0.5];
[aOut, aijmax, irank, inc, ifail] = f01bl(t, a)
```
```

aOut =

0.0178   -0.0216    0.0520    0.0237    0.0072
-0.0118    0.0434   -0.0813    0.0357   -0.0014
0.0472    0.0294    0.0139   -0.0138    0.0077
-0.0566    0.0291    0.0474    0.0305    0.0050
-0.0037   -0.0138    0.0166    0.0357    0.0035
0.0384    0.0343    0.0576   -0.0571    0.0073

aijmax =

9.0000
9.3101
8.7461
5.6832
0.0000

irank =

4

inc =

4
2
4
4
0

ifail =

0

```