F02 Chapter Contents
F02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentF02WUF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

F02WUF returns all, or part, of the singular value decomposition of a real upper triangular matrix.

## 2  Specification

 SUBROUTINE F02WUF ( N, A, LDA, NCOLB, B, LDB, WANTQ, Q, LDQ, SV, WANTP, WORK, IFAIL)
 INTEGER N, LDA, NCOLB, LDB, LDQ, IFAIL REAL (KIND=nag_wp) A(LDA,*), B(LDB,*), Q(LDQ,*), SV(N), WORK(*) LOGICAL WANTQ, WANTP

## 3  Description

The $n$ by $n$ upper triangular matrix $R$ is factorized as
 $R=QSPT,$
where $Q$ and $P$ are $n$ by $n$ orthogonal matrices and $S$ is an $n$ by $n$ diagonal matrix with non-negative diagonal elements, ${\sigma }_{1},{\sigma }_{2},\dots ,{\sigma }_{n}$, ordered such that
 $σ1≥σ2≥…≥σn≥0.$
The columns of $Q$ are the left-hand singular vectors of $R$, the diagonal elements of $S$ are the singular values of $R$ and the columns of $P$ are the right-hand singular vectors of $R$.
Either or both of $Q$ and ${P}^{\mathrm{T}}$ may be requested and the matrix $C$ given by
 $C=QTB,$
where $B$ is an $n$ by $\mathit{ncolb}$ given matrix, may also be requested.
The routine obtains the singular value decomposition by first reducing $R$ to bidiagonal form by means of Givens plane rotations and then using the $QR$ algorithm to obtain the singular value decomposition of the bidiagonal form.
Good background descriptions to the singular value decomposition are given in Chan (1982), Dongarra et al. (1979), Golub and Van Loan (1996), Hammarling (1985) and Wilkinson (1978).
Note that if $K$ is any orthogonal diagonal matrix so that
 $KKT=I$
(that is the diagonal elements of $K$ are $+1$ or $-1$) then
 $A=QKSPKT$
is also a singular value decomposition of $A$.

## 4  References

Chan T F (1982) An improved algorithm for computing the singular value decomposition ACM Trans. Math. Software 8 72–83
Dongarra J J, Moler C B, Bunch J R and Stewart G W (1979) LINPACK Users' Guide SIAM, Philadelphia
Golub G H and Van Loan C F (1996) Matrix Computations (3rd Edition) Johns Hopkins University Press, Baltimore
Hammarling S (1985) The singular value decomposition in multivariate statistics SIGNUM Newsl. 20(3) 2–25
Wilkinson J H (1978) Singular Value Decomposition – Basic Aspects Numerical Software – Needs and Availability (ed D A H Jacobs) Academic Press

## 5  Parameters

