F02 Chapter Contents
F02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentF02WGF

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

F02WGF returns leading terms in the singular value decomposition (SVD) of a real general matrix and computes the corresponding left and right singular vectors.

## 2  Specification

 SUBROUTINE F02WGF ( M, N, K, NCV, AV, NCONV, SIGMA, U, LDU, V, LDV, RESID, IUSER, RUSER, IFAIL)
 INTEGER M, N, K, NCV, NCONV, LDU, LDV, IUSER(*), IFAIL REAL (KIND=nag_wp) SIGMA(NCV), U(LDU,NCV), V(LDV,NCV), RESID(NCV), RUSER(*) EXTERNAL AV

## 3  Description

F02WGF computes a few, $k$, of the largest singular values and corresponding vectors of an $m$ by $n$ matrix $A$. The value of $k$ should be small relative to $m$ and $n$, for example $k\sim O\left(\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)\right)$. The full singular value decomposition (SVD) of an $m$ by $n$ matrix $A$ is given by
 $A=UΣVT ,$
where $U$ and $V$ are orthogonal and $\Sigma$ is an $m$ by $n$ diagonal matrix with real diagonal elements, ${\sigma }_{i}$, such that
 $σ1 ≥ σ2 ≥⋯≥ σ minm,n ≥ 0 .$
The ${\sigma }_{i}$ are the singular values of $A$ and the first $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ columns of $U$ and $V$ are the left and right singular vectors of $A$.
If ${U}_{k}$, ${V}_{k}$ denote the leading $k$ columns of $U$ and $V$ respectively, and if ${\Sigma }_{k}$ denotes the leading principal submatrix of $\Sigma$, then
 $Ak ≡ Uk Σk VTk$
is the best rank-$k$ approximation to $A$ in both the $2$-norm and the Frobenius norm.
The singular values and singular vectors satisfy
 $Avi = σi ui and ATui = σi vi so that ATA νi = σi2 νi ​ and ​ A AT ui = σ i 2 u i ,$
where ${u}_{i}$ and ${v}_{i}$ are the $i$th columns of ${U}_{k}$ and ${V}_{k}$ respectively.
Thus, for $m\ge n$, the largest singular values and corresponding right singular vectors are computed by finding eigenvalues and eigenvectors for the symmetric matrix ${A}^{\mathrm{T}}A$. For $m, the largest singular values and corresponding left singular vectors are computed by finding eigenvalues and eigenvectors for the symmetric matrix $A{A}^{\mathrm{T}}$. These eigenvalues and eigenvectors are found using routines from Chapter F12. You should read the F12 Chapter Introduction for full details of the method used here.
The real matrix $A$ is not explicitly supplied to F02WGF. Instead, you are required to supply a routine, AV, that must calculate one of the requested matrix-vector products $Ax$ or ${A}^{\mathrm{T}}x$ for a given real vector $x$ (of length $n$ or $m$ respectively).
Wilkinson J H (1978) Singular Value Decomposition – Basic Aspects Numerical Software – Needs and Availability (ed D A H Jacobs) Academic Press

## 5  Parameters

1:     M – INTEGERInput
On entry: $m$, the number of rows of the matrix $A$.
Constraint: ${\mathbf{M}}\ge 0$.
If ${\mathbf{M}}=0$, an immediate return is effected.
2:     N – INTEGERInput
On entry: $n$, the number of columns of the matrix $A$.
Constraint: ${\mathbf{N}}\ge 0$.
If ${\mathbf{N}}=0$, an immediate return is effected.
3:     K – INTEGERInput
On entry: $k$, the number of singular values to be computed.
Constraint: $0<{\mathbf{K}}<\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{M}},{\mathbf{N}}\right)-1$.
4:     NCV – INTEGERInput
On entry: the dimension of the arrays SIGMA and RESID and the second dimension of the arrays U and V as declared in the (sub)program from which F02WGF is called. This is the number of Lanczos basis vectors to use during the computation of the largest eigenvalues of ${A}^{\mathrm{T}}A$ ($m\ge n$) or $A{A}^{\mathrm{T}}$ ($m).
At present there is no a priori analysis to guide the selection of NCV relative to K. However, it is recommended that ${\mathbf{NCV}}\ge 2×{\mathbf{K}}+1$. If many problems of the same type are to be solved, you should experiment with varying NCV while keeping K fixed for a given test problem. This will usually decrease the required number of matrix-vector operations but it also increases the internal storage required to maintain the orthogonal basis vectors. The optimal ‘cross-over’ with respect to CPU time is problem dependent and must be determined empirically.
Constraint: ${\mathbf{K}}<{\mathbf{NCV}}\le \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{M}},{\mathbf{N}}\right)$.
5:     AV – SUBROUTINE, supplied by the user.External Procedure
AV must return the vector result of the matrix-vector product $Ax$ or ${A}^{\mathrm{T}}x$, as indicated by the input value of IFLAG, for the given vector $x$.
AV is called from F02WGF with the parameter IUSER and RUSER as supplied to F02WGF. You are free to use these arrays to supply information to AV.
The specification of AV is:
 SUBROUTINE AV ( IFLAG, M, N, X, AX, IUSER, RUSER)
 INTEGER IFLAG, M, N, IUSER(*) REAL (KIND=nag_wp) X(*), AX(*), RUSER(*)
