Singular Value Decomposition
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 <rw/dgenmat.h>
#include <rw/rstream.h>
#include <rw/lapack/sv.h>
#include <rw/lapack/svdcalc.h>
int main()
{
RWGenMat<float> A; // 1
std::cin >> A;
RWSVDecomp<double, RWSVDCalc<double> > svd(A); // 2
std::cout << "singular values: " // 3
<< svd.singularValues() << std::endl;
std::cout << "left vectors" << svd.leftVectors() << std::endl;
std::cout << "right vectors" << svd.rightVectors()
<< std::endl;
return 0;
}
SVD Server Objects
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>
#include <rw/math/genmat.h>
#include <rw/lapack/lssv.h>
#include <rw/lapack/svddccalc.h>
int main()
{
RWGenMat<double> A; // 1
std::cin >> A;
RWSVServer< double, RWSVDDivConqCalc<double> > server; // 2
server.computeRightVectors(0);
server.setTolerance(0.001);
RWLeastSqSV< double, RWSVDDivConqCalc<double> > svd =
server(A); // 3
std::cout << "singular values: " // 4
<< svd.singularValues() << std::endl;
std::cout << "left vectors" << svd.leftVectors() << std::endl;
return 0;
}