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_dgejsv (f08kh)

## Purpose

nag_lapack_dgejsv (f08kh) computes the singular value decomposition (SVD) of a real m$m$ by n$n$ matrix A$A$ where mn$m\ge n$, and optionally computes the left and/or right singular vectors. nag_lapack_dgejsv (f08kh) implements the preconditioned Jacobi SVD of Drmac and Veselic. This is the expert driver function that calls nag_lapack_dgesvj (f08kj) after certain preconditioning. In most cases nag_lapack_dgesvd (f08kb) or nag_lapack_dgesdd (f08kd) is sufficient to obtain the SVD of a real matrix. These are much simpler to use and also handle the case m < n$m.

## Syntax

[a, sva, u, v, work, iwork, info] = f08kh(joba, jobu, jobv, jobr, jobt, jobp, a, 'm', m, 'n', n)
[a, sva, u, v, work, iwork, info] = nag_lapack_dgejsv(joba, jobu, jobv, jobr, jobt, jobp, a, 'm', m, 'n', n)

## 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 n$n$ 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$ in descending order of magnitude. The columns of U$U$ and V$V$ are the left and the right singular vectors of A$A$. The diagonal of Σ$\Sigma$ is computed and stored in the array sva.

## 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
Drmac Z and Veselic K (2008a) New fast and accurate Jacobi SVD algorithm I SIAM J. Matrix Anal. Appl. 29 4
Drmac Z and Veselic K (2008b) New fast and accurate Jacobi SVD algorithm II SIAM J. Matrix Anal. Appl. 29 4
Golub G H and Van Loan C F (1996) Matrix Computations (3rd Edition) Johns Hopkins University Press, Baltimore

## Parameters

### Compulsory Input Parameters

