f02 Chapter Contents
f02 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_real_partial_svd (f02wgc)

## 1  Purpose

nag_real_partial_svd (f02wgc) 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

 #include #include
void  nag_real_partial_svd (Nag_OrderType order, Integer m, Integer n, Integer k, Integer ncv,
 void (*av)(Integer *iflag, Integer m, Integer n, const double x[], double ax[], Nag_Comm *comm),
Integer *nconv, double sigma[], double u[], Integer pdu, double v[], Integer pdv, double resid[], Nag_Comm *comm, NagError *fail)

## 3  Description

nag_real_partial_svd (f02wgc) 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 functions 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 nag_real_partial_svd (f02wgc). Instead, you are required to supply a function, 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).

## 4  References

Wilkinson J H (1978) Singular Value Decomposition – Basic Aspects Numerical Software – Needs and Availability (ed D A H Jacobs) Academic Press

## 5  Arguments

1:     orderNag_OrderTypeInput
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by ${\mathbf{order}}=\mathrm{Nag_RowMajor}$. See Section 3.2.1.3 in the Essential Introduction for a more detailed explanation of the use of this argument.
Constraint: ${\mathbf{order}}=\mathrm{Nag_RowMajor}$ or Nag_ColMajor.
2:     mIntegerInput
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.
3:     nIntegerInput
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.
4:     kIntegerInput
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$.
5:     ncvIntegerInput
On entry: the dimension of the arrays sigma and resid. 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)$.
6:     avfunction, supplied by the userExternal Function
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$.
The specification of av is:
 void av (Integer *iflag, Integer m, Integer n, const double x[], double ax[], Nag_Comm *comm)
1:     iflagInteger *Input/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, nag_real_partial_svd (f02wgc) will exit immediately with fail set to iflag.
2:     mIntegerInput
On entry: the number of rows of the matrix $A$.
3:     nIntegerInput
On entry: the number of columns of the matrix $A$.
4:     x[$\mathit{dim}$]const doubleInput
Note: the dimension of the array x will be
• ${\mathbf{n}}$ when ${\mathbf{iflag}}=1$;
• ${\mathbf{m}}$ when ${\mathbf{iflag}}=2$.
On entry: the vector to be pre-multiplied by the matrix $A$ or ${A}^{\mathrm{T}}$.
5:     ax[$\mathit{dim}$]doubleOutput
Note: the dimension of the array ax will be
• ${\mathbf{m}}$ when ${\mathbf{iflag}}=1$;
• ${\mathbf{n}}$ when ${\mathbf{iflag}}=2$.
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:     commNag_Comm *
Pointer to structure of type Nag_Comm; the following members are relevant to av.
userdouble *
iuserInteger *
pPointer
The type Pointer will be void *. Before calling nag_real_partial_svd (f02wgc) you may allocate memory and initialize these pointers with various quantities for use by av when called from nag_real_partial_svd (f02wgc) (see Section 3.2.1 in the Essential Introduction).
7:     nconvInteger *Output
On exit: the number of converged singular values found.
8:     sigma[ncv]doubleOutput
On exit: the nconv converged singular values are stored in the first nconv elements of sigma.
9:     u[$\mathit{dim}$]doubleOutput
Note: the dimension, dim, of the array u must be at least
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pdu}}×{\mathbf{ncv}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}×{\mathbf{pdu}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
Where ${\mathbf{U}}\left(i,j\right)$ appears in this document, it refers to the array element
• ${\mathbf{u}}\left[\left(j-1\right)×{\mathbf{pdu}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{u}}\left[\left(i-1\right)×{\mathbf{pdu}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
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}}$.
10:   pduIntegerInput
On entry: the stride used in the array u.
Constraints:
• if ${\mathbf{order}}=\mathrm{Nag_ColMajor}$, ${\mathbf{pdu}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}\right)$;
• if ${\mathbf{order}}=\mathrm{Nag_RowMajor}$, ${\mathbf{pdu}}\ge {\mathbf{ncv}}$.
11:   v[$\mathit{dim}$]doubleOutput
Note: the dimension, dim, of the array v must be at least
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pdv}}×{\mathbf{ncv}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}×{\mathbf{pdv}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
Where ${\mathbf{V}}\left(i,j\right)$ appears in this document, it refers to the array element
• ${\mathbf{v}}\left[\left(j-1\right)×{\mathbf{pdv}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{v}}\left[\left(i-1\right)×{\mathbf{pdv}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
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}}$.
12:   pdvIntegerInput
On entry: the stride used in the array v.
Constraints:
• if ${\mathbf{order}}=\mathrm{Nag_ColMajor}$, ${\mathbf{pdv}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
• if ${\mathbf{order}}=\mathrm{Nag_RowMajor}$, ${\mathbf{pdv}}\ge {\mathbf{ncv}}$.
13:   resid[ncv]doubleOutput
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}$.
14:   commNag_Comm *Communication Structure
The NAG communication argument (see Section 3.2.1.1 in the Essential Introduction).
15:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).
nag_real_partial_svd (f02wgc) returns with NE_NOERROR if at least $k$ singular values have converged and the corresponding left and right singular vectors have been computed.

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_EIGENVALUES
The number of eigenvalues found to sufficient accuracy is zero.
NE_INT
On entry, ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{k}}>0$.
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 0$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 0$.
On entry, ${\mathbf{pdu}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdu}}>0$.
On entry, ${\mathbf{pdv}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdv}}>0$.
NE_INT_2
On entry, ${\mathbf{pdu}}=〈\mathit{\text{value}}〉$ and ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdu}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{pdu}}=〈\mathit{\text{value}}〉$ and ${\mathbf{ncv}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdu}}\ge {\mathbf{ncv}}$.
On entry, ${\mathbf{pdv}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdv}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{pdv}}=〈\mathit{\text{value}}〉$ and ${\mathbf{ncv}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdv}}\ge {\mathbf{ncv}}$.
NE_INT_3
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$, ${\mathbf{n}}=〈\mathit{\text{value}}〉$ and ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: $0<{\mathbf{k}}<\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{m}},{\mathbf{n}}\right)-1$.
NE_INT_4
On entry, ${\mathbf{k}}=〈\mathit{\text{value}}〉$, ${\mathbf{ncv}}=〈\mathit{\text{value}}〉$, ${\mathbf{m}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{k}}<{\mathbf{ncv}}\le \mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{m}},{\mathbf{n}}\right)$.
NE_INTERNAL_ERROR
An error occurred during an internal call. Consider increasing the size of ncv relative to k.
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_LANCZOS_ITERATION
No shifts could be applied during a cycle of the implicitly restarted Lanczos iteration.
NE_MAX_ITER
The maximum number of iterations has been reached. The maximum number of iterations $\text{}=〈\mathit{\text{value}}〉$. The number of converged eigenvalues $\text{}=〈\mathit{\text{value}}〉$.
NE_NO_LANCZOS_FAC
Could not build a full Lanczos factorization.
NE_USER_STOP
On output from user-defined function av, iflag was set to a negative value, ${\mathbf{iflag}}=〈\mathit{\text{value}}〉$.

## 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 (f02wgce.c)

### 9.2  Program Data

Program Data (f02wgce.d)

### 9.3  Program Results

Program Results (f02wgce.r)