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_dgesvj (f08kj)

## Purpose

nag_lapack_dgesvj (f08kj) computes the one-sided Jacobi singular value decomposition (SVD) of a real m$m$ by n$n$ matrix A$A$, mn$m\ge n$, with fast scaled rotations and de Rijk’s pivoting, optionally computing the left and/or right singular vectors. For m < n$m, the functions nag_lapack_dgesvd (f08kb) or nag_lapack_dgesdd (f08kd) may be used.

## Syntax

[a, sva, v, work, info] = f08kj(joba, jobu, jobv, a, mv, v, work, 'm', m, 'n', n)
[a, sva, v, work, info] = nag_lapack_dgesvj(joba, jobu, jobv, a, mv, v, work, 'm', m, 'n', n)

## Description

The SVD is written as
 A = UΣVT , $A = UΣVT ,$
where Σ$\Sigma$ is an n$n$ by n$n$ diagonal matrix, U$U$ is an m$m$ by n$n$ orthonormal 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$.

## 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 structure of matrix A$A$.
joba = 'L'${\mathbf{joba}}=\text{'L'}$
The input matrix A$A$ is lower triangular.
joba = 'U'${\mathbf{joba}}=\text{'U'}$
The input matrix A$A$ is upper triangular.
joba = 'G'${\mathbf{joba}}=\text{'G'}$
The input matrix A$A$ is a general m$m$ by n$n$ matrix, mn${\mathbf{m}}\ge {\mathbf{n}}$.
Constraint: joba = 'L'${\mathbf{joba}}=\text{'L'}$, 'U'$\text{'U'}$ or 'G'$\text{'G'}$.
2:     jobu – string (length ≥ 1)
Specifies whether to compute the left singular vectors and if so whether you want to control their numerical orthogonality threshold.
jobu = 'U'${\mathbf{jobu}}=\text{'U'}$
The left singular vectors corresponding to the nonzero singular values are computed and returned in the leading columns of a. See more details in the description of a. The numerical orthogonality threshold is set to approximately tol = ctol × ε$\mathit{tol}=\mathit{ctol}×\epsilon$, where ε$\epsilon$ is the machine precision and ctol = sqrt(m)$\mathit{ctol}=\sqrt{m}$.
jobu = 'C'${\mathbf{jobu}}=\text{'C'}$
Analogous to jobu = 'U'${\mathbf{jobu}}=\text{'U'}$, except that you can control the level of numerical orthogonality of the computed left singular vectors. The orthogonality threshold is set to tol = ctol × ε$\mathit{tol}=\mathit{ctol}×\epsilon$, where ctol$\mathit{ctol}$ is given on input in work(1)${\mathbf{work}}\left(1\right)$. The option jobu = 'C'${\mathbf{jobu}}=\text{'C'}$ can be used if m × ε$m×\epsilon$ is a satisfactory orthogonality of the computed left singular vectors, so ctol = m$\mathit{ctol}={\mathbf{m}}$ could save a few sweeps of Jacobi rotations. See the descriptions of a and work(1)${\mathbf{work}}\left(1\right)$.
jobu = 'N'${\mathbf{jobu}}=\text{'N'}$
The matrix U$U$ is not computed. However, see the description of a.
Constraint: jobu = 'U'${\mathbf{jobu}}=\text{'U'}$, 'C'$\text{'C'}$ or 'N'$\text{'N'}$.
3:     jobv – string (length ≥ 1)
Specifies whether and how to compute the right singular vectors.
jobv = 'V'${\mathbf{jobv}}=\text{'V'}$
The matrix V$V$ is computed and returned in the array v.
jobv = 'A'${\mathbf{jobv}}=\text{'A'}$
The Jacobi rotations are applied to the leading mv${m}_{v}$ by n$n$ part of the array v. In other words, the right singular vector matrix V$V$ is not computed explicitly, instead it is applied to an mv${m}_{v}$ by n$n$ matrix initially stored in the first mv rows of v.
jobv = 'N'${\mathbf{jobv}}=\text{'N'}$
The matrix V$V$ is not computed and the array v is not referenced.
Constraint: jobv = 'V'${\mathbf{jobv}}=\text{'V'}$, 'A'$\text{'A'}$ or 'N'$\text{'N'}$.
4:     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$.
5:     mv – int64int32nag_int scalar
If jobv = 'A'${\mathbf{jobv}}=\text{'A'}$, the product of Jacobi rotations is applied to the first mv${m}_{v}$ rows of v.
If jobv'A'${\mathbf{jobv}}\ne \text{'A'}$, mv is ignored. See the description of jobv.
6:     v(ldv, : $:$) – double array
The first dimension, ldv, of the array v must satisfy
• if jobv = 'V'${\mathbf{jobv}}=\text{'V'}$, ldvmax (1,n)$\mathit{ldv}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
• if jobv = 'A'${\mathbf{jobv}}=\text{'A'}$, ldvmax (1,mv)$\mathit{ldv}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{mv}}\right)$;
• otherwise ldv1$\mathit{ldv}\ge 1$.
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)$ if jobv = 'V'${\mathbf{jobv}}=\text{'V'}$ or 'A'$\text{'A'}$, and at least 1$1$ otherwise
If jobv = 'A'${\mathbf{jobv}}=\text{'A'}$, v must contain an mv${m}_{v}$ by n$n$ matrix to be premultiplied by the matrix V$V$ of right singular vectors.
7:     work(lwork) – double array
lwork, the dimension of the array, must satisfy the constraint lworkmax (6,m + n)$\mathit{lwork}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(6,{\mathbf{m}}+{\mathbf{n}}\right)$.
If jobu = 'C'${\mathbf{jobu}}=\text{'C'}$, work(1) = ctol${\mathbf{work}}\left(1\right)=\mathit{ctol}$, where ctol$\mathit{ctol}$ defines the threshold for convergence. The process stops if all columns of A$A$ are mutually orthogonal up to ctol × ε$\mathit{ctol}×\epsilon$. It is required that ctol1$\mathit{ctol}\ge 1$, i.e., it is not possible to force the function to obtain orthogonality below ε$\epsilon$. ctol$\mathit{ctol}$ greater than 1 / ε$1/\epsilon$ is meaningless, where ε$\epsilon$ is the machine precision.
Constraint: if jobu = 'C'${\mathbf{jobu}}=\text{'C'}$, work(1)1.0${\mathbf{work}}\left(1\right)\ge 1.0$.

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

