public class SVDecomposition
extends java.lang.Object
Providing a separate class for the SVD allows an application to perform such decompositions repeatedly without having to reallocate temporary storage space.
Note: by default, this performs a "thin" SVD, where U and V are not
necessarily square matrices if the input is not square. To enable a full
SVD, such as when necessary for computing null-spaces of non-square
matrices, then factor the matrix using the flag FULL_UV
.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_ITERATION_LIMIT
The default iteration limit for computing the SVD.
|
static int |
FULL_UV
Specifies to compute the full SVD decomposition, otherwise only a
'thin' decomposition is computed for non-square matrices
|
static int |
OMIT_U
Specifies that matrix U should not be computed.
|
static int |
OMIT_UV
Specifies that neither matrix U nor matrix V should not be computed.
|
static int |
OMIT_V
Specifies that matrix V should not be computed.
|
Constructor and Description |
---|
SVDecomposition()
Creates an uninitialized SVDecomposition.
|
SVDecomposition(Matrix M)
Creates an SVDecomposition and initializes it to the SVD for the matrix
M.
|
SVDecomposition(Matrix M,
int flags)
Creates an SVDecomposition and initializes it to the SVD for the matrix
M.
|
Modifier and Type | Method and Description |
---|---|
double |
condition()
Computes the condition number of the original matrix M associated with
this SVD.
|
double |
determinant()
Computes the determinant of the original matrix M associated with this
SVD.
|
static SVDecomposition |
factor(DenseMatrix U,
Vector svec,
DenseMatrix V,
Matrix M)
Convenience method that creates an SVDecomposition, factors it for the
matrix M, and stores the resulting U, S and V values into the
corresponding arguments.
|
void |
factor(Matrix M)
Peforms an SVD on the Matrix M.
|
void |
factor(Matrix M,
int flags)
Peforms an SVD on the Matrix M.
|
void |
get(DenseMatrix U,
Vector svec,
DenseMatrix V)
Gets the matrices associated with the SVD.
|
int |
getIterationLimit()
Gets the iteration limit for SVD computations.
|
VectorNd |
getS()
Returns the current singular values associated with this
decomposition.
|
MatrixNd |
getU()
Returns the current U matrix associated with this decomposition.
|
MatrixNd |
getV()
Returns the current V matrix associated with this decomposition.
|
boolean |
inverse(MatrixNd R)
Computes the inverse of the original matrix M associated this SVD, and
places the result in R.
|
double |
norm()
Computes the 2-norm of the original matrix M associated with this SVD.
|
boolean |
pseudoInverse(DenseMatrix R)
Computes the pseudo inverse of the original matrix M associated this SVD,
and places the result in R.
|
boolean |
pseudoInverse(MatrixNd R)
Computes the pseudo inverse of the original matrix M associated this SVD,
and places the result in R.
|
int |
rank(double tol)
Estimates the rank of the original matrix used to form this
decomposition, based on a supplied tolerance
tol . |
void |
setIterationLimit(int lim)
Sets the iteration limit for SVD computations.
|
boolean |
solve(DenseMatrix X,
DenseMatrix B)
Solves the linear equation
M X = B where M is the original matrix associated with this SVD, and X and B are matrices. |
boolean |
solve(DenseMatrix X,
DenseMatrix B,
double tol)
Solves the linear equation
M X = B where M is the original matrix associated with this SVD, and X and B are matrices. |
boolean |
solve(Vector x,
Vector b)
Solves the linear equation
M x = b where M is the original matrix associated with this SVD, and x and b are vectors. |
boolean |
solve(Vector x,
Vector b,
double tol)
Solves the linear equation
M x = b where M is the original matrix associated with this SVD, and x and b are vectors. |
public static final int OMIT_U
public static final int OMIT_V
public static final int OMIT_UV
public static final int FULL_UV
public static final int DEFAULT_ITERATION_LIMIT
public SVDecomposition(Matrix M)
M
- matrix to perform the SVD onpublic SVDecomposition(Matrix M, int flags)
OMIT_U
and/or OMIT_V
.M
- matrix to perform the SVD onflags
- flags controlling the factorizationpublic SVDecomposition()
public int getIterationLimit()
setIterationLimit(int)
public void setIterationLimit(int lim)
lim
- iteration limitgetIterationLimit()
public void factor(Matrix M)
M
- matrix to perform the SVD onpublic void factor(Matrix M, int flags)
OMIT_U
and/or OMIT_V
.M
- matrix to perform the SVD onflags
- controlling the factorization.public void get(DenseMatrix U, Vector svec, DenseMatrix V)
U
- If not null
, returns the left-hand orthogonal matrixsvec
- If not null
, returns the diagonal elements of SV
- If not null
, returns the right-hand orthogonal matrix
(note that this is V, and not it's transpose V^T).ImproperStateException
- if this SVDecomposition is uninitialized, or if either U or V are requested
but were not computedImproperSizeException
- if U, svec, or V are not of the proper dimension and cannot be resized.public MatrixNd getU()
null
. Subsequent factorizations will
cause a different U to be created. The returned matrix should not be
modified if any subsequent calls are to be made which depend on U
(including solve and inverse methods).ImproperStateException
- if this SVDecomposition is uninitializedpublic MatrixNd getV()
null
. Subsequent factorizations will
cause a different V to be created. The returned matrix should not be
modified if any subsequent calls are to be made which depend on V
(including solve and inverse methods).ImproperStateException
- if this SVDecomposition is uninitializedpublic VectorNd getS()
ImproperStateException
- if this SVDecomposition is uninitializedpublic static SVDecomposition factor(DenseMatrix U, Vector svec, DenseMatrix V, Matrix M)
null
, then U and/or V will not be computed.U
- If not null
, returns the left-hand orthogonal matrixsvec
- If not null
, returns the diagonal elements of SV
- If not null
, returns the right-hand orthogonal matrix
(note that this is V, and not it's transpose V^T).M
- matrix to be factoredpublic double condition()
ImproperStateException
- if this SVDecomposition is uninitializedpublic double norm()
ImproperStateException
- if this SVDecomposition is uninitializedpublic double determinant()
ImproperStateException
- if this SVDecomposition is uninitializedpublic boolean solve(Vector x, Vector b)
m X n
with m > n
, then the system
is overdetermined and solution with the minimum least square error is
computed. If m < n
, then the system is underdetermined
and the minimum norm solution is computed.
This method assumes that M has full rank (i.e., that the minimum
singular value is > 0
). If it does not, then the method returns
false and the solution will likely contain infinite values. To handle
situations where M does not have full rank, one should use solve(Vector,Vector,double)
instead.
x
- unknown vector to solve forb
- constant vectorImproperStateException
- if this decomposition is uninitialized, or if U or V were not computedImproperSizeException
- if b does not have a size compatible with M, or if x does not have a size
compatible with M and cannot be resized.public boolean solve(Vector x, Vector b, double tol)
m X n
with m > n
, then the system
is overdetermined and solution with the minimum least square error is
computed. If m < n
, then the system is underdetermined
and the minimum norm solution is computed.
To handle situations where M
is rank deficient, the
calculation ignores singular values whose value is less than or equal to
tol*sigmax
, where tol
is a specified tolerance and
is the maximum singular value. Results that would
otherwise be obtained by dividing by these values are instead set to
zero, resulting in pseudoinverse solutions. Specifying a negative value for
tol
removes this behavior, so that the resulting solution will be
identical to #solve(VectorNd,VectorNd)} and false
will be
returned if M does not have full rank.
x
- unknown vector to solve forb
- constant vectortol
- solution tolerancetol
is negative and M does not have full rankImproperStateException
- if this decomposition is uninitialized, or if U or V were not computedImproperSizeException
- if b does not have a size compatible with M, or if x does not have a size
compatible with M and cannot be resized.public boolean solve(DenseMatrix X, DenseMatrix B)
m X n
with m > n
, then the system
is overdetermined and solution with the minimum least square error is
computed. If m < n
, then the system is underdetermined
and the minimum norm solution (which respect to each column of
X) is computed.
This method assumes that M has full rank (i.e., that the minimum
singular value is > 0
). If it does not, then the method returns
false and the solution will likely contain infinite values. To handle
situations where M does not have full rank, one should use solve(DenseMatrix,DenseMatrix,double)
instead.
X
- unknown matrix to solve forB
- constant matrixImproperStateException
- if this decomposition is uninitialized, or if U or V were not computedImproperSizeException
- if B has a different number of rows than M, or if the size of X
is incompatible with B or M and cannot be resized.public boolean solve(DenseMatrix X, DenseMatrix B, double tol)
m X n
with m > n
, then the system
is overdetermined and solution with the minimum least square error is
computed. If m < n
, then the system is underdetermined
and the minimum norm solution (which respect to each column of
X) is computed.
To handle situations where M
is rank deficient, the
calculation ignores singular values whose value is less than or equal to
tol*sigmax
, where tol
is a specified tolerance and
is the maximum singular value. Results that would
otherwise be obtained by dividing by these values are instead set to
zero, resulting in pseudoinverse solutions. Specifying a negative value for
tol
removes this behavior, so that the resulting solution will be
identical to #solve(VectorNd,VectorNd)} and false
will be
returned if M does not have full rank.
X
- unknown matrix to solve forB
- constant matrixtol
- solution tolerancetol
is negative and M does not have full rank
of M is zeroImproperStateException
- if this decomposition is uninitialized, or if U or V were not computedImproperSizeException
- if B has a different number of rows than M, or if the size of X
is incompatible with B or M and cannot be resized.public boolean inverse(MatrixNd R)
R
- matrix in which the inverse is storedImproperStateException
- if this decomposition is uninitialized, or if U or V were not computedImproperSizeException
- if M is not square, or if R does not have the same size as M and cannot be
resized.public boolean pseudoInverse(MatrixNd R)
R
- matrix in which the inverse is storedImproperStateException
- if this decomposition is uninitialized, or if U or V were not computedImproperSizeException
- if R does not have the same size as M and cannot be resized.public boolean pseudoInverse(DenseMatrix R)
R
- matrix in which the inverse is storedImproperStateException
- if this decomposition is uninitialized, or if U or V were not computedImproperSizeException
- if R does not have the same size as M and cannot be resized.public int rank(double tol)
tol
. The rank is
estimated by counting all singular values whose value is greater than
smax*tol
, where smax
is the maximim singular value.tol
- tolerance for estimating the rankImproperStateException
- if this SVDecomposition is uninitialized