- All Implemented Interfaces:
Serializable
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 S, and an n-by-n orthogonal matrix V so that A = USVT. For an m-by-n matrix A with m < n, the singular value decomposition is an m-by-m orthogonal matrix U, and m-by-m diagonal matrix S, and an n-by-m orthogonal matrix V so that A = U*S*VT.
The singular values, sigma[k] = S[k][k], are ordered so that sigma[0] ≥ sigma[1] ≥ ... ≥ sigma[n-1].
The singular value decomposition always exists, so the constructor will never fail. The matrix condition number and the effective numerical rank can be computed from this decomposition.
The column vectors of U are those eigenvectors of AAT corresponding to eigenvalues that are the squares of the values on the diagonal of S. Similarly the column vectors of V are the eigenvectors of ATA corresponding to eigenvalues that are the squares of the values on the diagonal of S. That is, the eigenvalue for the eigenvector contained in the jth column of U, and the eigenvalue for the eigenvector contained in the jth column of V are both the square of Sjj.
The implementation is based on the one used in the Jama package (a public-domain package provided via a collaboration between MathWorks and the National Institute of Standards and Technology). Jama defines a matrix class whereas QRDecomp treats matrices as two-dimensional arrays, and was written so that the org.bzdev library is self-contained. Some additional runtime tests were added, and there are a few additional constructors and methods. Since this is more or less a port of the Jama class, this particular class is in the public domain. The Jama implementation is reliable for m ≥ n, and may fail otherwise. To handle the case where m < n, we construct the singular-value decomposition for the matrix's transpose and then swap the value of U and V. This works because, if AT = USVT, then A = (USVT)T = VSTUT = VSUT (S is diagonal and therefore ST = S).
Note: the definition of the singular value decomposition differs from the one used in the Wikipedia article, where U is an m by m matrix and S is an m by n diagonal matrix (i.e., all off-diagonal elements are zero). The Wikipedia definition defines U so it contains all the eigenvectors of AAT, not just the ones whose eigenvalues are non-negative. The definition used in by this class, by contrast, includes only those eigenvectors whose eigenvalues are non-negative. The rationale for the definition used by this class is that the matrices are smaller, which reduces memory requirements.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondouble
cond()
Get the 2-norm condition number.int
Get the number of columns for the matrix that was decomposed.int
Get the number of rows for the matrix that was decomposed.double[][]
getS()
Get the diagonal matrix for this singular value decomposition.double[]
Get the singular values provided by this singular value decomposition.double[][]
getU()
Get the left singular vectors for this singular value decomposition.double[][]
getV()
Get the right singular vectors for this singular value decomponsition.double
norm2()
Get the 2-norm of the matrix A = USV, where U, S, and V are the matrices comprising this singular value decomposition.int
rank()
Get the effective numerical rank for this singular value decomposition.
-
Constructor Details
-
SVDecomp
public SVDecomp(double[][] A) Constructor.- Parameters:
A
- an m by n matrix with m ≥ n
-
SVDecomp
public SVDecomp(double[][] A, int m, int n, boolean strict) Constructor given explicit dimensions.- Parameters:
A
- the m by n matrix to decompose, with m ≥ nm
- the number of rowsn
- the number of columnsstrict
- true if A must have exactly the number of rows and columns required; false if A may have rows or columns longer than needed, in which case the additional entries will be ignored
-
SVDecomp
public SVDecomp(double[] flatMatrix, boolean columnMajorOrder, int m, int n) throws IllegalArgumentException Constructor given a flat representation of a matrix. The elements of the matrix can be stored in one of two orders. Given a matrix Aij, if the row index varies the fastest, the flat representation will consist of the sequence A00, A10, .... Otherwise the sequence is A00, A 01, ....The number of rows m must be at least as large as the number of columns n.
- Parameters:
flatMatrix
- a one-dimensional array representing a two dimensional matrixcolumnMajorOrder
- true if the inverse is stored in column major order (e.g., the order used by Fortran); false if the inverse is stored in row-major-order (e.g., the order used by C)m
- the number of rowsn
- the number of columns- Throws:
IllegalArgumentException
- the flat matrix is too small to represent an m by n matrix
-
-
Method Details
-
getNumberOfRows
public int getNumberOfRows()Get the number of rows for the matrix that was decomposed.- Returns:
- the number of rows.
-
getNumberOfColumns
public int getNumberOfColumns()Get the number of columns for the matrix that was decomposed.- Returns:
- the number of columns.
-
getU
public double[][] getU()Get the left singular vectors for this singular value decomposition. For an m by n matrix A withm ≥ n whose singular value decomponsition is USV, the matrix U is a square n by n matrix whose columns are called the left-singular vectors.- Returns:
- the matrix U
-
getV
public double[][] getV()Get the right singular vectors for this singular value decomponsition. For an m by n matrix A with ≥ n whose singular value decomponsition is USV the matrix V is an m by n matrix whose columns are called the right-singular vectors.- Returns:
- the matrix V
-
getSingularValues
public double[] getSingularValues()Get the singular values provided by this singular value decomposition. For an m by n matrix A with ≥ n whose singular value decomponsition is USV the matrix S is an m by n diagonal matrix with non-negative real numbers on the diagonal ordered so that the largest values occur first.- Returns:
- a vector of length n containing the diagonal elements of S
-
getS
public double[][] getS()Get the diagonal matrix for this singular value decomposition. For an m by n matrix A with ≥ n whose singular value decomponsition is USV the matrix S is an m by n diagonal matrix with non-negative real numbers on the diagonal ordered so that the largest values appear first.- Returns:
- the matrix S
-
norm2
public double norm2()Get the 2-norm of the matrix A = USV, where U, S, and V are the matrices comprising this singular value decomposition. For a vector, the 2-norm is simply the square root of the sum of the squares of the vector's components. For a matrix A the 2-norm is supx≠0(‖Ax‖2/‖x‖2). It is numerically equal to the largest singular value.- Returns:
- the 2-norm of the matrix whose singular value decomposition is this singular value decomposition
-
cond
public double cond()Get the 2-norm condition number. The value is the largest singular value divided by the smallest- Returns:
- max(S)/min(S)
-
rank
public int rank()Get the effective numerical rank for this singular value decomposition. The effective numerical rank is the number of singular values that are above a limit set by floating-point accuracy. The limit is numerically equal to the first singular value multiple by 2-52max(m,n) for a singular value decomposition of an m by n matrix.- Returns:
- the effective numerical rank
-