lda 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 matrix U$U$ containing the left singular vectors of A$A$.
If jobu = 'U'${\mathbf{jobu}}=\text{'U'}$ or 'C'$\text{'C'}$
if info = 0${\mathbf{info}}=0$
rank(A)$\mathrm{rank}\left(A\right)$ orthonormal columns of U$U$ are returned in the leading rank(A)$\mathrm{rank}\left(A\right)$ columns of the array a. Here rank(A)n$\mathrm{rank}\left(A\right)\le {\mathbf{n}}$ is the number of computed singular values of A$A$ that are above the safe range parameter, as returned by nag_machine_real_safe (x02am). The singular vectors corresponding to underflowed or zero singular values are not computed. The value of rank(A)$\mathrm{rank}\left(A\right)$ is returned by rounding work(2)${\mathbf{work}}\left(2\right)$ to the nearest whole number. Also see the descriptions of sva and work. The computed columns of U$U$ are mutually numerically orthogonal up to approximately tol = sqrt(m) × ε$\mathit{tol}=\sqrt{m}×\epsilon$; or tol = ctol × ε$\mathit{tol}=\mathit{ctol}×\epsilon$ (jobu = 'C'${\mathbf{jobu}}=\text{'C'}$), where ε$\epsilon$ is the machine precision and ctol$\mathit{ctol}$ is supplied on entry in work(1)${\mathbf{work}}\left(1\right)$, see the description of jobu.
If info > 0${\mathbf{info}}>0$
nag_lapack_dgesvj (f08kj) did not converge in 30$30$ iterations (sweeps). In this case, the computed columns of U$U$ may not be orthogonal up to tol$\mathit{tol}$. The output U$U$ (stored in a), Σ$\Sigma$ (given by the computed singular values in sva) and V$V$ is still a decomposition of the input matrix A$A$ in the sense that the residual Aα × U × Σ × VT2 / A2${‖A-\alpha ×U×\Sigma ×{V}^{\mathrm{T}}‖}_{2}/{‖A‖}_{2}$ is small, where α$\alpha$ is the value returned in work(1)${\mathbf{work}}\left(1\right)$.
If jobu = 'N'${\mathbf{jobu}}=\text{'N'}$
if info = 0${\mathbf{info}}=0$
Note that the left singular vectors are ‘for free’ in the one-sided Jacobi SVD algorithm. However, if only the singular values are needed, the level of numerical orthogonality of U$U$ is not an issue and iterations are stopped when the columns of the iterated matrix are numerically orthogonal up to approximately m × ε$m×\epsilon$. Thus, on exit, a contains the columns of U$U$ scaled with the corresponding singular values.
If info > 0${\mathbf{info}}>0$
nag_lapack_dgesvj (f08kj) did not converge in 30$30$ iterations (sweeps).
2:     sva(n) – double array
The, possibly scaled, singular values of A$A$.
If info = 0${\mathbf{info}}=0$
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 α$\alpha$ is the scale factor stored in work(1)${\mathbf{work}}\left(1\right)$. Normally α = 1$\alpha =1$, however, if some of the singular values of A$A$ might underflow or overflow, then α1$\alpha \ne 1$ and the scale factor needs to be applied to obtain the singular values.
If info > 0${\mathbf{info}}>0$
nag_lapack_dgesvj (f08kj) did not converge in 30$30$ iterations and α × sva$\alpha ×{\mathbf{sva}}$ may not be accurate.
3:     v(ldv, : $:$) – double array
The first dimension, ldv, of the array v will be
• if jobv = 'V'${\mathbf{jobv}}=\text{'V'}$, ldvmax (1,n)$\mathit{ldv}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$;
• if jobv = 'A'${\mathbf{jobv}}=\text{'A'}$, ldvmax (1,mv)$\mathit{ldv}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{mv}}\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'}$ or 'A'$\text{'A'}$, and at least 1$1$ otherwise
The right singular vectors of A$A$.
If jobv = 'V'${\mathbf{jobv}}=\text{'V'}$, v contains the n$n$ by n$n$ matrix of the right singular vectors.
If jobv = 'A'${\mathbf{jobv}}=\text{'A'}$, v contains the product of the computed right singular vector matrix and the initial matrix in the array v.
If jobv = 'N'${\mathbf{jobv}}=\text{'N'}$, v is not referenced.
4:     work(lwork) – double array
lworkmax (6,m + n)$\mathit{lwork}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(6,{\mathbf{m}}+{\mathbf{n}}\right)$.
Contains information about the completed job.
work(1)${\mathbf{work}}\left(1\right)$
the scaling factor, α$\alpha$, 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 description of sva.)
work(2)${\mathbf{work}}\left(2\right)$
nint(work(2))$\mathrm{nint}\left({\mathbf{work}}\left(2\right)\right)$gives the number of the computed nonzero singular values.
work(3)${\mathbf{work}}\left(3\right)$
nint(work(3))$\mathrm{nint}\left({\mathbf{work}}\left(3\right)\right)$ gives the number of the computed singular values that are larger than the underflow threshold.
work(4)${\mathbf{work}}\left(4\right)$
nint(work(4))$\mathrm{nint}\left({\mathbf{work}}\left(4\right)\right)$ gives the number of iterations (sweeps of Jacobi rotations) needed for numerical convergence.
work(5)${\mathbf{work}}\left(5\right)$
maxij|cos(A( : ,i),A( : ,j))|${\mathrm{max}}_{i\ne j}|\mathrm{cos}\left(A\left(:,i\right),A\left(:,j\right)\right)|$ in the last iteration (sweep). This is useful information in cases when nag_lapack_dgesvj (f08kj) did not converge, as it can be used to estimate whether the output is still useful and for subsequent analysis.
work(6)${\mathbf{work}}\left(6\right)$
The largest absolute value over all sines of the Jacobi rotation angles in the last sweep. It can be useful for subsequent analysis.
5:     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_dgesvj (f08kj) did not converge in the allowed number of iterations (30$30$), but its output might still be useful.

