Class SVD<T extends MatrixMixin<T,?,?,?,?,?,?,?>>
java.lang.Object
org.flag4j.linalg.decompositions.svd.SVD<T>
- Type Parameters:
T
- The type of the matrix to compute the singular value decomposition of.
- All Implemented Interfaces:
Decomposition<T>
- Direct Known Subclasses:
ComplexSVD
,RealSVD
public abstract class SVD<T extends MatrixMixin<T,?,?,?,?,?,?,?>>
extends Object
implements Decomposition<T>
This abstract class specifies methods for computing the singular value decomposition (SVD) of a matrix.
That is, decompose a rectangular matrix
M
as M=USV
H where U
and V
are
unitary matrices whose columns are the left and right singular vectors of M
and S
is a rectangular
diagonal matrix containing the singular values of M
.-
Field Summary
Modifier and TypeFieldDescriptionprotected boolean
Flag which indicates if the singular vectors should be computed in addition to the singular values.protected int
The rank of the matrix being decomposed.protected boolean
Flag which indicates if the reduced (or full) SVD should be computed.protected Matrix
The rectangular diagonalS
corresponding toM=USV
H in the SVD.protected T
The unitary matrixU
corresponding toM=USV
H in the SVD.protected T
The unitary matrixV
corresponding toM=USV
H in the SVD. -
Constructor Summary
ModifierConstructorDescriptionprotected
SVD
(boolean computeUV, boolean reduced) Creates a decomposer to compute the Schur decomposition. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
computeRank
(int rows, int cols, double[] singularValues) Computes the rank of the matrix being decomposed using the singular values of the matrix.Applies decomposition to the source matrix.protected abstract void
extractNormalizedCols
(T singularVecs, int j) Extracts the singular vectors, normalizes them and sets the columns ofU
andV
to be the left/right singular vectors.int
getRank()
Gets the rank of the last matrix decomposed.getS()
Gets the diagonal matrixS
corresponding toM=USV
H in the SVD.getU()
Gets the unitary matrixU
corresponding toM=USV
H in the SVD.getV()
Gets the unitary matrixV
corresponding toM=USV
H in the SVD.protected abstract void
Initializes the unitaryU
andV
matrices for the SVD.protected abstract T
invDirectSum
(T src) Computes the inverse direct sum of a matrix and its hermitian transpose.protected abstract T
makeEigenPairs
(T B, double[] eigVals) Gets the eigen values and vectors of symmetric block matrix which corresponds to the singular values and vectors of the matrix being decomposed.protected abstract void
makeEigenVals
(T B, double[] eigVals) Gets the eigen values of the symmetric block matrix which corresponds to the singular values of the matrix being decomposed.
-
Field Details
-
computeUV
protected boolean computeUVFlag which indicates if the singular vectors should be computed in addition to the singular values. -
reduced
protected boolean reducedFlag which indicates if the reduced (or full) SVD should be computed. -
U
The unitary matrixU
corresponding toM=USV
H in the SVD. -
S
The rectangular diagonalS
corresponding toM=USV
H in the SVD. -
V
The unitary matrixV
corresponding toM=USV
H in the SVD. -
rank
protected int rankThe rank of the matrix being decomposed. This is calculated as a byproduct of the decomposition.
-
-
Constructor Details
-
SVD
protected SVD(boolean computeUV, boolean reduced) Creates a decomposer to compute the Schur decomposition.- Parameters:
computeUV
- A flag which indicates if the unitary matricesQ
andV
should be computed (i.e. the singular vectors).
- If true, theQ
andV
matrices will be computed. - If false, theQ
andV
matrices will not be computed. If it is not needed, this may provide a performance improvement.reduced
- Flag which indicates if the reduced (or full) SVD should be computed.
- If true, reduced SVD is computed. - If false, the full SVD is computed.
-
-
Method Details
-
getU
Gets the unitary matrixU
corresponding toM=USV
H in the SVD.- Returns:
U
corresponding toM=USV
H in the SVD.
-
getS
Gets the diagonal matrixS
corresponding toM=USV
H in the SVD.- Returns:
S
corresponding toM=USV
H in the SVD.
-
getV
Gets the unitary matrixV
corresponding toM=USV
H in the SVD.- Returns:
V
corresponding toM=USV
H in the SVD. Note that the hermitian transpose has not been computed.
-
getRank
public int getRank()Gets the rank of the last matrix decomposed. This is computed as a byproduct of the decomposition.- Returns:
- The rank of the last matrix decomposed.
-
decompose
Applies decomposition to the source matrix.- Specified by:
decompose
in interfaceDecomposition<T extends MatrixMixin<T,
?, ?, ?, ?, ?, ?, ?>> - Parameters:
src
- The source matrix to decompose.- Returns:
- A reference to this decomposer.
-
invDirectSum
-
computeRank
protected void computeRank(int rows, int cols, double[] singularValues) Computes the rank of the matrix being decomposed using the singular values of the matrix.- Parameters:
rows
- The number of rows in the original source matrix.cols
- The number of columns in the original source matrix.singularValues
- The singular values of the original source matrix.
-
makeEigenPairs
Gets the eigen values and vectors of symmetric block matrix which corresponds to the singular values and vectors of the matrix being decomposed.- Parameters:
B
- Symmetric block matrix to compute the eigenvalues of.eigVals
- Storage for eigenvalues.- Returns:
- The eigenvalues and eigenvectors of the symmetric block matrix which corresponds to the singular values and vectors of the matrix being decomposed.
-
makeEigenVals
Gets the eigen values of the symmetric block matrix which corresponds to the singular values of the matrix being decomposed.- Parameters:
B
- Symmetric block matrix to compute the eigenvalues of.eigVals
- Storage for eigenvalues.
-
initUV
Initializes the unitaryU
andV
matrices for the SVD.- Parameters:
src
- Shape of the source matrix being decomposed.cols
- The number of columns forU
andV
.
-
extractNormalizedCols
Extracts the singular vectors, normalizes them and sets the columns ofU
andV
to be the left/right singular vectors.- Parameters:
singularVecs
- Computed left and right singular vectors.j
- Index of the column ofU
andV
to set.
-