1:     IFLAG – INTEGERInput/Output
On entry: if ${\mathbf{IFLAG}}=1$, AX must return the $m$-vector result of the matrix-vector product $Ax$.
If ${\mathbf{IFLAG}}=2$, AX must return the $n$-vector result of the matrix-vector product ${A}^{\mathrm{T}}x$.
On exit: may be used as a flag to indicate a failure in the computation of $Ax$ or ${A}^{\mathrm{T}}x$. If IFLAG is negative on exit from AV, F02WGF will exit immediately with IFAIL set to IFLAG.
2:     M – INTEGERInput
On entry: the number of rows of the matrix $A$.
3:     N – INTEGERInput
On entry: the number of columns of the matrix $A$.
4:     X($*$) – REAL (KIND=nag_wp) arrayInput
On entry: the vector to be pre-multiplied by the matrix $A$ or ${A}^{\mathrm{T}}$.
5:     AX($*$) – REAL (KIND=nag_wp) arrayOutput
On exit: if ${\mathbf{IFLAG}}=1$, contains the $m$-vector result of the matrix-vector product $Ax$.
If ${\mathbf{IFLAG}}=2$, contains the $n$-vector result of the matrix-vector product ${A}^{\mathrm{T}}x$.
6:     IUSER($*$) – INTEGER arrayUser Workspace
7:     RUSER($*$) – REAL (KIND=nag_wp) arrayUser Workspace
AV is called with the parameters IUSER and RUSER as supplied to F02WGF. You are free to use the arrays IUSER and RUSER to supply information to AV as an alternative to using COMMON global variables.
AV must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which F02WGF is called. Parameters denoted as Input must not be changed by this procedure.
6:     NCONV – INTEGEROutput
On exit: the number of converged singular values found.
7:     SIGMA(NCV) – REAL (KIND=nag_wp) arrayOutput
On exit: the NCONV converged singular values are stored in the first NCONV elements of SIGMA.
8:     U(LDU,NCV) – REAL (KIND=nag_wp) arrayOutput
On exit: the left singular vectors corresponding to the singular values stored in SIGMA.
The $\mathit{i}$th element of the $\mathit{j}$th left singular vector ${u}_{\mathit{j}}$ is stored in ${\mathbf{U}}\left(\mathit{i},\mathit{j}\right)$, for $\mathit{i}=1,2,\dots ,m$ and $\mathit{j}=1,2,\dots ,{\mathbf{NCONV}}$.
9:     LDU – INTEGERInput
On entry: the first dimension of the array U as declared in the (sub)program from which F02WGF is called.
Constraint: ${\mathbf{LDU}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{M}}\right)$.
10:   V(LDV,NCV) – REAL (KIND=nag_wp) arrayOutput
On exit: the right singular vectors corresponding to the singular values stored in SIGMA.
The $\mathit{i}$th element of the $\mathit{j}$th right singular vector ${v}_{\mathit{j}}$ is stored in ${\mathbf{V}}\left(\mathit{i},\mathit{j}\right)$, for $\mathit{i}=1,2,\dots ,n$ and $\mathit{j}=1,2,\dots ,{\mathbf{NCONV}}$.
11:   LDV – INTEGERInput
On entry: the first dimension of the array V as declared in the (sub)program from which F02WGF is called.
Constraint: ${\mathbf{LDV}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{N}}\right)$.
12:   RESID(NCV) – REAL (KIND=nag_wp) arrayOutput
On exit: the residual $‖A{v}_{j}-{\sigma }_{j}{u}_{j}‖$, for $m\ge n$, or $‖{A}^{\mathrm{T}}{u}_{j}-{\sigma }_{j}{v}_{j}‖$, for $m, for each of the converged singular values ${\sigma }_{j}$ and corresponding left and right singular vectors ${u}_{j}$ and ${v}_{j}$.
13:   IUSER($*$) – INTEGER arrayUser Workspace
14:   RUSER($*$) – REAL (KIND=nag_wp) arrayUser Workspace
IUSER and RUSER are not used by F02WGF, but are passed directly to AV and may be used to pass information to this routine as an alternative to using COMMON global variables.
15:   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).
F02WGF returns with ${\mathbf{IFAIL}}={\mathbf{0}}$ if at least $k$ singular values have converged and the corresponding left and right singular vectors have been computed.

## 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}}<0$
IFLAG was set to a negative value following a call to AV.
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{M}}<0$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{N}}<0$.
${\mathbf{IFAIL}}=3$
 On entry, ${\mathbf{K}}\le 0$.
${\mathbf{IFAIL}}=4$
 On entry, ${\mathbf{NCV}}\le {\mathbf{K}}+1$, or ${\mathbf{NCV}}>\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{M}},{\mathbf{N}}\right)$.
${\mathbf{IFAIL}}=5$
 On entry, ${\mathbf{LDU}}<{\mathbf{M}}$.
${\mathbf{IFAIL}}=6$
 On entry, ${\mathbf{LDV}}<{\mathbf{N}}$.
${\mathbf{IFAIL}}=7$
No converged singular values were found to sufficient accuracy.
${\mathbf{IFAIL}}=8$
The internal maximum number of iterations has been reached. Some singular values may have converged.
${\mathbf{IFAIL}}=9$
${\mathbf{IFAIL}}=10$
${\mathbf{IFAIL}}=20$
An error occurred during an internal call. Consider increasing the size of NCV relative to K.

## 7  Accuracy

See Section 2.14.2 in the F08 Chapter Introduction.

None.

## 9  Example

This example finds the four largest singular values ($\sigma$) and corresponding right and left singular vectors for the matrix $A$, where $A$ is the $m$ by $n$ real matrix derived from the simplest finite difference discretization of the two-dimensional kernal $k\left(s,t\right)dt$ where
 $ks,t = st-1 if ​0≤s≤t≤ 1 ts-1 if ​0≤t

### 9.1  Program Text

Program Text (f02wgfe.f90)

### 9.2  Program Data

Program Data (f02wgfe.d)

### 9.3  Program Results

Program Results (f02wgfe.r)