## 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.
See Section 6 of Drmac and Veselic (2008a) for a detailed discussion of the accuracy of the computed SVD.

This SVD algorithm is numerically superior to the bidiagonalization based QR$QR$ algorithm implemented by nag_lapack_dgesvd (f08kb) and the divide and conquer algorithm implemented by nag_lapack_dgesdd (f08kd) algorithms and is considerably faster than previous implementations of the (equally accurate) Jacobi SVD method. Moreover, this algorithm can compute the SVD faster than nag_lapack_dgesvd (f08kb) and not much slower than nag_lapack_dgesdd (f08kd). See Section 3.3 of Drmac and Veselic (2008b) for the details.

## Example

```function nag_lapack_dgesvj_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 = 'g';
jobu = 'u';
jobv = 'v';
work = zeros(10,1);
v    = zeros(4, 4);
mv = int64(0);
% Compute the singular values and left and right singular vectors
% of A (A = U*S*V, m >= n)
[a, sva, v, work, info] = nag_lapack_dgesvj(joba, jobu, jobv, a, mv, v, work);

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
fprintf('\nSingular values:\n');
disp(transpose(sva));
if (abs(work(1)-1) > eps)
fprintf('\nValues nned scaling by %13.5e.\n', work(1));
end

fprintf('\n');
[ifail] = nag_file_print_matrix_real_gen('g', ' ', a, '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('\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

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 f08kj_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 = 'g';
jobu = 'u';
jobv = 'v';
work = zeros(10,1);
v    = zeros(4, 4);
mv = int64(0);
% Compute the singular values and left and right singular vectors
% of A (A = U*S*V, m >= n)
[a, sva, v, work, info] = f08kj(joba, jobu, jobv, a, mv, v, work);

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
fprintf('\nSingular values:\n');
disp(transpose(sva));
if (abs(work(1)-1) > eps)
fprintf('\nValues nned scaling by %13.5e.\n', work(1));
end

fprintf('\n');
[ifail] = x04ca('g', ' ', a, '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('\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

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

```