![]() |
Eigen
3.2.0
|
lede
The best way to do least squares solving is with a SVD decomposition. Eigen provides one as the JacobiSVD class, and its solve() is doing least-squares solving.
Here is an example:
Example: | Output: |
---|---|
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
MatrixXf A = MatrixXf::Random(3, 2);
cout << "Here is the matrix A:\n" << A << endl;
VectorXf b = VectorXf::Random(3);
cout << "Here is the right hand side b:\n" << b << endl;
cout << "The least-squares solution is:\n"
}
| Here is the matrix A: 0.68 0.597 -0.211 0.823 0.566 -0.605 Here is the right hand side b: -0.33 0.536 -0.444 The least-squares solution is: -0.67 0.314 |
For more information, including faster but less reliable methods, read our page concentrating on linear least squares problems.