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$ by $n$ real matrix, $m\ge n$, using a $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:
 At Mark 22: m was made optional

## Description

Householder's factorization with column interchanges is used in the decomposition $F=QU$, where $F$ is $A$ with its columns permuted, $Q$ is the first $r$ columns of an $m$ by $m$ orthogonal matrix and $U$ is an $r$ by $n$ upper-trapezoidal matrix of rank $r$. The pseudo-inverse of $F$ is given by $X$ where
 $X=UTUUT-1QT.$
If the matrix is found to be of maximum rank, $r=n$, $U$ is a nonsingular $n$ by $n$ upper-triangular matrix and the pseudo-inverse of $F$ simplifies to $X={U}^{-1}{Q}^{\mathrm{T}}$. The transpose of the pseudo-inverse of $A$ is overwritten on $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:     $\mathrm{t}$ – double scalar
The tolerance used to decide when elements can be regarded as zero (see Further Comments).
2:     $\mathrm{a}\left(\mathit{lda},{\mathbf{n}}\right)$ – double array
lda, the first dimension of the array, must satisfy the constraint $\mathit{lda}\ge {\mathbf{m}}$.
The $m$ by $n$ rectangular matrix $A$.

### Optional Input Parameters

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

### Output Parameters

1:     $\mathrm{a}\left(\mathit{lda},{\mathbf{n}}\right)$ – double array
The transpose of the pseudo-inverse of $A$.
2:     $\mathrm{aijmax}\left({\mathbf{n}}\right)$ – double array
${\mathbf{aijmax}}\left(i\right)$ contains the element of largest modulus in the reduced matrix at the $i$th stage. If $r, then only the first $r+1$ elements of aijmax have values assigned to them; the remaining elements are unused. The ratio ${\mathbf{aijmax}}\left(1\right)/{\mathbf{aijmax}}\left(r\right)$ usually gives an indication of the condition number of the original matrix (see Further Comments).
3:     $\mathrm{irank}$int64int32nag_int scalar
$r$, the rank of $A$ as determined using the tolerance t.
4:     $\mathrm{inc}\left({\mathbf{n}}\right)$int64int32nag_int array
The record of the column interchanges in the Householder factorization.
5:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
${\mathbf{ifail}}=2$
Invalid tolerance, due to
 (i) t is negative, ${\mathbf{irank}}=-1$; (ii) t too large, ${\mathbf{irank}}=0$; (iii) t too small, ${\mathbf{irank}}>0$.
${\mathbf{ifail}}=3$
 On entry, ${\mathbf{m}}<{\mathbf{n}}$.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

For most matrices the pseudo-inverse is the best possible having regard to the condition of $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$ and $U$ satisfy the relation $QU=F+E$ where
 $E2
in which $c$ is a modest function of $m$ and $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$.
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$ times the bound on possible errors in individual elements of the original matrix. If the elements of $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$ is ${10}^{p}$ we expect to get $p$ figures wrong in the pseudo-inverse. An estimate of the condition number is usually given by ${\mathbf{aijmax}}\left(1\right)/{\mathbf{aijmax}}\left(r\right)$.

## Example

A complete program follows which outputs the maximum of the moduli of the ‘remaining’ elements at each step in the factorization, the rank, as determined by the given value of t, and the transposed pseudo-inverse. Data and results are given for an example which is a $6$ by $5$ matrix of deficient rank in which the last column is a linear combination of the other four. Setting t to $\epsilon$ times the norm of the matrix, the rank is correctly determined as $4$ and the pseudo-inverse is computed to full implementation accuracy.
```function f01bl_example

fprintf('f01bl example results\n\n');

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];

anorm = norm(a);
t = anorm*eps('double');

[ainv, aijmax, irank, inc, ifail] = f01bl(t, a);

fprintf('Maximum element in reduced matrix at Kth stages\n');
fprintf('   K      Modulus\n');
ind = [1:double(irank)+1];
fprintf('%4d   %12.4e\n',[ind ; aijmax']);
fprintf('\nRank = %5d\n\n', irank);
fprintf('Pseudo-inverse\n');
disp(ainv');

```
```f01bl example results

Maximum element in reduced matrix at Kth stages
K      Modulus
1     9.0000e+00
2     9.3101e+00
3     8.7461e+00
4     5.6832e+00
5     2.8449e-16

Rank =     4

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

```