Module: Linear Algebra Group: Least Squares Factorization classes
Does not inherit
#include <rw/lapack/lsch.h> #include <rw/lapack/lsqr.h> #include <rw/lapack/lssv.h> #include <rw/lapack/dvddccalc.h> RWLeastSqSV<double, RWSVDDivConqCalc<double> > SVD(A); // A is
// an RWGenMat<double> RWMathVec<double> x = SVD.solve(b); // b is an RWMathVec<double>
A linear system of equations has the form Ax=b, where A contains more rows than columns and generally has no exact solution; it is overdetermined. The best that we can do is to find a solution vector x such that the norm of the residual vector, Ax-b, is made as small as possible. The vector x is then a solution in the least squares sense, and this sort of problem is called a least squares problem.
There are three classes in the Linear Algebra Module for solving least squares problems. The approach is to construct a factorization of the matrix A that can be used to solve the problem. The classes RWLeastSqCh<T>, RWLeastSqQR<T,QRCalc>, and RWLeastSqSV<T,SVDCalc> encapsulate this factorization object. There are three methods provided:
Cholesky method or method of normal equations, encoded by Ch
Complete orthogonal factorization method, encoded by QR
Singular value method, encoded by SV.
Which method you choose depends on the trade-off between speed and robustness that you want to make. As you move down the list, the methods decrease in speed, but increase in accuracy and robustness.
A system of equations Ax=b is underdetermined if the columns of A are linearly dependent. In this case, there are infinitely many solutions. Often, the one that is desired is the one with minimum norm. The classes RWLeastSqQR<T,QRCalc> and RWLeastSqSV<T> allow you to compute this solution.
Note that the classes RWLeastSqQR<T,QRCalc> and RWLeastSqSV<T> are publicly derived from their underlying decomposition types, so that advanced users can manipulate the underlying decomposition directly.
NOTE -- For greater flexibility with RWLeastSqQR<T,QRCalc>, the user can implement this method, or the Linear Algebra Module provides two classes to perform this function - RWQRCalc<T> and RWQRCalcP3<T>. Please see their descriptions in this reference guide for more information.
// Find the least squares (if overdetermined) or
// minimum norm (if underdetermined) solution to
// the linear system of equations Ax=b. Also input
// a tolerance called epsilon which represents the
// accuracy of the entries in A. This makes the
// procedure robust in the event that A has nearly
// linearly dependent columns. #include <iostream> #include <rw/lapack/lsch.h> #include <rw/lapack/lsqr.h> #include <rw/lapack/lssv.h> #include <rw/lapack/svddccalc.h> int main() { RWGenMat<double> A; double tol; RWMathVec<double> b; std::cin >> A >> tol >> b; RWLeastSqSV<double, RWSVDDivConqCalc<double> >decomp (A,tol); RWMathVec<double> x = decomp.solve(b); std::cout << "Solution is " << x << std::endl; return 0; }
RWLeastSqCh(); RWLeastSqQR(); RWLeastSqSV();
Default constructor. Builds an empty decomposition. The decomposition is filled in using the factor function.
RWLeastSqCh(const RWGenMat<T>& A); RWLeastSqQR(const RWGenMat<float>& A, float tol=0); RWLeastSqQR(const RWGenMat<double>& A, double tol=0); RWLeastSqQR(const RWGenMat<DComplex>& A, double
tol=0); RWLeastSqSV(const RWGenMat<float>& A, float tol=0); RWLeastSqSV(const RWGenMat<double>& A, double tol=0); RWLeastSqSV(const RWGenMat<DComplex>& A, double
tol=0);
Constructs a factorization representing the given matrix. In the QR and SV methods, the supplied tolerance tells when to consider entries along the diagonal of the underlying decomposition as 0. The tolerance should indicate the accuracy to which you know entries in A.
RWLeastSqQR(const RWCODecomp<T,QRCalc>& A); RWLeastSqQR(const RWQRDecomp<float,SVDCalc>& A, float
tol=0); RWLeastSqQR(const RWQRDecomp<double,SVDCalc>& A,
double tol=0); RWLeastSqQR(const RWQRDecomp<DComplex,SVDCalc>& A,
double tol=0); RWLeastSqSV(const RWSVDecomp<float,SVDCalc>& A, float
tol=0); RWLeastSqSV(const RWSVDecomp<double,SVDCalc>& A,
double tol=0); RWLeastSqSV(const RWSVDecomp<DComplex,SVDCalc>& A,
double tol=0);
Constructs a factorization representing the linear system represented by the given decomposition. The parameter tol tells when to consider entries along the diagonal of the given QR or SV decomposition as 0. The tolerance should indicate the accuracy to which you know the entries in the matrix.
unsigned
cols();
Returns the number of columns in the matrix represented by this factorization.
void RWLeastSqCh<T>::factor(const RWGenMat<T>& A); void RWLeastSqQR<float,QRCalc>::factor(const
RWGenMat<float>& A, float tol=0); RWLeastSqQR<double,QRCalc>::factor(const
RWGenMat<double>& A, double tol=0); RWLeastSqQR<DComplex>::factor(const
RWGenMat<DComplex>& A, double tol=0); void RWLeastSqSV<float,SVDCalc>::factor(const
RWGenMat<float>& A, float tol=0); void RWLeastSqSV<double,SVDCalc>::factor(const
RWGenMat<double>& A, double tol=0); void RWLeastSqSV<DComplex,SVDCalc>::factor(const
RWGenMat<DComplex>& A, double tol=0);
Replaces the current factorization with a factorization representing the given matrix. The current factorization is lost. In the QR and SV methods, the supplied tolerance tells when to consider entries along the diagonal of the underlying decomposition as 0. The tolerance should indicate the accuracy to which you know entries in A.
void RWLeastSqQR<float,QRCalc>::factor(const
RWQRDecomp<float>& A, float tol=0); RWLeastSqQR<double,QRCalc>::factor(const
RWQRDecomp<double>& A, double tol=0); RWLeastSqQR<DComplex,QRCalc>::factor(const
RWQRDecomp<DComplex>& A, double tol=0); void RWLeastSqQR<T>::factor(const RWCODecomp<T>& A); void RWLeastSqSV<float,SVDCalc>::factor(const
RWSVDecomp<float>& A, float tol=0); RWLeastSqSV<double,SVDCalc>::factor(const
RWSVDecomp<double>& A, double tol=0); RWLeastSqSV<DComplex,SVDCalc>::factor(const
RWSVDecomp<DComplex>& A, double tol=0);
Replaces the current factorization with a factorization representing the linear system represented by the given decomposition. The current factorization is lost. The parameter tol tells when to consider entries along the diagonal of the given QR or SV decomposition as 0. The tolerance should indicate the accuracy to which you know the entries in the matrix.
unsigned
RWLeastSqCh<T>::fail(); unsigned
RWLeastSqSV<T,SVDCalc>::fail(); unsigned
RWLeastSqCh<T>::good(); unsigned
RWLeastSqSV<T,SVDCalc>::good();
Indicates whether the construction of the factorization is successful. In the Cholesky case, the factorization can fail if the system matrix is not of full rank. In the SV case, the factorization can fail if some of the singular values fail to converge.
unsigned
rank();
Returns the rank the matrix represented by this factorization. If a tolerance is used to construct the factorization, this is a measure of the numerical rank of the matrix, that is, the rank assuming entries on the diagonal of the decomposition less than the tolerance are 0.
unsigned
rows();
Returns the number of rows in the matrix represented by this factorization.
void RWLeastSqSV<T,SVDCalc>::truncate(float tol);
Truncates the factorization so that singular values less than the indicated tolerance are treated as 0.
unsigned residual(const RWMathVec<T>& b);
Returns the residual vector, b-Ax, associated with the right-hand side vector b and the corresponding least squares solution vector x.
unsigned residualNorm(const RWMathVec<T>& b);
Returns the norm of the residual vector, (b-Ax)'(b-Ax), associated with the right-hand side vector b and the corresponding least squares solution vector x.
RWMathVec<T> solve(const RWMathVec<T>& b);
Returns the solution to the least squares problem for the given right-hand-side vector b. If the system matrix is overdetermined, this is the vector that minimizes the norm of the residual vector. If the system matrix is not overdetermined, this is the minimum norm vector that satisfies the system equations.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.