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, decomposes a rectangular matrix M into M=USVH 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
FieldsModifier 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 diagonal S corresponding to M=USVH in the SVD.protected T
The unitary matrix U corresponding to M=USVH in the SVD.protected T
The unitary matrix V corresponding to M=USVH in the SVD. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
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 of U and V to be the left/right singular vectors.int
getRank()
Gets the rank of the last matrix decomposed.getS()
Gets the diagonal matrix S corresponding to M=USVH in the SVD.Gets the singular values of the last matrix decomposed.getU()
Gets the unitary matrix U corresponding to M=USVH in the SVD.getV()
Gets the unitary matrix V corresponding to M=USVH in the SVD.protected abstract void
Initializes the unitary U and V 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 matrix U corresponding to M=USVH in the SVD. -
S
The rectangular diagonal S corresponding to M=USVH in the SVD. -
V
The unitary matrix V corresponding to M=USVH 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
and V should be computed (i.e. the singular vectors).
- If true, theQ
and V matrices will be computed. - If false, theQ
and V 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 matrix U corresponding to M=USVH in the SVD.- Returns:
- U corresponding to M=USVH in the SVD.
-
getS
Gets the diagonal matrix S corresponding to M=USVH in the SVD.- Returns:
- S corresponding to M=USVH in the SVD.
-
getSingularValues
Gets the singular values of the last matrix decomposed.- Returns:
- The singular values of the last matrix decomposed.
-
getV
Gets the unitary matrix V corresponding to M=USVH in the SVD.- Returns:
- V corresponding to M=USVH 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 unitary U and V matrices for the SVD.- Parameters:
src
- Shape of the source matrix being decomposed.cols
- The number of columns for U and V.
-
extractNormalizedCols
Extracts the singular vectors, normalizes them and sets the columns of U and V to be the left/right singular vectors.- Parameters:
singularVecs
- Computed left and right singular vectors.j
- Index of the column of U and V to set.
-