# NAG Library Function Document

## 1Purpose

nag_dbdsqr (f08mec) computes the singular value decomposition of a real upper or lower bidiagonal matrix, or of a real general matrix which has been reduced to bidiagonal form.

## 2Specification

 #include #include
 void nag_dbdsqr (Nag_OrderType order, Nag_UploType uplo, Integer n, Integer ncvt, Integer nru, Integer ncc, double d[], double e[], double vt[], Integer pdvt, double u[], Integer pdu, double c[], Integer pdc, NagError *fail)

## 3Description

nag_dbdsqr (f08mec) computes the singular values and, optionally, the left or right singular vectors of a real upper or lower bidiagonal matrix $B$. In other words, it can compute the singular value decomposition (SVD) of $B$ as
 $B = U Σ VT .$
Here $\Sigma$ is a diagonal matrix with real diagonal elements ${\sigma }_{i}$ (the singular values of $B$), such that
 $σ1 ≥ σ2 ≥ ⋯ ≥ σn ≥ 0 ;$
$U$ is an orthogonal matrix whose columns are the left singular vectors ${u}_{i}$; $V$ is an orthogonal matrix whose rows are the right singular vectors ${v}_{i}$. Thus
 $Bui = σi vi and BT vi = σi ui , i = 1,2,…,n .$
To compute $U$ and/or ${V}^{\mathrm{T}}$, the arrays u and/or vt must be initialized to the unit matrix before nag_dbdsqr (f08mec) is called.
The function may also be used to compute the SVD of a real general matrix $A$ which has been reduced to bidiagonal form by an orthogonal transformation: $A=QB{P}^{\mathrm{T}}$. If $A$ is $m$ by $n$ with $m\ge n$, then $Q$ is $m$ by $n$ and ${P}^{\mathrm{T}}$ is $n$ by $n$; if $A$ is $n$ by $p$ with $n, then $Q$ is $n$ by $n$ and ${P}^{\mathrm{T}}$ is $n$ by $p$. In this case, the matrices $Q$ and/or ${P}^{\mathrm{T}}$ must be formed explicitly by nag_dorgbr (f08kfc) and passed to nag_dbdsqr (f08mec) in the arrays u and/or vt respectively.
nag_dbdsqr (f08mec) also has the capability of forming ${U}^{\mathrm{T}}C$, where $C$ is an arbitrary real matrix; this is needed when using the SVD to solve linear least squares problems.
nag_dbdsqr (f08mec) uses two different algorithms. If any singular vectors are required (i.e., if ${\mathbf{ncvt}}>0$ or ${\mathbf{nru}}>0$ or ${\mathbf{ncc}}>0$), the bidiagonal $QR$ algorithm is used, switching between zero-shift and implicitly shifted forms to preserve the accuracy of small singular values, and switching between $QR$ and $QL$ variants in order to handle graded matrices effectively (see Demmel and Kahan (1990)). If only singular values are required (i.e., if ${\mathbf{ncvt}}={\mathbf{nru}}={\mathbf{ncc}}=0$), they are computed by the differential qd algorithm (see Fernando and Parlett (1994)), which is faster and can achieve even greater accuracy.
The singular vectors are normalized so that $‖{u}_{i}‖=‖{v}_{i}‖=1$, but are determined only to within a factor $±1$.

## 4References

Demmel J W and Kahan W (1990) Accurate singular values of bidiagonal matrices SIAM J. Sci. Statist. Comput. 11 873–912
Fernando K V and Parlett B N (1994) Accurate singular values and differential qd algorithms Numer. Math. 67 191–229
Golub G H and Van Loan C F (1996) Matrix Computations (3rd Edition) Johns Hopkins University Press, Baltimore

## 5Arguments