1:     joba – string (length ≥ 1)
Specifies the form of pivoting for the QR$QR$ factorization stage; whether an estimate of the condition number of the scaled matrix is required; and the form of rank reduction that is performed.
joba = 'C'${\mathbf{joba}}=\text{'C'}$
The initial QR$QR$ factorization of the input matrix is performed with column pivoting; no estimate of condition number is computed; and, the rank is reduced by only the underflowed part of the triangular factor R$R$. This option works well (high relative accuracy) if A = BD$A=BD$, with well-conditioned B$B$ and arbitrary diagonal matrix D$D$. The accuracy cannot be spoiled by column scaling. The accuracy of the computed output depends on the condition of B$B$, and the procedure aims at the best theoretical accuracy.
joba = 'E'${\mathbf{joba}}=\text{'E'}$
Computation as with joba = 'C'${\mathbf{joba}}=\text{'C'}$ with an additional estimate of the condition number of B$B$. It provides a realistic error bound.
joba = 'F'${\mathbf{joba}}=\text{'F'}$
The initial QR$QR$ factorization of the input matrix is performed with full row and column pivoting; no estimate of condition number is computed; and, the rank is reduced by only the underflowed part of the triangular factor R$R$. If A = D1 × C × D2$A={D}_{1}×C×{D}_{2}$ with ill-conditioned diagonal scalings D1${D}_{1}$, D2${D}_{2}$, and well-conditioned matrix C$C$, this option gives higher accuracy than the joba = 'C'${\mathbf{joba}}=\text{'C'}$ option. If the structure of the input matrix is not known, and relative accuracy is desirable, then this option is advisable.
joba = 'G'${\mathbf{joba}}=\text{'G'}$
Computation as with joba = 'F'${\mathbf{joba}}=\text{'F'}$ with an additional estimate of the condition number of B$B$, where A = DB$A=DB$ (i.e., B = C × D2$B=C×{D}_{2}$). If A$A$ has heavily weighted rows, then using this condition number gives too pessimistic an error bound.
joba = 'A'${\mathbf{joba}}=\text{'A'}$
Computation as with joba = 'C'${\mathbf{joba}}=\text{'C'}$ except in the treatment of rank reduction. In this case, small singular values are to be considered as noise and, if found, the matrix is treated as numerically rank deficient. The computed SVD A = UΣVT$A=U\Sigma {V}^{\mathrm{T}}$ restores A$A$ up to f(m,n) × ε × A$f\left(m,n\right)×\epsilon ×‖A‖$, where ε$\epsilon$ is machine precision. This gives the procedure licence to discard (set to zero) all singular values below n × ε × A${\mathbf{n}}×\epsilon ×‖A‖$.
joba = 'R'${\mathbf{joba}}=\text{'R'}$
Similar to joba = 'A'${\mathbf{joba}}=\text{'A'}$. The rank revealing property of the initial QR$QR$ factorization is used to reveal (using the upper triangular factor) a gap σr + 1 < εσr${\sigma }_{r+1}<\epsilon {\sigma }_{r}$ in which case the numerical rank is declared to be r$r$. The SVD is computed with absolute error bounds, but more accurately than with joba = 'A'${\mathbf{joba}}=\text{'A'}$.
Constraint: joba = 'C'${\mathbf{joba}}=\text{'C'}$, 'E'$\text{'E'}$, 'F'$\text{'F'}$, 'G'$\text{'G'}$, 'A'$\text{'A'}$ or 'R'$\text{'R'}$.
2:     jobu – string (length ≥ 1)
Specifies options for computing the left singular vectors U$U$.
jobu = 'U'${\mathbf{jobu}}=\text{'U'}$
The first n$n$ left singular vectors (columns of U$U$) are computed and returned in the array u.
jobu = 'F'${\mathbf{jobu}}=\text{'F'}$
All m$m$ left singular vectors are computed and returned in the array u.
jobu = 'W'${\mathbf{jobu}}=\text{'W'}$
No left singular vectors are computed, but the array u (with ldum$\mathit{ldu}\ge {\mathbf{m}}$ and second dimension at least n) is available as workspace for computing right singular values. See the description of u.
jobu = 'N'${\mathbf{jobu}}=\text{'N'}$
No left singular vectors are computed. u${\mathbf{u}}$ is not referenced.
Constraint: jobu = 'U'${\mathbf{jobu}}=\text{'U'}$, 'F'$\text{'F'}$, 'W'$\text{'W'}$ or 'N'$\text{'N'}$.
3:     jobv – string (length ≥ 1)
Specifies options for computing the right singular vectors V$V$.
jobv = 'V'${\mathbf{jobv}}=\text{'V'}$
the n$n$ right singular vectors (columns of V$V$) are computed and returned in the array v; Jacobi rotations are not explicitly accumulated.
jobv = 'J'${\mathbf{jobv}}=\text{'J'}$
the n$n$ right singular vectors (columns of V$V$) are computed and returned in the array v, but they are computed as the product of Jacobi rotations. This option is allowed only if jobu = 'U'${\mathbf{jobu}}=\text{'U'}$ or 'F'$\text{'F'}$, i.e., in computing the full SVD.
jobv = 'W'${\mathbf{jobv}}=\text{'W'}$
No right singular values are computed, but the array v (with ldvn$\mathit{ldv}\ge {\mathbf{n}}$ and second dimension at least n) is available as workspace for computing left singular values. See the description of v.
jobv = 'N'${\mathbf{jobv}}=\text{'N'}$
No right singular vectors are computed. v${\mathbf{v}}$ is not referenced.
Constraints:
• jobv = 'V'${\mathbf{jobv}}=\text{'V'}$, 'J'$\text{'J'}$, 'W'$\text{'W'}$ or 'N'$\text{'N'}$;
• if jobu = 'W'${\mathbf{jobu}}=\text{'W'}$ or 'N'$\text{'N'}$, jobv'J'${\mathbf{jobv}}\ne \text{'J'}$.
4:     jobr – string (length ≥ 1)
Specifies the conditions under which columns of A$A$ are to be set to zero. This effectively specifies a lower limit on the range of singular values; any singular values below this limit are (through column zeroing) set to zero. If A0$A\ne 0$ is scaled so that the largest column (in the Euclidean norm) of cA$cA$ is equal to the square root of the overflow threshold, then jobr allows the function to kill columns of A$A$ whose norm in cA$cA$ is less than sqrt(sfmin)$\sqrt{\mathit{sfmin}}$ (for jobr = 'R'${\mathbf{jobr}}=\text{'R'}$), or less than sfmin / ε$\mathit{sfmin}/\epsilon$ (otherwise). sfmin$\mathit{sfmin}$ is the safe range parameter, as returned by function nag_machine_real_safe (x02am).
jobr = 'N'${\mathbf{jobr}}=\text{'N'}$
Only set to zero those columns of A$A$ for which the norm of corresponding column of cA < sfmin / ε$cA<\mathit{sfmin}/\epsilon$, that is, those columns that are effectively zero (to machine precision) anyway. If the condition number of A$A$ is greater than the overflow threshold λ$\lambda$, where λ$\lambda$ is the value returned by nag_machine_real_largest (x02al), you are recommended to use function nag_lapack_dgesvj (f08kj).
jobr = 'R'${\mathbf{jobr}}=\text{'R'}$
Set to zero those columns of A$A$ for which the norm of the corresponding column of cA < sqrt(sfmin)$cA<\sqrt{\mathit{sfmin}}$. This approximately represents a restricted range for σ(cA)$\sigma \left(cA\right)$ of [sqrt(sfmin),sqrt(λ)]$\left[\sqrt{\mathit{sfmin}},\sqrt{\lambda }\right]$.
For computing the singular values in the full range from the safe minimum up to the overflow threshold use nag_lapack_dgesvj (f08kj)
Constraint: jobr = 'N'${\mathbf{jobr}}=\text{'N'}$ or 'R'$\text{'R'}$.
5:     jobt – string (length ≥ 1)
Specifies, in the case n = m$n=m$, whether the function is permitted to use the transpose of A$A$ for improved efficiency. If the matrix is square then the procedure may use transposed A$A$ if AT${A}^{\mathrm{T}}$ seems to be better with respect to convergence. If the matrix is not square, jobt is ignored. The decision is based on two values of entropy over the adjoint orbit of ATA${A}^{\mathrm{T}}A$. See the descriptions of work(6)${\mathbf{work}}\left(6\right)$ and work(7)${\mathbf{work}}\left(7\right)$.
jobt = 'T'${\mathbf{jobt}}=\text{'T'}$
If n = m$n=m$, perform an entropy test and then transpose if the test indicates possibly faster convergence of the Jacobi process if AT${A}^{\mathrm{T}}$ is taken as input. If A$A$ is replaced with AT${A}^{\mathrm{T}}$, then the row pivoting is included automatically.
jobt = 'N'${\mathbf{jobt}}=\text{'N'}$
No entropy test and no transposition is performed.
The option jobt = 'T'${\mathbf{jobt}}=\text{'T'}$ can be used to compute only the singular values, or the full SVD (U$U$, Σ$\Sigma$ and V$V$). In the case where only one set of singular vectors (U$U$ or V$V$) is required, the caller must still provide both u and v, as one of the matrices is used as workspace if the matrix A$A$ is transposed. See the descriptions of u and v
Constraint: jobt = 'T'${\mathbf{jobt}}=\text{'T'}$ or 'N'$\text{'N'}$.
6:     jobp – string (length ≥ 1)
Specifies whether the function should be allowed to introduce structured perturbations to drown denormalized numbers. For details see Drmac and Veselic (2008a) and Drmac and Veselic (2008b). For the sake of simplicity, these perturbations are included only when the full SVD or only the singular values are requested.
jobp = 'P'${\mathbf{jobp}}=\text{'P'}$
Introduce perturbation if A$A$ is found to be very badly scaled (introducing denormalized numbers).
jobp = 'N'${\mathbf{jobp}}=\text{'N'}$
Do not perturb.
Constraint: jobp = 'P'${\mathbf{jobp}}=\text{'P'}$ or 'N'$\text{'N'}$.
7:     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: mn0${\mathbf{m}}\ge {\mathbf{n}}\ge 0$.

