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_dgesvd (f08kb)

## Purpose

nag_lapack_dgesvd (f08kb) computes the singular value decomposition (SVD) of a real m$m$ by n$n$ matrix A$A$, optionally computing the left and/or right singular vectors.

## Syntax

[a, s, u, vt, work, info] = f08kb(jobu, jobvt, a, 'm', m, 'n', n)
[a, s, u, vt, work, info] = nag_lapack_dgesvd(jobu, jobvt, a, 'm', m, 'n', n)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 23: work is now an output parameter
.

## Description

The SVD is written as
 A = UΣVT , $A = UΣVT ,$
where Σ$\Sigma$ is an m$m$ by n$n$ matrix which is zero except for its min (m,n)$\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ diagonal elements, U$U$ is an m$m$ by m$m$ orthogonal matrix, and V$V$ is an n$n$ by n$n$ orthogonal matrix. The diagonal elements of Σ$\Sigma$ are the singular values of A$A$; they are real and non-negative, and are returned in descending order. The first min (m,n)$\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ columns of U$U$ and V$V$ are the left and right singular vectors of A$A$.
Note that the function returns VT${V}^{\mathrm{T}}$, not V$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:     jobu – string (length ≥ 1)
Specifies options for computing all or part of the matrix U$U$.
jobu = 'A'${\mathbf{jobu}}=\text{'A'}$
All m$m$ columns of U$U$ are returned in array u.
jobu = 'S'${\mathbf{jobu}}=\text{'S'}$
The first min (m,n)$\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ columns of U$U$ (the left singular vectors) are returned in the array u.
jobu = 'O'${\mathbf{jobu}}=\text{'O'}$
The first min (m,n)$\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ columns of U$U$ (the left singular vectors) are overwritten on the array a.
jobu = 'N'${\mathbf{jobu}}=\text{'N'}$
No columns of U$U$ (no left singular vectors) are computed.
Constraint: jobu = 'A'${\mathbf{jobu}}=\text{'A'}$, 'S'$\text{'S'}$, 'O'$\text{'O'}$ or 'N'$\text{'N'}$.
2:     jobvt – string (length ≥ 1)
Specifies options for computing all or part of the matrix VT${V}^{\mathrm{T}}$.
jobvt = 'A'${\mathbf{jobvt}}=\text{'A'}$
All n$n$ rows of VT${V}^{\mathrm{T}}$ are returned in the array vt.
jobvt = 'S'${\mathbf{jobvt}}=\text{'S'}$
The first min (m,n)$\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ rows of VT${V}^{\mathrm{T}}$ (the right singular vectors) are returned in the array vt.
jobvt = 'O'${\mathbf{jobvt}}=\text{'O'}$
The first min (m,n)$\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ rows of VT${V}^{\mathrm{T}}$ (the right singular vectors) are overwritten on the array a.
jobvt = 'N'${\mathbf{jobvt}}=\text{'N'}$
No rows of VT${V}^{\mathrm{T}}$ (no right singular vectors) are computed.
Constraints:
• jobvt = 'A'${\mathbf{jobvt}}=\text{'A'}$, 'S'$\text{'S'}$, 'O'$\text{'O'}$ or 'N'$\text{'N'}$;
• jobvt and jobu cannot both be 'O'$\text{'O'}$.
3:     a(lda, : $:$) – double array
The first dimension of the array a must be at least max (1,m)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}\right)$
The second dimension of the array must be at least max (1,n)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$
The m$m$ by n$n$ matrix A$A$.

### Optional Input Parameters

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

### Input Parameters Omitted from the MATLAB Interface

lda ldu ldvt lwork

### Output Parameters

