Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_lapack_dgesdd (f08kd)

## Purpose

nag_lapack_dgesdd (f08kd) computes the singular value decomposition (SVD) of a real $m$ by $n$ matrix $A$, optionally computing the left and/or right singular vectors, by using a divide-and-conquer method.

## Syntax

[a, s, u, vt, info] = f08kd(jobz, a, 'm', m, 'n', n)
[a, s, u, vt, info] = nag_lapack_dgesdd(jobz, a, 'm', m, 'n', n)

## Description

The SVD is written as
 $A = UΣVT ,$
where $\Sigma$ is an $m$ by $n$ matrix which is zero except for its $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ diagonal elements, $U$ is an $m$ by $m$ orthogonal matrix, and $V$ is an $n$ by $n$ orthogonal matrix. The diagonal elements of $\Sigma$ are the singular values of $A$; they are real and non-negative, and are returned in descending order. 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$.
Note that the function returns ${V}^{\mathrm{T}}$, not $V$.

## References

Anderson E, Bai Z, Bischof C, Blackford S, Demmel J, Dongarra J J, Du Croz J J, Greenbaum A, Hammarling S, McKenney A and Sorensen D (1999) LAPACK Users' Guide (3rd Edition) SIAM, Philadelphia http://www.netlib.org/lapack/lug
Golub G H and Van Loan C F (1996) Matrix Computations (3rd Edition) Johns Hopkins University Press, Baltimore

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{jobz}$ – string (length ≥ 1)
Specifies options for computing all or part of the matrix $U$.
${\mathbf{jobz}}=\text{'A'}$
All $m$ columns of $U$ and all $n$ rows of ${V}^{\mathrm{T}}$ are returned in the arrays u and vt.
${\mathbf{jobz}}=\text{'S'}$
The first $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ columns of $U$ and the first $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ rows of ${V}^{\mathrm{T}}$ are returned in the arrays u and vt.
${\mathbf{jobz}}=\text{'O'}$
If ${\mathbf{m}}\ge {\mathbf{n}}$, the first $n$ columns of $U$ are overwritten on the array a and all rows of ${V}^{\mathrm{T}}$ are returned in the array vt. Otherwise, all columns of $U$ are returned in the array u and the first $m$ rows of ${V}^{\mathrm{T}}$ are overwritten in the array vt.
${\mathbf{jobz}}=\text{'N'}$
No columns of $U$ or rows of ${V}^{\mathrm{T}}$ are computed.
Constraint: ${\mathbf{jobz}}=\text{'A'}$, $\text{'S'}$, $\text{'O'}$ or $\text{'N'}$.
2:     $\mathrm{a}\left(\mathit{lda},:\right)$ – double array
The first dimension of the array a must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}\right)$.
The second dimension of the array a must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
The $m$ by $n$ matrix $A$.

### Optional Input Parameters

1:     $\mathrm{m}$int64int32nag_int scalar
Default: the first dimension of the array a.
$m$, the number of rows of the matrix $A$.
Constraint: ${\mathbf{m}}\ge 0$.
2:     $\mathrm{n}$int64int32nag_int scalar
Default: the second dimension of the array a.
$n$, the number of columns of the matrix $A$.
Constraint: ${\mathbf{n}}\ge 0$.

### Output Parameters

1:     $\mathrm{a}\left(\mathit{lda},:\right)$ – double array
The first dimension of the array a will be $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}\right)$.
The second dimension of the array a will be $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
If ${\mathbf{jobz}}=\text{'O'}$, a is overwritten with the first $n$ columns of $U$ (the left singular vectors, stored column-wise) if ${\mathbf{m}}\ge {\mathbf{n}}$; a is overwritten with the first $m$ rows of ${V}^{\mathrm{T}}$ (the right singular vectors, stored row-wise) otherwise.
If ${\mathbf{jobz}}\ne \text{'O'}$, the contents of a are destroyed.
2:     $\mathrm{s}\left(\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{m}},{\mathbf{n}}\right)\right)$ – double array
The singular values of $A$, sorted so that ${\mathbf{s}}\left(i\right)\ge {\mathbf{s}}\left(i+1\right)$.
3:     $\mathrm{u}\left(\mathit{ldu},:\right)$ – double array
The first dimension, $\mathit{ldu}$, of the array u will be
• if ${\mathbf{jobz}}=\text{'S'}$ or $\text{'A'}$ or ${\mathbf{jobz}}=\text{'O'}$ and ${\mathbf{m}}<{\mathbf{n}}$, $\mathit{ldu}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}\right)$;
• otherwise $\mathit{ldu}=1$.
The second dimension of the array u will be $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}\right)$ if ${\mathbf{jobz}}=\text{'A'}$ or ${\mathbf{jobz}}=\text{'O'}$ and ${\mathbf{m}}<{\mathbf{n}}$, $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{m}},{\mathbf{n}}\right)\right)$ if ${\mathbf{jobz}}=\text{'S'}$ and $1$ otherwise.
If ${\mathbf{jobz}}=\text{'A'}$ or ${\mathbf{jobz}}=\text{'O'}$ and ${\mathbf{m}}<{\mathbf{n}}$, u contains the $m$ by $m$ orthogonal matrix $U$.
If ${\mathbf{jobz}}=\text{'S'}$, u contains the first $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ columns of $U$ (the left singular vectors, stored column-wise).
If ${\mathbf{jobz}}=\text{'O'}$ and ${\mathbf{m}}\ge {\mathbf{n}}$, or ${\mathbf{jobz}}=\text{'N'}$, u is not referenced.
4:     $\mathrm{vt}\left(\mathit{ldvt},:\right)$ – double array
The first dimension, $\mathit{ldvt}$, of the array vt will be
• if ${\mathbf{jobz}}=\text{'A'}$ or ${\mathbf{jobz}}=\text{'O'}$ and ${\mathbf{m}}\ge {\mathbf{n}}$, $\mathit{ldvt}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
• if ${\mathbf{jobz}}=\text{'S'}$, $\mathit{ldvt}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{m}},{\mathbf{n}}\right)\right)$;
• otherwise $\mathit{ldvt}=1$.
The second dimension of the array vt will be $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$ if ${\mathbf{jobz}}=\text{'A'}$ or $\text{'S'}$ or ${\mathbf{jobz}}=\text{'O'}$ and ${\mathbf{m}}\ge {\mathbf{n}}$ and $1$ otherwise.
If ${\mathbf{jobz}}=\text{'A'}$ or ${\mathbf{jobz}}=\text{'O'}$ and ${\mathbf{m}}\ge {\mathbf{n}}$, vt contains the $n$ by $n$ orthogonal matrix ${V}^{\mathrm{T}}$.
If ${\mathbf{jobz}}=\text{'S'}$, vt contains the first $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ rows of ${V}^{\mathrm{T}}$ (the right singular vectors, stored row-wise).
If ${\mathbf{jobz}}=\text{'O'}$ and ${\mathbf{m}}<{\mathbf{n}}$, or ${\mathbf{jobz}}=\text{'N'}$, vt is not referenced.
5:     $\mathrm{info}$int64int32nag_int scalar
${\mathbf{info}}=0$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