### Input Parameters Omitted from the MATLAB Interface

lda ldu ldv 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)$.
The contents of a are overwritten.
2:     sva(n) – double array
The, possibly scaled, singular values of A$A$.
The singular values of A$A$ are σi = αsva(i)${\sigma }_{\mathit{i}}=\alpha {\mathbf{sva}}\left(\mathit{i}\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$, where α = work(1) / work(2)$\alpha ={\mathbf{work}}\left(1\right)/{\mathbf{work}}\left(2\right)$. Normally α = 1$\alpha =1$ and no scaling is required to obtain the singular values. However, if the largest singular value of A$A$ overflows or if small singular values have been saved from underflow by scaling the input matrix A$A$, then α1$\alpha \ne 1$.
If jobr = 'R'${\mathbf{jobr}}=\text{'R'}$ then some of the singular values may be returned as exact zeros because they are below the numerical rank threshold or are denormalized numbers.
3:     u(ldu, : $:$) – double array
The first dimension, ldu, of the array u will be
• if jobu = 'F'${\mathbf{jobu}}=\text{'F'}$, 'U'$\text{'U'}$ or 'W'$\text{'W'}$, ldumax (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 = 'F'${\mathbf{jobu}}=\text{'F'}$, max (1,n)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$ if jobu = 'U'${\mathbf{jobu}}=\text{'U'}$ or 'W'$\text{'W'}$, and at least 1$1$ otherwise
If jobu = 'U'${\mathbf{jobu}}=\text{'U'}$, u contains the m$m$ by n$n$ matrix of the left singular vectors.
If jobu = 'F'${\mathbf{jobu}}=\text{'F'}$, u contains the m$m$ by m$m$ matrix of the left singular vectors, including an orthonormal basis of the orthogonal complement of Range(A$A$).
If jobu = 'W'${\mathbf{jobu}}=\text{'W'}$ and (jobv = 'V'${\mathbf{jobv}}=\text{'V'}$ and jobt = 'T'${\mathbf{jobt}}=\text{'T'}$ and m = n${\mathbf{m}}={\mathbf{n}}$), then u is used as workspace if the procedure replaces A$A$ with AT${A}^{\mathrm{T}}$. In that case, V$V$ is computed in u as left singular vectors of AT${A}^{\mathrm{T}}$ and then copied back to the array v.
If jobu = 'N'${\mathbf{jobu}}=\text{'N'}$, u is not referenced.
4:     v(ldv, : $:$) – double array
The first dimension, ldv, of the array v will be
• if jobv = 'V'${\mathbf{jobv}}=\text{'V'}$, 'J'$\text{'J'}$ or 'W'$\text{'W'}$, ldvmax (1,n)$\mathit{ldv}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
• otherwise ldv1$\mathit{ldv}\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 jobv = 'V'${\mathbf{jobv}}=\text{'V'}$, 'J'$\text{'J'}$ or 'W'$\text{'W'}$, and at least 1$1$ otherwise
If jobv = 'V'${\mathbf{jobv}}=\text{'V'}$ or 'J'$\text{'J'}$, v contains the n$n$ by n$n$ matrix of the right singular vectors.
If jobv = 'W'${\mathbf{jobv}}=\text{'W'}$ and (jobu = 'U'${\mathbf{jobu}}=\text{'U'}$ and jobt = 'T'${\mathbf{jobt}}=\text{'T'}$ and m = n${\mathbf{m}}={\mathbf{n}}$), then v is used as workspace if the procedure replaces A$A$ with AT${A}^{\mathrm{T}}$. In that case, U$U$ is computed in v as right singular vectors of AT${A}^{\mathrm{T}}$ and then copied back to the array u.
If jobv = 'N'${\mathbf{jobv}}=\text{'N'}$, v is not referenced.
5:     work(lwork) – double array
Contains information about the completed job.
work(1)${\mathbf{work}}\left(1\right)$
α = work(1) / work(2)$\alpha ={\mathbf{work}}\left(1\right)/{\mathbf{work}}\left(2\right)$ is the scaling factor such that σi = αsva(i)${\sigma }_{\mathit{i}}=\alpha {\mathbf{sva}}\left(\mathit{i}\right)$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$ are the computed singular values of A$A$. (See the description of sva${\mathbf{sva}}$.)
work(2)${\mathbf{work}}\left(2\right)$
See the description of work(1)${\mathbf{work}}\left(1\right)$.
work(3)${\mathbf{work}}\left(3\right)$
sconda, an estimate for the condition number of column equilibrated A$A$ (if joba = 'E'${\mathbf{joba}}=\text{'E'}$ or 'G'$\text{'G'}$). sconda is an estimate of sqrt(((RTR)11))$\sqrt{\left({‖{\left({R}^{\mathrm{T}}R\right)}^{-1}‖}_{1}\right)}$. It is computed using nag_lapack_dpocon (f07fg). It satisfies n(1/4) × scondaR12n(1/4) × sconda${n}^{-\frac{1}{4}}×\mathit{sconda}\le {‖{R}^{-1}‖}_{2}\le {n}^{\frac{1}{4}}×\mathit{sconda}$ where R$R$ is the triangular factor from the QR$QR$ factorization of A$A$. However, if R$R$ is truncated and the numerical rank is determined to be strictly smaller than n$n$, sconda is returned as 1$-1$, thus indicating that the smallest singular values might be lost.
If full SVD is needed, and you are familiar with the details of the method, the following two condition numbers are useful for the analysis of the algorithm.
work(4)${\mathbf{work}}\left(4\right)$
An estimate of the scaled condition number of the triangular factor in the first QR$QR$ factorization.
work(5)${\mathbf{work}}\left(5\right)$
An estimate of the scaled condition number of the triangular factor in the second QR$QR$ factorization.
The following two parameters are computed if jobt = 'T'${\mathbf{jobt}}=\text{'T'}$.
work(6)${\mathbf{work}}\left(6\right)$
The entropy of ATA${A}^{\mathrm{T}}A$: this is the Shannon entropy of diagATA / traceATA$\mathrm{diag}{A}^{\mathrm{T}}A/\mathrm{trace}{A}^{\mathrm{T}}A$ taken as a point in the probability simplex.
work(7)${\mathbf{work}}\left(7\right)$
The entropy of AAT$A{A}^{\mathrm{T}}$.
6:     iwork(m + 3 × n${\mathbf{m}}+3×{\mathbf{n}}$) – int64int32nag_int array
Contains information about the completed job.
iwork(1)${\mathbf{iwork}}\left(1\right)$
The numerical rank of A$A$ determined after the initial QR$QR$ factorization with pivoting. See the descriptions of joba and jobr.
iwork(2)${\mathbf{iwork}}\left(2\right)$
The number of computed nonzero singular values.
iwork(3)${\mathbf{iwork}}\left(3\right)$
If nonzero, a warning message: If iwork(3) = 1${\mathbf{iwork}}\left(3\right)=1$ then some of the column norms of A$A$ were denormalized (tiny) numbers. The requested high accuracy is not warranted by the data.
7:     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

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

W INFO > 0${\mathbf{INFO}}>0$
nag_lapack_dgejsv (f08kh) did not converge in the allowed number of iterations (30$30$). The computed values might be inaccurate.

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

nag_lapack_dgejsv (f08kh) implements a preconditioned Jacobi SVD algorithm. It uses nag_lapack_dgeqrf (f08ae), nag_lapack_dgelqf (f08ah) and nag_lapack_dgeqp3 (f08bf) as preprocessors and preconditioners. Optionally, an additional row pivoting can be used as a preprocessor, which in some cases results in much higher accuracy. An example is matrix A$A$ with the structure A = D1CD2$A={D}_{1}C{D}_{2}$, where D1${D}_{1}$, D2${D}_{2}$ are arbitrarily ill-conditioned diagonal matrices and C$C$ is a well-conditioned matrix. In that case, complete pivoting in the first QR$QR$ factorizations provides accuracy dependent on the condition number of C$C$, and independent of D1${D}_{1}$, D2${D}_{2}$. Such higher accuracy is not completely understood theoretically, but it works well in practice. Further, if A$A$ can be written as A = BD$A=BD$, with well-conditioned B$B$ and some diagonal D$D$, then the high accuracy is guaranteed, both theoretically and in software, independent of D$D$.

## Example

function nag_lapack_dgejsv_example
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];
joba = 'e';
jobu = 'u';
jobv = 'v';
jobr = 'r';
jobt = 'n';
jobp = 'n';
% Compute the singular values and left and right singular vectors
% of A (A = U*S*V^T, m >= n)
[a, sva, u, v, work, iwork, info] = nag_lapack_dgejsv(joba, jobu, jobv, jobr, jobt, jobp, a);

if info == 0
% Compute the approximate error bound for the computed singular values
% using the 2-norm, s(1) = norm(A), and machine precision, eps.
eps = nag_machine_precision;
serrbd = eps*sva(1);

% Print solution
if (abs(work(1)-work(2)) < 2*eps)
% No scaling required
fprintf('\nSingular values:\n');
disp(transpose(sva));
else
fprintf('\nScaled singular values:\n');
disp(transpose(sva));
fprintf('\nFor true singular values, multiply by a/b\n');
fprintf('   where a=%13.5e and b=%13.5e.\n', work(1), work(2));
end

fprintf('\n');
[ifail] = nag_file_print_matrix_real_gen('g', ' ', u, 'Left singular vectors');
fprintf('\n');
[ifail] = nag_file_print_matrix_real_gen('g', ' ', v, 'Right singular vectors');

% Call nag_lapack_ddisna to estimate reciprocal condition numbers for the singular vectors
[rcondu, info] = nag_lapack_ddisna('Left', int64(6), int64(4), sva);
[rcondv, info] = nag_lapack_ddisna('Right', int64(6), int64(4), sva);

% Print the approximate error bounds for the singular values and vectors
fprintf('\nEstimate of the condition number of column equilibrated a\n');
fprintf('%11.1e\n', work(3));
fprintf('\nError estimate for the singular values a\n');
fprintf('%11.1e\n', serrbd);
fprintf('\nError estimates for left singular vectors\n');
fprintf('%11.1e ',serrbd./rcondu);
fprintf('\n\nError estimates for right singular vectors\n');
fprintf('%11.1e ',serrbd./rcondv);
fprintf('\n');
end

Singular values:
9.9966    3.6831    1.3569    0.5000

Left singular vectors
1       2       3       4
1   0.2774 -0.6003 -0.1277  0.1323
2   0.2020 -0.0301  0.2805  0.7034
3   0.2918  0.3348  0.6453  0.1906
4  -0.0938 -0.3699  0.6781 -0.5399
5  -0.4213  0.5266  0.0413 -0.0575
6   0.7816  0.3353 -0.1645 -0.3957

Right singular vectors
1       2       3       4
1   0.1921 -0.8030  0.0041 -0.5642
2  -0.8794 -0.3926 -0.0752  0.2587
3   0.2140 -0.2980  0.7827  0.5027
4  -0.3795  0.3351  0.6178 -0.6017

Estimate of the condition number of column equilibrated a
9.0e+00

Error estimate for the singular values a
1.1e-15

Error estimates for left singular vectors
1.8e-16     4.8e-16     1.3e-15     2.2e-15

Error estimates for right singular vectors
1.8e-16     4.8e-16     1.3e-15     1.3e-15

function f08kh_example
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];
joba = 'e';
jobu = 'u';
jobv = 'v';
jobr = 'r';
jobt = 'n';
jobp = 'n';
% Compute the singular values and left and right singular vectors
% of A (A = U*S*V^T, m >= n)
[a, sva, u, v, work, iwork, info] = f08kh(joba, jobu, jobv, jobr, jobt, jobp, a);

