F08 Chapter Contents
F08 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentF08KBF (DGESVD)

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

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

## 2  Specification

 SUBROUTINE F08KBF ( JOBU, JOBVT, M, N, A, LDA, S, U, LDU, VT, LDVT, WORK, LWORK, INFO)
 INTEGER M, N, LDA, LDU, LDVT, LWORK, INFO REAL (KIND=nag_wp) A(LDA,*), S(*), U(LDU,*), VT(LDVT,*), WORK(max(1,LWORK)) CHARACTER(1) JOBU, JOBVT
The routine may be called by its LAPACK name dgesvd.

## 3  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 routine returns ${V}^{\mathrm{T}}$, not $V$.

## 4  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

## 5  Arguments

1:     $\mathrm{JOBU}$ – CHARACTER(1)Input
On entry: specifies options for computing all or part of the matrix $U$.
${\mathbf{JOBU}}=\text{'A'}$
All $m$ columns of $U$ are returned in array U.
${\mathbf{JOBU}}=\text{'S'}$
The first $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ columns of $U$ (the left singular vectors) are returned in the array U.
${\mathbf{JOBU}}=\text{'O'}$
The first $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ columns of $U$ (the left singular vectors) are overwritten on the array A.
${\mathbf{JOBU}}=\text{'N'}$
No columns of $U$ (no left singular vectors) are computed.
Constraint: ${\mathbf{JOBU}}=\text{'A'}$, $\text{'S'}$, $\text{'O'}$ or $\text{'N'}$.
2:     $\mathrm{JOBVT}$ – CHARACTER(1)Input
On entry: specifies options for computing all or part of the matrix ${V}^{\mathrm{T}}$.
${\mathbf{JOBVT}}=\text{'A'}$
All $n$ rows of ${V}^{\mathrm{T}}$ are returned in the array VT.
${\mathbf{JOBVT}}=\text{'S'}$
The first $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ rows of ${V}^{\mathrm{T}}$ (the right singular vectors) are returned in the array VT.
${\mathbf{JOBVT}}=\text{'O'}$
The first $\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left(m,n\right)$ rows of ${V}^{\mathrm{T}}$ (the right singular vectors) are overwritten on the array A.
${\mathbf{JOBVT}}=\text{'N'}$
No rows of ${V}^{\mathrm{T}}$ (no right singular vectors) are computed.
Constraints:
• ${\mathbf{JOBVT}}=\text{'A'}$, $\text{'S'}$, $\text{'O'}$ or $\text{'N'}$;
• JOBVT and JOBU cannot both be $\text{'O'}$.
3:     $\mathrm{M}$ – INTEGERInput
On entry: $m$, the number of rows of the matrix $A$.
Constraint: ${\mathbf{M}}\ge 0$.
4:     $\mathrm{N}$ – INTEGERInput
On entry: $n$, the number of columns of the matrix $A$.
Constraint: ${\mathbf{N}}\ge 0$.
5:     $\mathrm{A}\left({\mathbf{LDA}},*\right)$ – REAL (KIND=nag_wp) arrayInput/Output
Note: the second dimension of the array A must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{N}}\right)$.
On entry: the $m$ by $n$ matrix $A$.
On exit: if ${\mathbf{JOBU}}=\text{'O'}$, A is overwritten with 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{JOBVT}}=\text{'O'}$, A is overwritten with 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{JOBU}}\ne \text{'O'}$ and ${\mathbf{JOBVT}}\ne \text{'O'}$, the contents of A are destroyed.
6:     $\mathrm{LDA}$ – INTEGERInput
On entry: the first dimension of the array A as declared in the (sub)program from which F08KBF (DGESVD) is called.
Constraint: ${\mathbf{LDA}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{M}}\right)$.
7:     $\mathrm{S}\left(*\right)$ – REAL (KIND=nag_wp) arrayOutput
Note: the dimension of the array S must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{M}},{\mathbf{N}}\right)\right)$.
On exit: the singular values of $A$, sorted so that ${\mathbf{S}}\left(i\right)\ge {\mathbf{S}}\left(i+1\right)$.
8:     $\mathrm{U}\left({\mathbf{LDU}},*\right)$ – REAL (KIND=nag_wp) arrayOutput
Note: the second dimension of the array U must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{M}}\right)$ if ${\mathbf{JOBU}}=\text{'A'}$, $\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{JOBU}}=\text{'S'}$, and at least $1$ otherwise.
On exit: if ${\mathbf{JOBU}}=\text{'A'}$, U contains the $m$ by $m$ orthogonal matrix $U$.
If ${\mathbf{JOBU}}=\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{JOBU}}=\text{'N'}$ or $\text{'O'}$, U is not referenced.
9:     $\mathrm{LDU}$ – INTEGERInput
On entry: the first dimension of the array U as declared in the (sub)program from which F08KBF (DGESVD) is called.
Constraints:
• if ${\mathbf{JOBU}}=\text{'A'}$ or $\text{'S'}$, ${\mathbf{LDU}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{M}}\right)$;
• otherwise ${\mathbf{LDU}}\ge 1$.
10:   $\mathrm{VT}\left({\mathbf{LDVT}},*\right)$ – REAL (KIND=nag_wp) arrayOutput
Note: the second dimension of the array VT must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{N}}\right)$ if ${\mathbf{JOBVT}}=\text{'A'}$ or $\text{'S'}$, and at least $1$ otherwise.
On exit: if ${\mathbf{JOBVT}}=\text{'A'}$, VT contains the $n$ by $n$ orthogonal matrix ${V}^{\mathrm{T}}$.
If ${\mathbf{JOBVT}}=\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{JOBVT}}=\text{'N'}$ or $\text{'O'}$, VT is not referenced.
11:   $\mathrm{LDVT}$ – INTEGERInput
On entry: the first dimension of the array VT as declared in the (sub)program from which F08KBF (DGESVD) is called.
Constraints:
• if ${\mathbf{JOBVT}}=\text{'A'}$, ${\mathbf{LDVT}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{N}}\right)$;
• if ${\mathbf{JOBVT}}=\text{'S'}$, ${\mathbf{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 ${\mathbf{LDVT}}\ge 1$.
12:   $\mathrm{WORK}\left(\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{LWORK}}\right)\right)$ – REAL (KIND=nag_wp) arrayWorkspace
On exit: if ${\mathbf{INFO}}={\mathbf{0}}$, ${\mathbf{WORK}}\left(1\right)$ returns the optimal LWORK.
If ${\mathbf{INFO}}>{\mathbf{0}}$, ${\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$ whose diagonal is in S (not necessarily sorted). $B$ satisfies $A=UB{V}^{\mathrm{T}}$, so it has the same singular values as $A$, and singular vectors related by $U$ and ${V}^{\mathrm{T}}$.
13:   $\mathrm{LWORK}$ – INTEGERInput
On entry: the dimension of the array WORK as declared in the (sub)program from which F08KBF (DGESVD) is called.
If ${\mathbf{LWORK}}=-1$, a workspace query is assumed; the routine only calculates the optimal size of the WORK array, returns this value as the first entry of the WORK array, and no error message related to LWORK is issued.
Suggested value: for optimal performance, LWORK should generally be larger. Consider increasing LWORK by at least $\mathit{nb}×\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{M}},{\mathbf{N}}\right)$, where $\mathit{nb}$ is the optimal block size.
Constraint: ${\mathbf{LWORK}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,3×\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{M}},{\mathbf{N}}\right)+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{M}},{\mathbf{N}}\right),5×\mathrm{min}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{M}},{\mathbf{N}}\right)\right)$.
14:   $\mathrm{INFO}$ – INTEGEROutput
On exit: ${\mathbf{INFO}}=0$ unless the routine detects an error (see Section 6).

## 6  Error Indicators and Warnings

${\mathbf{INFO}}<0$
If ${\mathbf{INFO}}=-i$, argument $i$ had an illegal value. An explanatory message is output, and execution of the program is terminated.
${\mathbf{INFO}}>0$
If F08KBF (DGESVD) did not converge, INFO specifies how many superdiagonals of an intermediate bidiagonal form did not converge to zero.

## 7  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.

## 8  Parallelism and Performance

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

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 routine is F08KPF (ZGESVD).

## 10  Example

This example finds the singular values and left and right singular vectors of the $6$ by $4$ matrix
 $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.30 0.62 -7.39 1.03 -2.57 ,$
together with approximate error bounds for the computed singular values and vectors.
The example program for F08KDF (DGESDD) illustrates finding a singular value decomposition for the case $m\le n$.

### 10.1  Program Text

Program Text (f08kbfe.f90)

### 10.2  Program Data

Program Data (f08kbfe.d)

### 10.3  Program Results

Program Results (f08kbfe.r)