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_zbdsqr (f08ms)

## Purpose

nag_lapack_zbdsqr (f08ms) computes the singular value decomposition of a complex general matrix which has been reduced to bidiagonal form.

## Syntax

[d, e, vt, u, c, info] = f08ms(uplo, d, e, vt, u, c, 'n', n, 'ncvt', ncvt, 'nru', nru, 'ncc', ncc)
[d, e, vt, u, c, info] = nag_lapack_zbdsqr(uplo, d, e, vt, u, c, 'n', n, 'ncvt', ncvt, 'nru', nru, 'ncc', ncc)

## Description

nag_lapack_zbdsqr (f08ms) 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_lapack_zbdsqr (f08ms) is called.
The function stores the real orthogonal matrices $U$ and ${V}^{\mathrm{T}}$ in complex arrays u and vt, so that it may also be used to compute the SVD of a complex general matrix $A$ which has been reduced to bidiagonal form by a unitary transformation: $A=QB{P}^{\mathrm{H}}$. If $A$ is $m$ by $n$ with $m\ge n$, then $Q$ is $m$ by $n$ and ${P}^{\mathrm{H}}$ is $n$ by $n$; if $A$ is $n$ by $p$ with $n, then $Q$ is $n$ by $n$ and ${P}^{\mathrm{H}}$ is $n$ by $p$. In this case, the matrices $Q$ and/or ${P}^{\mathrm{H}}$ must be formed explicitly by nag_lapack_zungbr (f08kt) and passed to nag_lapack_zbdsqr (f08ms) in the arrays u and/or vt respectively.
nag_lapack_zbdsqr (f08ms) also has the capability of forming ${U}^{\mathrm{H}}C$, where $C$ is an arbitrary complex matrix; this is needed when using the SVD to solve linear least squares problems.
nag_lapack_zbdsqr (f08ms) 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 complex factor of absolute value $1$.

## References

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{uplo}$ – string (length ≥ 1)
Indicates whether $B$ is an upper or lower bidiagonal matrix.
${\mathbf{uplo}}=\text{'U'}$
$B$ is an upper bidiagonal matrix.
${\mathbf{uplo}}=\text{'L'}$
$B$ is a lower bidiagonal matrix.
Constraint: ${\mathbf{uplo}}=\text{'U'}$ or $\text{'L'}$.
2:     $\mathrm{d}\left(:\right)$ – double array
The dimension of the array d must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$
The diagonal elements of the bidiagonal matrix $B$.
3:     $\mathrm{e}\left(:\right)$ – double array
The dimension of the array e must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}-1\right)$
The off-diagonal elements of the bidiagonal matrix $B$.
4:     $\mathrm{vt}\left(\mathit{ldvt},:\right)$ – complex array
The first dimension, $\mathit{ldvt}$, of the array vt must satisfy
• if ${\mathbf{ncvt}}>0$, $\mathit{ldvt}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
• otherwise $\mathit{ldvt}\ge 1$.
The second dimension of the array vt must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{ncvt}}\right)$.
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 unitary matrix ${P}^{\mathrm{H}}$ returned by nag_lapack_zungbr (f08kt) with ${\mathbf{vect}}=\text{'P'}$.
5:     $\mathrm{u}\left(\mathit{ldu},:\right)$ – complex array
The first dimension of the array u must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nru}}\right)$.
The second dimension of the array u must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
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 unitary matrix $Q$ returned by nag_lapack_zungbr (f08kt) with ${\mathbf{vect}}=\text{'Q'}$.
6:     $\mathrm{c}\left(\mathit{ldc},:\right)$ – complex array
The first dimension, $\mathit{ldc}$, of the array c must satisfy
• if ${\mathbf{ncc}}>0$, $\mathit{ldc}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
• otherwise $\mathit{ldc}\ge 1$.
The second dimension of the array c must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{ncc}}\right)$.
The $n$ by $\mathit{ncc}$ matrix $C$ if ${\mathbf{ncc}}>0$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the arrays d, u.
$n$, the order of the matrix $B$.
Constraint: ${\mathbf{n}}\ge 0$.
2:     $\mathrm{ncvt}$int64int32nag_int scalar
Default: the second dimension of the array vt.
$\mathit{ncvt}$, the number of columns of the matrix ${V}^{\mathrm{H}}$ of right singular vectors. Set ${\mathbf{ncvt}}=0$ of right singular vectors. Set ${\mathbf{ncvt}}=0$ if no right singular vectors are required.
Constraint: ${\mathbf{ncvt}}\ge 0$.
3:     $\mathrm{nru}$int64int32nag_int scalar
Default: the first dimension of the array u.
$\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$.
4:     $\mathrm{ncc}$int64int32nag_int scalar
Default: the second dimension of the array c.
$\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$.

### Output Parameters