1:     a(lda, : $:$) – double array
The first dimension of the array a will be max (1,m)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}\right)$
The second dimension of the array will be max (1,n)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$
ldamax (1,m)$\mathit{lda}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}\right)$.
If jobu = 'O'${\mathbf{jobu}}=\text{'O'}$, a is overwritten with the first min (m,n)$\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ columns of U$U$ (the left singular vectors, stored column-wise).
If jobvt = 'O'${\mathbf{jobvt}}=\text{'O'}$, a is overwritten with the first min (m,n)$\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ rows of VT${V}^{\mathrm{T}}$ (the right singular vectors, stored row-wise).
If jobu'O'${\mathbf{jobu}}\ne \text{'O'}$ and jobvt'O'${\mathbf{jobvt}}\ne \text{'O'}$, the contents of a are destroyed.
2:     s( : $:$) – double array
Note: the dimension of the array s must be at least max (1,min (m,n)) $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{m}},{\mathbf{n}}\right)\right)$.
The singular values of A$A$, sorted so that s(i)s(i + 1)${\mathbf{s}}\left(i\right)\ge {\mathbf{s}}\left(i+1\right)$.
3:     u(ldu, : $:$) – double array
The first dimension, ldu, of the array u will be
• if jobu = 'A'${\mathbf{jobu}}=\text{'A'}$ or 'S'$\text{'S'}$, ldu max (1,m) $\mathit{ldu}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}\right)$;
• otherwise ldu1$\mathit{ldu}\ge 1$.
The second dimension of the array will be max (1,m)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{m}}\right)$ if jobu = 'A'${\mathbf{jobu}}=\text{'A'}$, max (1,min (m,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 jobu = 'S'${\mathbf{jobu}}=\text{'S'}$, and at least 1$1$ otherwise
If jobu = 'A'${\mathbf{jobu}}=\text{'A'}$, u contains the m$m$ by m$m$ orthogonal matrix U$U$.
If jobu = 'S'${\mathbf{jobu}}=\text{'S'}$, u contains the first min (m,n)$\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ columns of U$U$ (the left singular vectors, stored column-wise).
If jobu = 'N'${\mathbf{jobu}}=\text{'N'}$ or 'O'$\text{'O'}$, u is not referenced.
4:     vt(ldvt, : $:$) – double array
The first dimension, ldvt, of the array vt will be
• if jobvt = 'A'${\mathbf{jobvt}}=\text{'A'}$, ldvt max (1,n) $\mathit{ldvt}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
• if jobvt = 'S'${\mathbf{jobvt}}=\text{'S'}$, ldvt max (1,min (m,n)) $\mathit{ldvt}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{m}},{\mathbf{n}}\right)\right)$;
• otherwise ldvt1$\mathit{ldvt}\ge 1$.
The second dimension of the array will be max (1,n)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$ if jobvt = 'A'${\mathbf{jobvt}}=\text{'A'}$ or 'S'$\text{'S'}$, and at least 1$1$ otherwise
If jobvt = 'A'${\mathbf{jobvt}}=\text{'A'}$, vt contains the n$n$ by n$n$ orthogonal matrix VT${V}^{\mathrm{T}}$.
If jobvt = 'S'${\mathbf{jobvt}}=\text{'S'}$, vt contains the first min (m,n)$\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ rows of VT${V}^{\mathrm{T}}$ (the right singular vectors, stored row-wise).
If jobvt = 'N'${\mathbf{jobvt}}=\text{'N'}$ or 'O'$\text{'O'}$, vt is not referenced.
5:     work(max (1,lwork)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,\mathit{lwork}\right)$) – double array
If ${\mathbf{INFO}}={\mathbf{0}}$, work(1)${\mathbf{work}}\left(1\right)$ returns the optimal lwork.
If ${\mathbf{INFO}}>{\mathbf{0}}$, work(2 : min (m,n))${\mathbf{work}}\left(2:\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{m}},{\mathbf{n}}\right)\right)$ contains the unconverged superdiagonal elements of an upper bidiagonal matrix B$B$ whose diagonal is in s (not necessarily sorted). B$B$ satisfies A = UBVT$A=UB{V}^{\mathrm{T}}$, so it has the same singular values as A$A$, and singular vectors related by U$U$ and VT${V}^{\mathrm{T}}$.
6:     info – int64int32nag_int scalar
info = 0${\mathbf{info}}=0$ unless the function detects an error (see Section [Error Indicators and Warnings]).

## Error Indicators and Warnings

info = i${\mathbf{info}}=-i$
If info = i${\mathbf{info}}=-i$, parameter i$i$ had an illegal value on entry. The parameters are numbered as follows:
1: jobu, 2: jobvt, 3: m, 4: n, 5: a, 6: lda, 7: s, 8: u, 9: ldu, 10: vt, 11: ldvt, 12: work, 13: lwork, 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.
INFO > 0${\mathbf{INFO}}>0$
If nag_lapack_dgesvd (f08kb) did not converge, info specifies how many superdiagonals of an intermediate bidiagonal form did not converge to zero. See the description of work above for details.

## Accuracy

The computed singular value decomposition is nearly the exact singular value decomposition for a nearby matrix (A + E) $\left(A+E\right)$, where
 ‖E‖2 = O(ε) ‖A‖2 , $‖E‖2 = O(ε) ‖A‖2 ,$
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 mn2 $m{n}^{2}$ when m > n$m>n$ and m2n ${m}^{2}n$ otherwise.
The singular values are returned in descending order.
The complex analogue of this function is nag_lapack_zgesvd (f08kp).

## Example

```function nag_lapack_dgesvd_example
jobu = 'Overwrite A by U';
jobvt = 'Singular vectors (V)';
a = [2.27, -1.54, 1.15, -1.94;
0.28, -1.67, 0.94, -0.78;
-0.48, -3.09, 0.99, -0.21;
1.07, 1.22, 0.79, 0.63;
-2.35, 2.93, -1.45, 2.3;
0.62, -7.39, 1.03, -2.57];
[aOut, s, u, vt, work, info] = nag_lapack_dgesvd(jobu, jobvt, a);
aOut, s, u, vt, info
```
```

aOut =

-0.2774   -0.6003   -0.1277    0.1323
-0.2020   -0.0301    0.2805    0.7034
-0.2918    0.3348    0.6453    0.1906
0.0938   -0.3699    0.6781   -0.5399
0.4213    0.5266    0.0413   -0.0575
-0.7816    0.3353   -0.1645   -0.3957

s =

9.9966
3.6831
1.3569
0.5000

u =

0

vt =

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

info =

0

```
```function f08kb_example
jobu = 'Overwrite A by U';
jobvt = 'Singular vectors (V)';
a = [2.27, -1.54, 1.15, -1.94;
0.28, -1.67, 0.94, -0.78;
-0.48, -3.09, 0.99, -0.21;
1.07, 1.22, 0.79, 0.63;
-2.35, 2.93, -1.45, 2.3;
0.62, -7.39, 1.03, -2.57];
[aOut, s, u, vt, work, info] = f08kb(jobu, jobvt, a);
aOut, s, u, vt, info
```
```

aOut =

-0.2774   -0.6003   -0.1277    0.1323
-0.2020   -0.0301    0.2805    0.7034
-0.2918    0.3348    0.6453    0.1906
0.0938   -0.3699    0.6781   -0.5399
0.4213    0.5266    0.0413   -0.0575
-0.7816    0.3353   -0.1645   -0.3957

s =

9.9966
3.6831
1.3569
0.5000

u =

0

vt =

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

info =

0

```