# NAG FL Interfacef02wgf (real_​gen_​partialsvd)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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

## 2Specification

Fortran Interface
 Subroutine f02wgf ( m, n, k, ncv, av, u, ldu, v, ldv,
 Integer, Intent (In) :: m, n, k, ncv, ldu, ldv Integer, Intent (Inout) :: iuser(*), ifail Integer, Intent (Out) :: nconv Real (Kind=nag_wp), Intent (Inout) :: u(ldu,ncv), v(ldv,ncv), ruser(*) Real (Kind=nag_wp), Intent (Out) :: sigma(ncv), resid(ncv) External :: av
#include <nag.h>
 void f02wgf_ (const Integer *m, const Integer *n, const Integer *k, const Integer *ncv, void (NAG_CALL *av)(Integer *iflag, const Integer *m, const Integer *n, const double x[], double ax[], Integer iuser[], double ruser[]),Integer *nconv, double sigma[], double u[], const Integer *ldu, double v[], const Integer *ldv, double resid[], Integer iuser[], double ruser[], Integer *ifail)
The routine may be called by the names f02wgf or nagf_eigen_real_gen_partialsvd.

## 3Description

f02wgf computes a few, $k$, of the largest singular values and corresponding vectors of an $m×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×n$ matrix $A$ is given by
 $A=UΣVT ,$
where $U$ and $V$ are orthogonal and $\Sigma$ is an $m×n$ diagonal matrix with real diagonal elements, ${\sigma }_{i}$, such that
 $σ1 ≥ σ2 ≥⋯≥ σ min(m,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

## 5Arguments

1: $\mathbf{m}$Integer Input
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: $\mathbf{n}$Integer Input
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: $\mathbf{k}$Integer Input
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: $\mathbf{ncv}$Integer Input
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: $\mathbf{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 argument iuser and ruser as supplied to f02wgf. You are free to use these arrays to supply information to av.
The specification of av is:
Fortran Interface
 Subroutine av ( m, n, x, ax,
 Integer, Intent (In) :: m, n Integer, Intent (Inout) :: iflag, iuser(*) Real (Kind=nag_wp), Intent (In) :: x(*) Real (Kind=nag_wp), Intent (Inout) :: ax(*), ruser(*)
 void av (Integer *iflag, const Integer *m, const Integer *n, const double x[], double ax[], Integer iuser[], double ruser[])
1: $\mathbf{iflag}$Integer 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, f02wgf will exit immediately with ifail set to iflag.
2: $\mathbf{m}$Integer Input
On entry: the number of rows of the matrix $A$.
3: $\mathbf{n}$Integer Input
On entry: the number of columns of the matrix $A$.
4: $\mathbf{x}\left(*\right)$Real (Kind=nag_wp) array Input
On entry: the vector to be pre-multiplied by the matrix $A$ or ${A}^{\mathrm{T}}$.
5: $\mathbf{ax}\left(*\right)$Real (Kind=nag_wp) array Output
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: $\mathbf{iuser}\left(*\right)$Integer array User Workspace
7: $\mathbf{ruser}\left(*\right)$Real (Kind=nag_wp) array User Workspace
av is called with the arguments iuser and ruser as supplied to f02wgf. You should use the arrays iuser and ruser to supply information to av.
av must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which f02wgf is called. Arguments denoted as Input must not be changed by this procedure.
Note: av should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by f02wgf. If your code inadvertently does return any NaNs or infinities, f02wgf is likely to produce unexpected results.
6: $\mathbf{nconv}$Integer Output
On exit: the number of converged singular values found.
7: $\mathbf{sigma}\left({\mathbf{ncv}}\right)$Real (Kind=nag_wp) array Output
On exit: the nconv converged singular values are stored in the first nconv elements of sigma.
8: $\mathbf{u}\left({\mathbf{ldu}},{\mathbf{ncv}}\right)$Real (Kind=nag_wp) array Output
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: $\mathbf{ldu}$Integer Input
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: $\mathbf{v}\left({\mathbf{ldv}},{\mathbf{ncv}}\right)$Real (Kind=nag_wp) array Output
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: $\mathbf{ldv}$Integer Input
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: $\mathbf{resid}\left({\mathbf{ncv}}\right)$Real (Kind=nag_wp) array Output
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: $\mathbf{iuser}\left(*\right)$Integer array User Workspace
14: $\mathbf{ruser}\left(*\right)$Real (Kind=nag_wp) array User 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.
15: $\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).
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.

## 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$
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\ge 0$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{k}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{k}}>0$.
${\mathbf{ifail}}=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)$.
${\mathbf{ifail}}=5$
On entry, ${\mathbf{ldu}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldu}}\ge {\mathbf{m}}$.
${\mathbf{ifail}}=6$
On entry, ${\mathbf{ldv}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldv}}\ge {\mathbf{n}}$.
${\mathbf{ifail}}=8$
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}}⟩$.
${\mathbf{ifail}}=9$
No shifts could be applied during a cycle of the implicitly restarted Lanczos iteration.
${\mathbf{ifail}}=10$
Could not build a full Lanczos factorization.
${\mathbf{ifail}}=11$
The number of eigenvalues found to sufficient accuracy is zero.
${\mathbf{ifail}}=20$
An error occurred during an internal call. Consider increasing the size of ncv relative to k.
${\mathbf{ifail}}<0$
On output from user-defined routine av, iflag was set to a negative value, ${\mathbf{iflag}}=⟨\mathit{\text{value}}⟩$.
${\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

See Section 2.14.2 in the F08 Chapter Introduction.

## 8Parallelism and Performance

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

None.

## 10Example

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×n$ real matrix derived from the simplest finite difference discretization of the two-dimensional kernel $k\left(s,t\right)dt$ where
 $k(s,t) = { s(t-1) if ​0≤s≤t≤ 1 t(s-1) if ​0≤t

### 10.1Program Text

Program Text (f02wgfe.f90)

### 10.2Program Data

Program Data (f02wgfe.d)

### 10.3Program Results

Program Results (f02wgfe.r)