if info == 0
% Compute the approximate error bound for the computed singular values
% using the 2-norm, s(1) = norm(A), and machine precision, eps.
eps = x02aj;
serrbd = eps*sva(1);

% Print solution
if (abs(work(1)-work(2)) < 2*eps)
% No scaling required
fprintf('\nSingular values:\n');
disp(transpose(sva));
else
fprintf('\nScaled singular values:\n');
disp(transpose(sva));
fprintf('\nFor true singular values, multiply by a/b\n');
fprintf('   where a=%13.5e and b=%13.5e.\n', work(1), work(2));
end

fprintf('\n');
[ifail] = x04ca('g', ' ', u, 'Left singular vectors');
fprintf('\n');
[ifail] = x04ca('g', ' ', v, 'Right singular vectors');

% Call f08fl to estimate reciprocal condition numbers for the singular vectors
[rcondu, info] = f08fl('Left', int64(6), int64(4), sva);
[rcondv, info] = f08fl('Right', int64(6), int64(4), sva);

% Print the approximate error bounds for the singular values and vectors
fprintf('\nEstimate of the condition number of column equilibrated a\n');
fprintf('%11.1e\n', work(3));
fprintf('\nError estimate for the singular values a\n');
fprintf('%11.1e\n', serrbd);
fprintf('\nError estimates for left singular vectors\n');
fprintf('%11.1e ',serrbd./rcondu);
fprintf('\n\nError estimates for right singular vectors\n');
fprintf('%11.1e ',serrbd./rcondv);
fprintf('\n');
end

Singular values:
9.9966    3.6831    1.3569    0.5000

Left singular vectors
1       2       3       4
1   0.2774 -0.6003 -0.1277  0.1323
2   0.2020 -0.0301  0.2805  0.7034
3   0.2918  0.3348  0.6453  0.1906
4  -0.0938 -0.3699  0.6781 -0.5399
5  -0.4213  0.5266  0.0413 -0.0575
6   0.7816  0.3353 -0.1645 -0.3957

Right singular vectors
1       2       3       4
1   0.1921 -0.8030  0.0041 -0.5642
2  -0.8794 -0.3926 -0.0752  0.2587
3   0.2140 -0.2980  0.7827  0.5027
4  -0.3795  0.3351  0.6178 -0.6017

Estimate of the condition number of column equilibrated a
9.0e+00

Error estimate for the singular values a
1.1e-15

Error estimates for left singular vectors
1.8e-16     4.8e-16     1.3e-15     2.2e-15

Error estimates for right singular vectors
1.8e-16     4.8e-16     1.3e-15     1.3e-15