1:     N – INTEGERInput
On entry: $n$, the order of the matrix $R$.
If ${\mathbf{N}}=0$, an immediate return is effected.
Constraint: ${\mathbf{N}}\ge 0$.
2:     A(LDA,$*$) – REAL (KIND=nag_wp) arrayInput/Output
Note: the second dimension of the array A must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{N}}\right)$.
On entry: the leading $n$ by $n$ upper triangular part of the array A must contain the upper triangular matrix $R$.
On exit: if ${\mathbf{WANTP}}=\mathrm{.TRUE.}$, the $n$ by $n$ part of A will contain the $n$ by $n$ orthogonal matrix ${P}^{\mathrm{T}}$, otherwise the $n$ by $n$ upper triangular part of A is used as internal workspace, but the strictly lower triangular part of A is not referenced.
3:     LDA – INTEGERInput
On entry: the first dimension of the array A as declared in the (sub)program from which F02WUF is called.
Constraint: ${\mathbf{LDA}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{N}}\right)$.
4:     NCOLB – INTEGERInput
On entry: $\mathit{ncolb}$, the number of columns of the matrix $B$.
If ${\mathbf{NCOLB}}=0$, the array B is not referenced.
Constraint: ${\mathbf{NCOLB}}\ge 0$.
5:     B(LDB,$*$) – REAL (KIND=nag_wp) arrayInput/Output
Note: the second dimension of the array B must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{NCOLB}}\right)$.
On entry: with ${\mathbf{NCOLB}}>0$, the leading $n$ by $\mathit{ncolb}$ part of the array B must contain the matrix to be transformed.
On exit: the leading $n$ by $\mathit{ncolb}$ part of the array B is overwritten by the matrix ${Q}^{\mathrm{T}}B$.
6:     LDB – INTEGERInput
On entry: the first dimension of the array B as declared in the (sub)program from which F02WUF is called.
Constraints:
• if ${\mathbf{NCOLB}}>0$, ${\mathbf{LDB}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{N}}\right)$;
• otherwise ${\mathbf{LDB}}\ge 1$.
7:     WANTQ – LOGICALInput
On entry: must be .TRUE. if the matrix $Q$ is required.
If ${\mathbf{WANTQ}}=\mathrm{.FALSE.}$, the array Q is not referenced.
8:     Q(LDQ,$*$) – REAL (KIND=nag_wp) arrayOutput
Note: the second dimension of the array Q must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{N}}\right)$ if ${\mathbf{WANTQ}}=\mathrm{.TRUE.}$, and at least $1$ otherwise.
On exit: with ${\mathbf{WANTQ}}=\mathrm{.TRUE.}$, the leading $n$ by $n$ part of the array Q will contain the orthogonal matrix $Q$. Otherwise the array Q is not referenced.
9:     LDQ – INTEGERInput
On entry: the first dimension of the array Q as declared in the (sub)program from which F02WUF is called.
Constraints:
• if ${\mathbf{WANTQ}}=\mathrm{.TRUE.}$, ${\mathbf{LDQ}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{N}}\right)$;
• otherwise ${\mathbf{LDQ}}\ge 1$.
10:   SV(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the array SV will contain the $n$ diagonal elements of the matrix $S$.
11:   WANTP – LOGICALInput
On entry: must be .TRUE. if the matrix ${P}^{\mathrm{T}}$ is required, in which case ${P}^{\mathrm{T}}$ is overwritten on the array A, otherwise WANTP must be .FALSE..
12:   WORK($*$) – REAL (KIND=nag_wp) arrayOutput
Note: the dimension of the array WORK must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,2×\left({\mathbf{N}}-1\right)\right)$ if ${\mathbf{NCOLB}}=0$ and ${\mathbf{WANTQ}}=\mathrm{.FALSE.}$ and ${\mathbf{WANTP}}=\mathrm{.FALSE.}$, $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,3×\left({\mathbf{N}}-1\right)\right)$ if (${\mathbf{NCOLB}}=0$ and ${\mathbf{WANTQ}}=\mathrm{.FALSE.}$ and ${\mathbf{WANTP}}=\mathrm{.TRUE.}$) or (${\mathbf{WANTP}}=\mathrm{.FALSE.}$ and (${\mathbf{NCOLB}}>0$ or ${\mathbf{WANTQ}}=\mathrm{.TRUE.}$)), and at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,5×\left({\mathbf{N}}-1\right)\right)$ otherwise.
On exit: ${\mathbf{WORK}}\left({\mathbf{N}}\right)$ contains the total number of iterations taken by the $QR$ algorithm.
The rest of the array is used as internal workspace.
13:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ 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 parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{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$
 On entry, ${\mathbf{N}}<0$, or ${\mathbf{LDA}}<{\mathbf{N}}$, or ${\mathbf{NCOLB}}<0$, or ${\mathbf{LDB}}<{\mathbf{N}}$ and ${\mathbf{NCOLB}}>0$, or ${\mathbf{LDQ}}<{\mathbf{N}}$ and ${\mathbf{WANTQ}}=\mathrm{.TRUE.}$.
${\mathbf{IFAIL}}>0$
The $QR$ algorithm has failed to converge in $50×{\mathbf{N}}$ iterations. In this case ${\mathbf{SV}}\left(1\right),{\mathbf{SV}}\left(2\right),\dots ,{\mathbf{SV}}\left({\mathbf{IFAIL}}\right)$ may not have been found correctly and the remaining singular values may not be the smallest. The matrix $R$ will nevertheless have been factorized as $R=QE{P}^{\mathrm{T}}$, where $E$ is a bidiagonal matrix with ${\mathbf{SV}}\left(1\right),{\mathbf{SV}}\left(2\right),\dots ,{\mathbf{SV}}\left(n\right)$ as the diagonal elements and ${\mathbf{WORK}}\left(1\right),{\mathbf{WORK}}\left(2\right),\dots ,{\mathbf{WORK}}\left(n-1\right)$ as the superdiagonal elements.
This failure is not likely to occur.

## 7  Accuracy

The computed factors $Q$, $S$ and $P$ satisfy the relation
 $QSPT=R+E,$
where
 $E≤cε A ,$
$\epsilon$ is the machine precision, $c$ is a modest function of $n$ and $‖.‖$ denotes the spectral (two) norm. Note that $‖A‖={\mathbf{SV}}\left(1\right)$.
A similar result holds for the computed matrix ${Q}^{\mathrm{T}}B$.
The computed matrix $Q$ satisfies the relation
 $Q=T+F,$
where $T$ is exactly orthogonal and
 $F≤dε,$
where $d$ is a modest function of $n$. A similar result holds for $P$.

For given values of NCOLB, WANTQ and WANTP, the number of floating point operations required is approximately proportional to ${n}^{3}$.
Following the use of this routine the rank of $R$ may be estimated by a call to the INTEGER FUNCTION F06KLF. The statement
```IRANK = F06KLF(N,SV,1,TOL)
```
returns the value $\left(k-1\right)$ in IRANK, where $k$ is the smallest integer for which ${\mathbf{SV}}\left(k\right)<\mathit{tol}×{\mathbf{SV}}\left(1\right)$, and $\mathit{tol}$ is the tolerance supplied in TOL, so that IRANK is an estimate of the rank of $S$ and thus also of $R$. If TOL is supplied as negative then the machine precision is used in place of TOL .

## 9  Example

This example finds the singular value decomposition of the $3$ by $3$ upper triangular matrix
 $A= -4 -2 -3 0 -3 -2 0 0 -4 ,$
together with the vector ${Q}^{\mathrm{T}}b$ for the vector
 $b= -1 -1 -1 .$

### 9.1  Program Text

Program Text (f02wufe.f90)

### 9.2  Program Data

Program Data (f02wufe.d)

### 9.3  Program Results

Program Results (f02wufe.r)