1:    $\mathbf{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 ${\mathbf{order}}=\mathrm{Nag_RowMajor}$. See Section 3.3.1.3 in How to Use the NAG Library and its Documentation for a more detailed explanation of the use of this argument.
Constraint: ${\mathbf{order}}=\mathrm{Nag_RowMajor}$ or $\mathrm{Nag_ColMajor}$.
2:    $\mathbf{uplo}$Nag_UploTypeInput
On entry: indicates whether $B$ is an upper or lower bidiagonal matrix.
${\mathbf{uplo}}=\mathrm{Nag_Upper}$
$B$ is an upper bidiagonal matrix.
${\mathbf{uplo}}=\mathrm{Nag_Lower}$
$B$ is a lower bidiagonal matrix.
Constraint: ${\mathbf{uplo}}=\mathrm{Nag_Upper}$ or $\mathrm{Nag_Lower}$.
3:    $\mathbf{n}$IntegerInput
On entry: $n$, the order of the matrix $B$.
Constraint: ${\mathbf{n}}\ge 0$.
4:    $\mathbf{ncvt}$IntegerInput
On entry: $\mathit{ncvt}$, the number of columns of the matrix ${V}^{\mathrm{T}}$ of right singular vectors. Set ${\mathbf{ncvt}}=0$ if no right singular vectors are required.
Constraint: ${\mathbf{ncvt}}\ge 0$.
5:    $\mathbf{nru}$IntegerInput
On entry: $\mathit{nru}$, the number of rows of the matrix $U$ of left singular vectors. Set ${\mathbf{nru}}=0$ if no left singular vectors are required.
Constraint: ${\mathbf{nru}}\ge 0$.
6:    $\mathbf{ncc}$IntegerInput
On entry: $\mathit{ncc}$, the number of columns of the matrix $C$. Set ${\mathbf{ncc}}=0$ if no matrix $C$ is supplied.
Constraint: ${\mathbf{ncc}}\ge 0$.
7:    $\mathbf{d}\left[\mathit{dim}\right]$doubleInput/Output
Note: the dimension, dim, of the array d must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
On entry: the diagonal elements of the bidiagonal matrix $B$.
On exit: the singular values in decreasing order of magnitude, unless ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_CONVERGENCE (in which case see Section 6).
8:    $\mathbf{e}\left[\mathit{dim}\right]$doubleInput/Output
Note: the dimension, dim, of the array e must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}-1\right)$.
On entry: the off-diagonal elements of the bidiagonal matrix $B$.
On exit: e is overwritten, but if ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_CONVERGENCE see Section 6.
9:    $\mathbf{vt}\left[\mathit{dim}\right]$doubleInput/Output
Note: the dimension, dim, of the array vt must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pdvt}}×{\mathbf{ncvt}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$ and at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pdvt}}×{\mathbf{n}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
The $\left(i,j\right)$th element of the matrix is stored in
• ${\mathbf{vt}}\left[\left(j-1\right)×{\mathbf{pdvt}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{vt}}\left[\left(i-1\right)×{\mathbf{pdvt}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On entry: if ${\mathbf{ncvt}}>0$, vt must contain an $n$ by $\mathit{ncvt}$ matrix. If the right singular vectors of $B$ are required, $\mathit{ncvt}=n$ and vt must contain the unit matrix; if the right singular vectors of $A$ are required, vt must contain the orthogonal matrix ${P}^{\mathrm{T}}$ returned by nag_dorgbr (f08kfc) with ${\mathbf{vect}}=\mathrm{Nag_FormP}$.
On exit: the $n$ by $\mathit{ncvt}$ matrix ${V}^{\mathrm{T}}$ or ${V}^{\mathrm{T}}{P}^{\mathrm{T}}$ of right singular vectors, stored by rows.
If ${\mathbf{ncvt}}=0$, vt is not referenced.
10:  $\mathbf{pdvt}$IntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array vt.
Constraints:
• if ${\mathbf{order}}=\mathrm{Nag_ColMajor}$,
• if ${\mathbf{ncvt}}>0$, ${\mathbf{pdvt}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
• otherwise ${\mathbf{pdvt}}\ge 1$;
• if ${\mathbf{order}}=\mathrm{Nag_RowMajor}$,
• if ${\mathbf{ncvt}}>0$, ${\mathbf{pdvt}}\ge {\mathbf{ncvt}}$;
• otherwise ${\mathbf{pdvt}}\ge 1$.
11:  $\mathbf{u}\left[\mathit{dim}\right]$doubleInput/Output
Note: the dimension, dim, of the array u must be at least
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pdu}}×{\mathbf{n}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nru}}×{\mathbf{pdu}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
The $\left(i,j\right)$th element of the matrix $U$ is stored in
• ${\mathbf{u}}\left[\left(j-1\right)×{\mathbf{pdu}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{u}}\left[\left(i-1\right)×{\mathbf{pdu}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On entry: if ${\mathbf{nru}}>0$, u must contain an $\mathit{nru}$ by $n$ matrix. If the left singular vectors of $B$ are required, $\mathit{nru}=n$ and u must contain the unit matrix; if the left singular vectors of $A$ are required, u must contain the orthogonal matrix $Q$ returned by nag_dorgbr (f08kfc) with ${\mathbf{vect}}=\mathrm{Nag_FormQ}$.
On exit: the $\mathit{nru}$ by $n$ matrix $U$ or $QU$ of left singular vectors, stored as columns of the matrix.
If ${\mathbf{nru}}=0$, u is not referenced.
12:  $\mathbf{pdu}$IntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array u.
Constraints:
• if ${\mathbf{order}}=\mathrm{Nag_ColMajor}$, ${\mathbf{pdu}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nru}}\right)$;
• if ${\mathbf{order}}=\mathrm{Nag_RowMajor}$, ${\mathbf{pdu}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
13:  $\mathbf{c}\left[\mathit{dim}\right]$doubleInput/Output
Note: the dimension, dim, of the array c must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pdc}}×{\mathbf{ncc}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$ and at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pdc}}×{\mathbf{n}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
The $\left(i,j\right)$th element of the matrix $C$ is stored in
• ${\mathbf{c}}\left[\left(j-1\right)×{\mathbf{pdc}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{c}}\left[\left(i-1\right)×{\mathbf{pdc}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On entry: the $n$ by $\mathit{ncc}$ matrix $C$ if ${\mathbf{ncc}}>0$.
On exit: c is overwritten by the matrix ${U}^{\mathrm{T}}C$. If ${\mathbf{ncc}}=0$, c is not referenced.
14:  $\mathbf{pdc}$IntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array c.
Constraints:
• if ${\mathbf{order}}=\mathrm{Nag_ColMajor}$,
• if ${\mathbf{ncc}}>0$, ${\mathbf{pdc}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
• otherwise ${\mathbf{pdc}}\ge 1$;
• if ${\mathbf{order}}=\mathrm{Nag_RowMajor}$, ${\mathbf{pdc}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{ncc}}\right)$.
15:  $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_CONVERGENCE
$〈\mathit{\text{value}}〉$ off-diagonals did not converge. The arrays d and e contain the diagonal and off-diagonal elements, respectively, of a bidiagonal matrix orthogonally equivalent to $B$.
NE_INT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 0$.
On entry, ${\mathbf{ncc}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ncc}}\ge 0$.
On entry, ${\mathbf{ncvt}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ncvt}}>0$.
On entry, ${\mathbf{ncvt}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ncvt}}\ge 0$.
On entry, ${\mathbf{nru}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nru}}\ge 0$.
On entry, ${\mathbf{pdc}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdc}}>0$.
On entry, ${\mathbf{pdu}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdu}}>0$.
On entry, ${\mathbf{pdvt}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdvt}}>0$.
NE_INT_2
On entry, ${\mathbf{pdc}}=〈\mathit{\text{value}}〉$ and ${\mathbf{ncc}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdc}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{ncc}}\right)$.
On entry, ${\mathbf{pdu}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdu}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
On entry, ${\mathbf{pdu}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nru}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdu}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nru}}\right)$.
On entry, ${\mathbf{pdvt}}=〈\mathit{\text{value}}〉$ and ${\mathbf{ncvt}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{ncvt}}>0$, ${\mathbf{pdvt}}\ge {\mathbf{ncvt}}$;
otherwise ${\mathbf{pdvt}}\ge 1$.
NE_INT_3
On entry, ${\mathbf{ncc}}=〈\mathit{\text{value}}〉$, ${\mathbf{pdc}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{ncc}}>0$, ${\mathbf{pdc}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
otherwise ${\mathbf{pdc}}\ge 1$.
On entry, ${\mathbf{pdvt}}=〈\mathit{\text{value}}〉$, ${\mathbf{ncvt}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{ncvt}}>0$, ${\mathbf{pdvt}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
otherwise ${\mathbf{pdvt}}\ge 1$.
NE_INTERNAL_ERROR
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.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

Each singular value and singular vector is computed to high relative accuracy. However, the reduction to bidiagonal form (prior to calling the function) may exclude the possibility of obtaining high relative accuracy in the small singular values of the original matrix if its singular values vary widely in magnitude.
If ${\sigma }_{i}$ is an exact singular value of $B$ and ${\stackrel{~}{\sigma }}_{i}$ is the corresponding computed value, then
 $σ~i - σi ≤ p m,n ε σi$
where $p\left(m,n\right)$ is a modestly increasing function of $m$ and $n$, and $\epsilon$ is the machine precision. If only singular values are computed, they are computed more accurately (i.e., the function $p\left(m,n\right)$ is smaller), than when some singular vectors are also computed.
If ${u}_{i}$ is the corresponding exact left singular vector of $B$, and ${\stackrel{~}{u}}_{i}$ is the corresponding computed left singular vector, then the angle $\theta \left({\stackrel{~}{u}}_{i},{u}_{i}\right)$ between them is bounded as follows:
 $θ u~i,ui ≤ p m,n ε relgapi$
where ${\mathit{relgap}}_{i}$ is the relative gap between ${\sigma }_{i}$ and the other singular values, defined by
 $relgapi = min i≠j σi - σj σi + σj .$
A similar error bound holds for the right singular vectors.

## 8Parallelism and Performance

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

The total number of floating-point operations is roughly proportional to ${n}^{2}$ if only the singular values are computed. About $6{n}^{2}×\mathit{nru}$ additional operations are required to compute the left singular vectors and about $6{n}^{2}×\mathit{ncvt}$ to compute the right singular vectors. The operations to compute the singular values must all be performed in scalar mode; the additional operations to compute the singular vectors can be vectorized and on some machines may be performed much faster.
The complex analogue of this function is nag_zbdsqr (f08msc).

## 10Example

This example computes the singular value decomposition of the upper bidiagonal matrix $B$, where
 $B = 3.62 1.26 0.00 0.00 0.00 -2.41 -1.53 0.00 0.00 0.00 1.92 1.19 0.00 0.00 0.00 -1.43 .$
See also the example for nag_dorgbr (f08kfc), which illustrates the use of the function to compute the singular value decomposition of a general matrix.

### 10.1Program Text

Program Text (f08mece.c)

### 10.2Program Data

Program Data (f08mece.d)

### 10.3Program Results

Program Results (f08mece.r)

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