smile.math.matrix

## Class Matrix.SVD

• java.lang.Object
• smile.math.matrix.Matrix.SVD
• All Implemented Interfaces:
java.io.Serializable
Enclosing class:
Matrix

```public static class Matrix.SVD
extends java.lang.Object
implements java.io.Serializable```
Singular Value Decomposition.

For an m-by-n matrix A with m ≥ n, the singular value decomposition is an m-by-n orthogonal matrix U, an n-by-n diagonal matrix Σ, and an n-by-n orthogonal matrix V so that A = U*Σ*V'.

For m < n, only the first m columns of V are computed and Σ is m-by-m.

The singular values, σk = Σkk, are ordered so that σ0 ≥ σ1 ≥ ... ≥ σn-1.

The singular value decomposition always exists. The matrix condition number and the effective numerical rank can be computed from this decomposition.

SVD is a very powerful technique for dealing with sets of equations or matrices that are either singular or else numerically very close to singular. In many cases where Gaussian elimination and LU decomposition fail to give satisfactory results, SVD will diagnose precisely what the problem is. SVD is also the method of choice for solving most linear least squares problems.

Applications which employ the SVD include computing the pseudo-inverse, least squares fitting of data, matrix approximation, and determining the rank, range and null space of a matrix. The SVD is also applied extensively to the study of linear inverse problems, and is useful in the analysis of regularization methods such as that of Tikhonov. It is widely used in statistics where it is related to principal component analysis. Yet another usage is latent semantic indexing in natural language text processing.

Serialized Form
• ### Field Summary

Fields
Modifier and Type Field and Description
`int` `m`
The number of rows of matrix.
`int` `n`
The number of columns of matrix.
`double[]` `s`
The singular values in descending order.
`Matrix` `U`
The left singular vectors U.
`Matrix` `V`
The right singular vectors V.
• ### Constructor Summary

Constructors
Constructor and Description
```SVD(double[] s, Matrix U, Matrix V)```
Constructor.
```SVD(int m, int n, double[] s)```
Constructor.
• ### Method Summary

All Methods
Modifier and Type Method and Description
`double` `condition()`
Returns the L2 norm condition number, which is max(S) / min(S).
`Matrix` `diag()`
Returns the diagonal matrix of singular values.
`double` `norm()`
Returns the L2 matrix norm.
`int` `nullity()`
Returns the dimension of null space.
`Matrix` `nullspace()`
Returns the matrix which columns are the orthonormal basis for the null space.
`Matrix` `pinv()`
Returns the pseudo inverse.
`Matrix` `range()`
Returns the matrix which columns are the orthonormal basis for the range space.
`int` `rank()`
Returns the effective numerical matrix rank.
`double[]` `solve(double[] b)`
Solves the least squares min || B - A*X ||.
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Field Detail

• #### m

`public final int m`
The number of rows of matrix.
• #### n

`public final int n`
The number of columns of matrix.
• #### s

`public final double[] s`
The singular values in descending order.
• #### U

`public final Matrix U`
The left singular vectors U.
• #### V

`public final Matrix V`
The right singular vectors V.
• ### Constructor Detail

• #### SVD

```public SVD(int m,
int n,
double[] s)```
Constructor.
• #### SVD

```public SVD(double[] s,
Matrix U,
Matrix V)```
Constructor.
• ### Method Detail

• #### diag

`public Matrix diag()`
Returns the diagonal matrix of singular values.
• #### norm

`public double norm()`
Returns the L2 matrix norm. The largest singular value.
• #### rank

`public int rank()`
Returns the effective numerical matrix rank. The number of non-negligible singular values.
• #### nullity

`public int nullity()`
Returns the dimension of null space. The number of negligible singular values.
• #### condition

`public double condition()`
Returns the L2 norm condition number, which is max(S) / min(S). A system of equations is considered to be well-conditioned if a small change in the coefficient matrix or a small change in the right hand side results in a small change in the solution vector. Otherwise, it is called ill-conditioned. Condition number is defined as the product of the norm of A and the norm of A-1. If we use the usual L2 norm on vectors and the associated matrix norm, then the condition number is the ratio of the largest singular value of matrix A to the smallest. The condition number depends on the underlying norm. However, regardless of the norm, it is always greater or equal to 1. If it is close to one, the matrix is well conditioned. If the condition number is large, then the matrix is said to be ill-conditioned. A matrix that is not invertible has the condition number equal to infinity.
• #### range

`public Matrix range()`
Returns the matrix which columns are the orthonormal basis for the range space. Returns null if the rank is zero (if and only if zero matrix).
• #### nullspace

`public Matrix nullspace()`
Returns the matrix which columns are the orthonormal basis for the null space. Returns null if the matrix is of full rank.
• #### pinv

`public Matrix pinv()`
Returns the pseudo inverse.
• #### solve

`public double[] solve(double[] b)`
Solves the least squares min || B - A*X ||.
Parameters:
`b` - the right hand side of overdetermined linear system.
Returns:
the solution vector beta that minimizes ||Y - X*beta||.
Throws:
`java.lang.RuntimeException` - if matrix is rank deficient.