Class SVD<T extends MatrixMixin<T,?,?,?>>
java.lang.Object
org.flag4j.linalg.decompositions.Decomposition<T>
org.flag4j.linalg.decompositions.svd.SVD<T>
- Type Parameters:
T
- The type of the matrix to compute the singular value decomposition of.
- Direct Known Subclasses:
ComplexSVD
,RealSVD
This abstract class specifies methods for computing the singular value decomposition (SVD) of a matrix.
That is, decomposes a rectangular matrix \( M \) into \( M=U\Sigma V^{H} \) where \( U \) and \( V \) are unitary matrices whose columns are the left and right singular vectors of \( M \) and \( \Sigma \) is a rectangular diagonal matrix containing the singular values of \( M \).
The SVD may also be used to compute the (numerical) rank of the matrix using getRank()
.
The SVD proceeds by an iterative algorithm with possible random behavior. For reproducibility, constructors support specifying a seed for the pseudo-random number generator.
Usage:
The decomposition workflow typically follows these steps:- Instantiate a concrete instance of
SVD
. - Call
decompose(MatrixMixin)
to perform the factorization. - Retrieve the resulting matrices using
getU()
andgetS()
.
Efficiency Considerations:
If singular vectors are not required, settingcomputeUV = false
may improve performance.- See Also:
-
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 \( \Sigma \) corresponding to \( M=U\Sigma V^{H} \) in the SVD.protected long
Seed to use in pseudo-random number generator.protected T
The unitary matrix \( U \) corresponding to \( M=U\Sigma V^{H} \) in the SVD.protected boolean
Flag indicating if seeding should be used in pseudo-random operations.protected T
The unitary matrix \( V \) corresponding to \( M=U\Sigma V^{H} \) in the SVD.Fields inherited from class org.flag4j.linalg.decompositions.Decomposition
hasDecomposed
-
Constructor Summary
Constructors -
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 \( \Sigma \) corresponding to \( M=U\Sigma V^{H} \) in the SVD.Gets the singular values of the last matrix decomposed.getU()
Gets the unitary matrix \( U \) corresponding to \( M=U\Sigma V^{H} \) in the SVD.getV()
Gets the unitary matrix \( V \) corresponding to \( M=U\Sigma V^{H} \) 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.Methods inherited from class org.flag4j.linalg.decompositions.Decomposition
ensureHasDecomposed
-
Field Details
-
seed
protected long seedSeed to use in pseudo-random number generator. -
useSeed
protected boolean useSeedFlag indicating if seeding should be used in pseudo-random operations.- If
true
, then seeding will be used. - If
false
, then seeding will not be used.
- If
-
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=U\Sigma V^{H} \) in the SVD. -
S
The rectangular diagonal \( \Sigma \) corresponding to \( M=U\Sigma V^{H} \) in the SVD. -
V
The unitary matrix \( V \) corresponding to \( M=U\Sigma V^{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 matrices \( U \) and \( V \) should be computed (i.e. the singular vectors).- If
true
, the \( U \) and \( V \) matrices will be computed. - If
false
, the \( U \) and \( V \) matrices will not be computed. If it is not needed, this may provide a performance improvement.
- If
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.
- If
-
SVD
protected SVD(boolean computeUV, boolean reduced, long seed) Creates a decomposer to compute the Schur decomposition.- Parameters:
computeUV
- A flag which indicates if the unitary matrices \( U \) and \( V \) should be computed (i.e. the singular vectors).- If
true
, the \( U \) and \( V \) matrices will be computed. - If
false
, the \( U \) and \( V \) matrices will not be computed. If it is not needed, this may provide a performance improvement.
- If
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.
- If
seed
- Seed to use in pseudo-random number generators. Setting this will allow for reproducibility between multiple calls with the same inputs.
-
-
Method Details
-
getU
Gets the unitary matrix \( U \) corresponding to \( M=U\Sigma V^{H} \) in the SVD.- Returns:
- \( U \) corresponding to \( M=U\Sigma V^{H} \) in the SVD.
-
getS
Gets the diagonal matrix \( \Sigma \) corresponding to \( M=U\Sigma V^{H} \) in the SVD.- Returns:
- \( \Sigma \) corresponding to \( M=U\Sigma V^{H} \) 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=U\Sigma V^{H} \) in the SVD.- Returns:
- \( V \) corresponding to \( M=U\Sigma V^{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 classDecomposition<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.
-