nag_real_partial_svd (f02wgc) (PDF version)
f02 Chapter Contents
f02 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_real_partial_svd (f02wgc)

 Contents

    1  Purpose
    7  Accuracy

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 <nag.h>
#include <nagf02.h>
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 kOminm,n. 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 Σ is an m by n diagonal matrix with real diagonal elements, σi, such that
σ1 σ2 σ minm,n 0 .  
The σi are the singular values of A and the first minm,n columns of U and V are the left and right singular vectors of A.
If Uk, Vk denote the leading k columns of U and V respectively, and if Σk denotes the leading principal submatrix of Σ, 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 ui and vi are the ith columns of Uk and Vk respectively.
Thus, for mn, the largest singular values and corresponding right singular vectors are computed by finding eigenvalues and eigenvectors for the symmetric matrix ATA. For m<n, the largest singular values and corresponding left singular vectors are computed by finding eigenvalues and eigenvectors for the symmetric matrix AAT. 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 ATx 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:     order Nag_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 order=Nag_RowMajor. See Section 3.2.1.3 in the Essential Introduction for a more detailed explanation of the use of this argument.
Constraint: order=Nag_RowMajor or Nag_ColMajor.
2:     m IntegerInput
On entry: m, the number of rows of the matrix A.
Constraint: m0.
If m=0, an immediate return is effected.
3:     n IntegerInput
On entry: n, the number of columns of the matrix A.
Constraint: n0.
If n=0, an immediate return is effected.
4:     k IntegerInput
On entry: k, the number of singular values to be computed.
Constraint: 0<k<minm,n-1.
5:     ncv IntegerInput
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 ATA (mn) or AAT (m<n).
At present there is no a priori analysis to guide the selection of ncv relative to k. However, it is recommended that ncv2×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: k<ncvminm,n.
6:     av function, supplied by the userExternal Function
av must return the vector result of the matrix-vector product Ax or ATx, 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:     iflag Integer *Input/Output
On entry: if iflag=1, ax must return the m-vector result of the matrix-vector product Ax.
If iflag=2, ax must return the n-vector result of the matrix-vector product ATx.
On exit: may be used as a flag to indicate a failure in the computation of Ax or ATx. If iflag is negative on exit from av, nag_real_partial_svd (f02wgc) will exit immediately with fail 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[dim] const doubleInput
Note: the dimension of the array x will be
  • n when iflag=1;
  • m when iflag=2.
On entry: the vector to be pre-multiplied by the matrix A or AT.
5:     ax[dim] doubleOutput
Note: the dimension of the array ax will be
  • m when iflag=1;
  • n when iflag=2.
On exit: if iflag=1, contains the m-vector result of the matrix-vector product Ax.
If iflag=2, contains the n-vector result of the matrix-vector product ATx.
6:     comm Nag_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.1 in the Essential Introduction).
7:     nconv Integer *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[dim] doubleOutput
Note: the dimension, dim, of the array u must be at least
  • max1,pdu×ncv when order=Nag_ColMajor;
  • max1,m×pdu when order=Nag_RowMajor.
Where Ui,j appears in this document, it refers to the array element
  • u[j-1×pdu+i-1] when order=Nag_ColMajor;
  • u[i-1×pdu+j-1] when order=Nag_RowMajor.
On exit: the left singular vectors corresponding to the singular values stored in sigma.
The ith element of the jth left singular vector uj is stored in Ui,j, for i=1,2,,m and j=1,2,,nconv.
10:   pdu IntegerInput
On entry: the stride used in the array u.
Constraints:
  • if order=Nag_ColMajor, pdumax1,m;
  • if order=Nag_RowMajor, pduncv.
11:   v[dim] doubleOutput
Note: the dimension, dim, of the array v must be at least
  • max1,pdv×ncv when order=Nag_ColMajor;
  • max1,n×pdv when order=Nag_RowMajor.
Where Vi,j appears in this document, it refers to the array element
  • v[j-1×pdv+i-1] when order=Nag_ColMajor;
  • v[i-1×pdv+j-1] when order=Nag_RowMajor.
On exit: the right singular vectors corresponding to the singular values stored in sigma.
The ith element of the jth right singular vector vj is stored in Vi,j, for i=1,2,,n and j=1,2,,nconv.
12:   pdv IntegerInput
On entry: the stride used in the array v.
Constraints:
  • if order=Nag_ColMajor, pdvmax1,n;
  • if order=Nag_RowMajor, pdvncv.
13:   resid[ncv] doubleOutput
On exit: the residual A vj - σj uj , for mn, or AT uj - σj vj , for m<n, for each of the converged singular values σj and corresponding left and right singular vectors uj and vj.
14:   comm Nag_Comm *
The NAG communication argument (see Section 3.2.1.1 in the Essential Introduction).
15:   fail NagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).
nag_real_partial_svd (f02wgc) returns with fail.code= 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.
See Section 3.2.1.2 in the Essential Introduction for further information.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_EIGENVALUES
The number of eigenvalues found to sufficient accuracy is zero.
NE_INT
On entry, k=value.
Constraint: k>0.
On entry, m=value.
Constraint: m0.
On entry, n=value.
Constraint: n0.
On entry, pdu=value.
Constraint: pdu>0.
On entry, pdv=value.
Constraint: pdv>0.
NE_INT_2
On entry, pdu=value and m=value.
Constraint: pdum.
On entry, pdu=value and ncv=value.
Constraint: pduncv.
On entry, pdv=value and n=value.
Constraint: pdvn.
On entry, pdv=value and ncv=value.
Constraint: pdvncv.
NE_INT_3
On entry, m=value, n=value and k=value.
Constraint: 0<k<minm,n-1.
NE_INT_4
On entry, k=value, ncv=value, m=value and n=value.
Constraint: k<ncvminm,n.
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.
An unexpected error has been triggered by this function. Please contact NAG.
See Section 3.6.6 in the Essential Introduction for further information.
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 =value. The number of converged eigenvalues =value.
NE_NO_LANCZOS_FAC
Could not build a full Lanczos factorization.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 3.6.5 in the Essential Introduction for further information.
NE_USER_STOP
On output from user-defined function av, iflag was set to a negative value, iflag=value.

7  Accuracy

See Section 2.14.2 in the f08 Chapter Introduction.

8  Parallelism and Performance

nag_real_partial_svd (f02wgc) is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
nag_real_partial_svd (f02wgc) 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 function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9  Further Comments

None.

10  Example

This example finds the four largest singular values (σ) 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 ks,tdt where
ks,t = st-1 if ​0st 1 ts-1 if ​0t<s1 .  

10.1  Program Text

Program Text (f02wgce.c)

10.2  Program Data

Program Data (f02wgce.d)

10.3  Program Results

Program Results (f02wgce.r)


nag_real_partial_svd (f02wgc) (PDF version)
f02 Chapter Contents
f02 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015