${\mathbf{info}}=-i$
If ${\mathbf{info}}=-i$, parameter $i$ had an illegal value on entry. The parameters are numbered as follows:
1: jobz, 2: m, 3: n, 4: a, 5: lda, 6: s, 7: u, 8: ldu, 9: vt, 10: ldvt, 11: work, 12: lwork, 13: iwork, 14: info.
It is possible that info refers to a parameter that is omitted from the MATLAB interface. This usually indicates that an error in one of the other input parameters has caused an incorrect value to be inferred.
${\mathbf{info}}>0$
nag_lapack_dgesdd (f08kd) did not converge, the updating process failed.

## Accuracy

The computed singular value decomposition is nearly the exact singular value decomposition for a nearby matrix $\left(A+E\right)$, where
 $E2 = Oε A2 ,$
and $\epsilon$ is the machine precision. In addition, the computed singular vectors are nearly orthogonal to working precision. See Section 4.9 of Anderson et al. (1999) for further details.

The total number of floating-point operations is approximately proportional to $m{n}^{2}$ when $m>n$ and ${m}^{2}n$ otherwise.
The singular values are returned in descending order.
The complex analogue of this function is nag_lapack_zgesvd (f08kp).

## Example

This example finds the singular values and left and right singular vectors of the $4$ by $6$ matrix
 $A = 2.27 0.28 -0.48 1.07 -2.35 0.62 -1.54 -1.67 -3.09 1.22 2.93 -7.39 1.15 0.94 0.99 0.79 -1.45 1.03 -1.94 -0.78 -0.21 0.63 2.30 -2.57 ,$
together with approximate error bounds for the computed singular values and vectors.
The example program for nag_lapack_dgesvd (f08kb) illustrates finding a singular value decomposition for the case $m\ge n$.
```function f08kd_example

fprintf('f08kd example results\n\n');

a = [ 2.27,  0.28, -0.48, 1.07, -2.35,  0.62;
-1.54, -1.67, -3.09, 1.22,  2.93, -7.39;
1.15,  0.94,  0.99, 0.79, -1.45,  1.03;
-1.94, -0.78, -0.21, 0.63,  2.30, -2.57];
m = int64(size(a,1));
n = int64(size(a,2));

jobz = 'Singular vector parts of U and VT';
[~, s, u, vt, info] = f08kd( ...
jobz, a);

disp('Singular values of A');
disp(s');
disp('Left singular vectors');
disp(u);
disp('Right singular vectors (by row)');
disp(vt);

% Singular values error bound
serrbd = x02aj*s(1);
% Reciprocal condition numbers for singular vectors
[rcondu, info] = f08fl( ...
'Left', m, n, s);
[rcondv, info] = f08fl( ...
'Right', m, n, s);

% Sigular vector error bounds
uerrbd = serrbd./rcondu;
verrbd = serrbd./rcondv;

disp('Error estimate for the singular values');
fprintf('%12.1e\n',serrbd);
disp('Error estimates for the left singular vectors');
fprintf('%12.1e',uerrbd);
fprintf('\n');
disp('Error estimates for the right singular vectors');
fprintf('%12.1e',verrbd);
fprintf('\n');

```
```f08kd example results

Singular values of A
9.9966    3.6831    1.3569    0.5000

Left singular vectors
-0.1921    0.8030   -0.0041    0.5642
0.8794    0.3926    0.0752   -0.2587
-0.2140    0.2980   -0.7827   -0.5027
0.3795   -0.3351   -0.6178    0.6017

Right singular vectors (by row)
-0.2774   -0.2020   -0.2918    0.0938    0.4213   -0.7816
0.6003    0.0301   -0.3348    0.3699   -0.5266   -0.3353
0.1277   -0.2805   -0.6453   -0.6781   -0.0413    0.1645
-0.1323   -0.7034   -0.1906    0.5399    0.0575    0.3957

Error estimate for the singular values
1.1e-15
Error estimates for the left singular vectors
1.8e-16     4.8e-16     1.3e-15     1.3e-15
Error estimates for the right singular vectors
1.8e-16     4.8e-16     1.3e-15     2.2e-15
```