A singular value decomposition (SVD) is a representation of a matrix A of the form:
where U and V are orthogonal, S is diagonal, and V* denotes the transpose of a real matrix V or the conjugate transpose of a complex matrix V. The entries along the diagonal of S are the singular values. The columns of U are the left singular vectors, and the columns of V are the right singular vectors.
NMath Matrix provides singular value decomposition classes for four datatypes: single- and double-precision floating point numbers, and single- and double-precision complex numbers. The classnames are FloatSVDecomp, DoubleSVDecomp, FloatComplexSVDecomp, and DoubleComplexSVDecomp.
Instances of the singular value decomposition classes are constructed from general matrices of the appropriate datatype. For example, this code creates a DoubleSVDecomp from a DoubleMatrix:
DoubleMatrix A = new DoubleMatrix( "4 x 3 [ 1 2 3 12 -2 6 -8 9 11 5 7 -1]" ); DoubleSVDecomp svd = new DoubleSVDecomp( A );
By default, the reduced singular value decomposition and all singular vectors are computed. For greater control, NMath Matrix provides singular value decomposition server classes that create singular value decomposition objects with non-default decomposition parameters. The classnames are FloatSVDecompServer, DoubleSVDecompServer, FloatComplexSVDecompServer, and DoubleComplexSVDecompServer.
The singular value decomposition server classes all have the same interface:
For example, this code uses a FloatComplexSVDecompServer to turn off the computation of the singular vectors:
FloatComplexSVDecompServer svds = new FloatComplexSVDecompServer(); svds.ComputeLeftVector = false; svds.ComputeRightVector = false; int rows = 10, cols = 10; FloatComplexMatrix A = new FloatComplexMatrix( rows, cols, new RandGenUniform( -1, 1 ) ); FloatComplexSVDecomp svd = svds.GetDecomp( A );
Once a singular value decomposition object has been constructed from a matrix, various read-only properties are provided for retrieving the elements of the decomposition, and for retrieving information about the original matrix:
int rows = 5, cols = 5; Float Matrix A = new FloatMatrix( rows, cols, new RandGenUniform( 1, -1 ) ); FloatSVDecomp svd = new FloatSVDecomp( A ); FloatMatrix U = svd.LeftVectors; FloatMatrix V = svd.RightVectors; FloatVector s = svd.SingularValues;
Methods are also provided for retrieving individual singular vectors and singular values:
For example, this code returns the first singular value, which is equal to the Euclidean (L2) norm of the matrix A:
int rows = 12, cols = 6; DoubleComplexMatrix A = new DoubleComplexMatrix( rows, cols, new RandGenUniform( 1, -1) ); DoubleComplexSVDecomp svd = new DoubleComplexSVDecomp( A ); double l2 = svd.SingularValue( 0 );
Lastly, a Truncate() method is provided that sets all singular values less than a given tolerance to zero. Corresponding singular vectors are also removed.
NOTE- This method can change the numerical rank of the matrix A, which is equal to the number of non-zero singular values.
DoubleMatrix A = new DoubleMatrix( "5x5[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5]" ); DoubleSVDecomp svd = new DoubleSVDecomp( A ); int fullRank = svd.Rank; // == 5 svd.Truncate( 1e-14 ); int deficientRank = svd.Rank; // == 2
An existing decomposition object can be reused with another matrix using the Factor() method:
int rows = 12, cols = 6; FloatMatrix A = new FloatMatrix( rows, cols, new RandGenUniform( 1, -1 ) ); FloatSVDecomp svd = new DoubleSVDecomp( A ); FloatVector svA = svd.SingularValues; FloatMatrix B = new DoubleMatrix( "5x5[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5]" ); svd.Factor( B ); FloatVector svB = svd.SingularValues;TOC | Previous | Next | Index