1:     $\mathrm{d}\left(:\right)$ – double array
The dimension of the array d will be $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$
The singular values in decreasing order of magnitude, unless ${\mathbf{info}}>{\mathbf{0}}$ (in which case see Error Indicators and Warnings).
2:     $\mathrm{e}\left(:\right)$ – double array
The dimension of the array e will be $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}-1\right)$
e is overwritten, but if ${\mathbf{info}}>{\mathbf{0}}$ see Error Indicators and Warnings.
3:     $\mathrm{vt}\left(\mathit{ldvt},:\right)$ – complex array
The first dimension, $\mathit{ldvt}$, of the array vt will be
• if ${\mathbf{ncvt}}>0$, $\mathit{ldvt}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
• otherwise $\mathit{ldvt}=1$.
The second dimension of the array vt will be $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{ncvt}}\right)$.
The $n$ by $\mathit{ncvt}$ matrix ${V}^{\mathrm{H}}$ or ${V}^{\mathrm{H}}$ of right singular vectors, stored by rows.
If ${\mathbf{ncvt}}=0$, vt is not referenced.
4:     $\mathrm{u}\left(\mathit{ldu},:\right)$ – complex array
The first dimension of the array u will be $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nru}}\right)$.
The second dimension of the array u will be $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
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.
5:     $\mathrm{c}\left(\mathit{ldc},:\right)$ – complex array
The first dimension, $\mathit{ldc}$, of the array c will be
• if ${\mathbf{ncc}}>0$, $\mathit{ldc}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
• otherwise $\mathit{ldc}=1$.
The second dimension of the array c will be $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{ncc}}\right)$.
c stores the matrix ${U}^{\mathrm{H}}C$. If ${\mathbf{ncc}}=0$, c is not referenced.
6:     $\mathrm{info}$int64int32nag_int scalar
${\mathbf{info}}=0$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

${\mathbf{info}}=-i$
If ${\mathbf{info}}=-i$, parameter $i$ had an illegal value on entry. The parameters are numbered as follows:
1: uplo, 2: n, 3: ncvt, 4: nru, 5: ncc, 6: d, 7: e, 8: vt, 9: ldvt, 10: u, 11: ldu, 12: c, 13: ldc, 14: work, 15: 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.
W  ${\mathbf{info}}>0$
The algorithm failed to converge and info specifies how many off-diagonals did not converge. In this case, d and e contain on exit the diagonal and off-diagonal elements, respectively, of a bidiagonal matrix orthogonally equivalent to $B$.

## Accuracy

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

The total number of real floating-point operations is roughly proportional to ${n}^{2}$ if only the singular values are computed. About $12{n}^{2}×\mathit{nru}$ additional operations are required to compute the left singular vectors and about $12{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 real analogue of this function is nag_lapack_dbdsqr (f08me).

## Example

See Example in nag_lapack_zungbr (f08kt), which illustrates the use of the function to compute the singular value decomposition of a general matrix.
```function f08ms_example

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

m = int64(6);
n = int64(4);
a = [ 0.96 - 0.81i, -0.03 + 0.96i, -0.91 + 2.06i, -0.05 + 0.41i;
-0.98 + 1.98i, -1.20 + 0.19i, -0.66 + 0.42i, -0.81 + 0.56i;
0.62 - 0.46i,  1.01 + 0.02i,  0.63 - 0.17i, -1.11 + 0.60i;
-0.37 + 0.38i,  0.19 - 0.54i, -0.98 - 0.36i,  0.22 - 0.20i;
0.83 + 0.51i,  0.20 + 0.01i, -0.17 - 0.46i,  1.47 + 1.59i;
1.08 - 0.28i,  0.20 - 0.12i, -0.07 + 1.23i,  0.26 + 0.26i];

% Factorize A = QR
[QR, tau, info] = f08as(a);

% Generate Q from QR
[Q, info] = f08at(QR, tau);

% Extract R from QR
R = triu(QR(1:n,1:n));

% Bidiagonalize R = Q1 B P^H
[B, d, e, tauq, taup, info] = f08ks(R);

% Form P^H explicitly
vect = 'P';
[PH, info] = f08kt(vect, m, B, taup);
% Form Q1 explicitly
vect = 'Q';
[Q1, info] = f08kt(vect, n, B, tauq);

% Update Q: Q2 = Q*Q1 (so A = QR = Q2 B PH)
vect = 'Q';
side = 'Right';
trans = 'No transpose';
[Q2, info] = f08ku(vect, side, trans, n, B, tauq, Q);

% Compute SVD of A from bidiagonal form
uplo = 'Upper';
c = [];
[S, ~, VH, U, ~, info] = f08ms(uplo, d, e, PH, Q2, c);

disp('Singular values:');
disp(S);
disp('Left Singular Vectors:');
disp(U);
disp('Right Singular Vectors:');
disp(VH');

```
```f08ms example results

Singular values:
3.9994
3.0003
1.9944
0.9995

Left Singular Vectors:
-0.5634 + 0.0016i   0.2687 + 0.2749i  -0.2451 - 0.4657i  -0.3787 - 0.2987i
0.1205 - 0.6108i   0.2909 - 0.1085i  -0.4329 + 0.1758i   0.0182 + 0.0437i
-0.0816 + 0.1613i   0.1660 - 0.3885i   0.4667 - 0.3821i   0.0800 + 0.2276i
0.1441 - 0.1532i  -0.1984 + 0.1737i   0.0034 - 0.1555i  -0.2608 + 0.5382i
-0.2487 - 0.0926i  -0.6253 - 0.3304i  -0.2643 + 0.0194i  -0.1002 - 0.0140i
-0.3758 + 0.0793i   0.0307 + 0.0816i  -0.1266 - 0.1747i   0.4175 + 0.4058i

Right Singular Vectors:
-0.6971 + 0.0000i  -0.2403 + 0.0000i   0.5123 + 0.0000i   0.4403 + 0.0000i
-0.0867 + 0.3548i  -0.0725 - 0.2336i   0.3030 - 0.1735i  -0.5294 + 0.6361i
0.0560 + 0.5400i   0.2477 - 0.5291i  -0.0678 + 0.5162i   0.3027 - 0.0346i
-0.1878 + 0.2253i  -0.7026 + 0.2177i  -0.4418 + 0.3864i  -0.1667 + 0.0258i

```