The singular value decomposition (SVD) is perhaps the most revealing decomposition in numerical linear algebra. It provides a wealth of information to the analyst, including orthogonal bases for the domain and range spaces, the 2-norm and Frobenius norm of the matrix, and a determination of the numerical rank of a matrix. The SVD of a matrix A is:
where the elements of the diagonal matrix S are the singular values, and the columns of U and V are the left and right singular vectors of A. Here's an example program that reads a matrix from standard input and prints the components of its SVD:
#include <iostream.h> #include <rw/dgenmat.h> #include <rw/dsv.h> main() { RWGenMat<double> A; // 1 cin >> A; DoubleSVDecomp svd(A); // 2 cout << "singular values: " // 3 << svd.singularValues() << endl; cout << "left vectors" << svd.leftVectors() << endl; cout << "right vectors" << svd.rightVectors() << endl; }
//1 | Here we define the matrix A and read it from standard input. |
//2 | This line constructs the singular value decomposition object. |
//3 | In the next few lines, we print out the components of the singular value decomposition. See the Class Reference for a complete list of the member functions available to manipulate the SVD object. |
Sometimes, you may want more control over the computation of the singular value decomposition. For example, you might want to specify that only the left vectors are computed, or only the right vectors, or none at all. You might want to specify a tolerance indicating that singular values below a certain threshold are considered 0. You can control these aspects of the SVD object's construction by using a singular value server object. For example, the following program computes only the left singular vectors of A, and specifies that singular values less than 0.001 are to be treated as 0.
#include <iostream.h> #include <rw/dgenmat.h> #include <rw/dsv.h> main() { RWGenMat<double> A; // 1 cin >> A; DoubleSVServer server; // 2 server.computeRightVectors(0); server.setTolerance(0.001); DoubleSVDecomp svd = server(A); // 3 cout << "singular values: " // 4 << svd.singularValues() << endl; cout << "left vectors" << svd.leftVectors() << endl; }
//1 | As before we define the matrix A and read it from standard input. |
//2 | In this line, a new server object is constructed. The parameters inside the server are initialized to their default values. In the next two lines, we change two of the parameters, setting the number of right singular vectors to compute to 0, and setting the tolerance to 0.001. |
//3 | This line constructs the singular value decomposition object using the server. |
//4 | As before, we print out the components of the decomposition. |
©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.