# NAG FL Interfacef02wuf (real_​triang_​svd)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine f02wuf ( n, a, lda, b, ldb, q, ldq, sv, work,
 Integer, Intent (In) :: n, lda, ncolb, ldb, ldq Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (Inout) :: a(lda,*), b(ldb,*), q(ldq,*), work(*) Real (Kind=nag_wp), Intent (Out) :: sv(n) Logical, Intent (In) :: wantq, wantp
#include <nag.h>
 void f02wuf_ (const Integer *n, double a[], const Integer *lda, const Integer *ncolb, double b[], const Integer *ldb, const logical *wantq, double q[], const Integer *ldq, double sv[], const logical *wantp, double work[], Integer *ifail)
The routine may be called by the names f02wuf or nagf_eigen_real_triang_svd.

## 3Description

The $n×n$ upper triangular matrix $R$ is factorized as
 $R=QSPT,$
where $Q$ and $P$ are $n×n$ orthogonal matrices and $S$ is an $n×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×\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=(QK)S(PK)T$
is also a singular value decomposition of $A$.
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

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: $n$, the order of the matrix $R$.
If ${\mathbf{n}}=0$, an immediate return is effected.
Constraint: ${\mathbf{n}}\ge 0$.
2: $\mathbf{a}\left({\mathbf{lda}},*\right)$Real (Kind=nag_wp) array Input/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×n$ upper triangular part of the array a must contain the upper triangular matrix $R$.
On exit: if ${\mathbf{wantp}}=\mathrm{.TRUE.}$, the $n×n$ part of a will contain the $n×n$ orthogonal matrix ${P}^{\mathrm{T}}$, otherwise the $n×n$ upper triangular part of a is used as internal workspace, but the strictly lower triangular part of a is not referenced.
3: $\mathbf{lda}$Integer Input
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: $\mathbf{ncolb}$Integer Input
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: $\mathbf{b}\left({\mathbf{ldb}},*\right)$Real (Kind=nag_wp) array Input/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×\mathit{ncolb}$ part of the array b must contain the matrix to be transformed.
On exit: the leading $n×\mathit{ncolb}$ part of the array b is overwritten by the matrix ${Q}^{\mathrm{T}}B$.
6: $\mathbf{ldb}$Integer Input
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: $\mathbf{wantq}$Logical Input
On entry: must be .TRUE. if the matrix $Q$ is required.
If ${\mathbf{wantq}}=\mathrm{.FALSE.}$, the array q is not referenced.
8: $\mathbf{q}\left({\mathbf{ldq}},*\right)$Real (Kind=nag_wp) array Output
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×n$ part of the array q will contain the orthogonal matrix $Q$. Otherwise the array q is not referenced.
9: $\mathbf{ldq}$Integer Input
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: $\mathbf{sv}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit:
• If ${\mathbf{ifail}}={\mathbf{0}}$ the array sv will contain the diagonal elements of the matrix.
• If ${\mathbf{ifail}}={\mathbf{1}}$ the array sv will contain the diagonal elements of the bidiagonal matrix $E$ in the factorization $R=QE{P}^{T}$; the superdiagonal elements of $E$ will be contained in the first ${\mathbf{n}}-1$ elements of work.
11: $\mathbf{wantp}$Logical Input
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: $\mathbf{work}\left(*\right)$Real (Kind=nag_wp) array Output
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(1:{\mathbf{n}}-1\right)$ contains the super-diagonal elements of the bidiagonal matrix $E$ computed during the bidiagonalization stage; ${\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: $\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 $QR$ algorithm has failed to converge. $⟨\mathit{\text{value}}⟩$ singular values have not been found.
${\mathbf{ifail}}=-1$
On entry, ${\mathbf{lda}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lda}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{ldb}}=⟨\mathit{\text{value}}⟩$, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{ncolb}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{ncolb}}>0$, ${\mathbf{ldb}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{ldq}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: if ${\mathbf{wantq}}=\mathrm{.TRUE.}$, ${\mathbf{ldq}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 0$.
On entry, ${\mathbf{ncolb}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ncolb}}\ge 0$.
${\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

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

## 8Parallelism and Performance

f02wuf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
f02wuf 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.

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

## 10Example

This example finds the singular value decomposition of the $3×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 ) .$

### 10.1Program Text

Program Text (f02wufe.f90)

### 10.2Program Data

Program Data (f02wufe.d)

### 10.3Program Results

Program Results (f02wufe.r)