Simple linear equations and matrix decomposition of Eigen learning

  Eigen provides calculation methods for understanding linear equations, including LU decomposition method, QR decomposition method, SVD (singular value decomposition), eigenvalue decomposition, etc. For a linear system of the general form:

        

  The way to solve the above equation is generally to decompose the matrix A, of course, the most basic method is the Gaussian elimination method.

  Let's take a look at Eigen's official first routine:

 1 #include <iostream>
 2 #include <Eigen/Dense>
 3 
 4 using namespace std;
 5 using namespace Eigen;
 6 
 7 int main()
 8 {
 9     Matrix3f A;
10     Vector3f b;
11     A << 1,2,3, 4,5,6, 7,8,10;
12     b << 3,3,4;
13     cout<<"Here is the Matrix A:\n"<< A <<endl;
14     cout<<" Here is the vector b:\n"<< b <<endl;
15     Vector3f x = A.colPivHouseholderQr().solve(b);
16     cout<<"The solution is:\n"<<x<<endl;
17     return 0;
18 }

The results are as follows:

Eigen's built-in algorithm for solving systems of linear equations is shown in the following table:

 

The problem of matrix multiplication can also be solved using these interfaces:

 1 #include <iostream>
 2 #include <Eigen/Dense>
 3 
 4 using namespace std;
 5 using namespace Eigen;
 6 
 7 int main()
 8 {
 9     Matrix2f A,b;
10     A << 2,-1,-1,3;
11     b << 1,2,3,1;
12     cout<<"Here is the matrix A:\n"<<A<<endl;
13     cout<<"Here is the right hand side b:\n"<<b<<endl;
14     Matrix2f x = A.ldlt().solve(b);
15     cout<<"The solution is:\n"<<x<<endl;
16     return 0;
17 }



The results are as follows:

 

Eigen also provides algorithms for computing eigenvalues ​​and eigenvectors:

Here is a simple example:

 1 #include <iostream>
 2 #include <Eigen/Dense>
 3 
 4 using namespace std;
 5 using namespace Eigen;
 6 
 7 int main()
 8 {
 9     Matrix2f A;
10     A << 1,2,2,3;
11     cout<<"Here is the matrix A:\n"<<A<<endl;
12     SelfAdjointEigenSolver<Matrix2f> eigensolver(A);
13     if( eigensolver.info() != Success ) abort();
14     cout<<" The eigenvalues of A are:\n"<<eigensolver.eigenvalues()<<endl;
15     cout<<" Here is a matrix whose columns are eigenvectors of A\n"
16         <<" corresponding to these eigenvalues:\n"
17         <<eigensolver.eigenvectors()<<endl;
18     return 0;
19 }

The results are as follows:

 

Eigen also provides algorithms for matrix inversion and matrix determinant, but these two algorithms are very uneconomical algorithms for large matrices. When you need to do such operations on large matrices, you need to judge by yourself. No need to do so. But for small matrices it can be used without concern.

Below is an example:

 1 #include <iostream>
 2 #include <Eigen/Dense>
 3 
 4 using namespace std;
 5 using namespace Eigen;
 6 
 7 int main()
 8 {
 9     Matrix3f A;
10     A << 1,2,1,
11          2,1,0,
12          -1,1,2;
13 
14     cout<<"Here is the matrix A:\n"<<A<<endl;
15     cout<<"The determinant of A is "<<A.determinant()<<endl;
16     cout<<"The inverse of A is:\n"<<A.inverse()<<endl;
17     return 0;
18 }

The results are as follows:

 

Eigen also provides solutions to understand the least squares problem, and gives two implementations, BDCSVD and JacobiSVD, of which the recommended one is BDCSVD. Below is an example:

 1 #include <iostream>
 2 #include <Eigen/Dense>
 3 
 4 using namespace std;
 5 using namespace Eigen;
 6 
 7 int main()
 8 {
 9     MatrixXf A = MatrixXf::Random(3,2);
10     cout<<"Here is the matrix A:\n"<<A<<endl;
11     VectorXf b = VectorXf::Random(3);
12     cout<<"Here is the right hand side b:\n"<<b<<endl;
13     cout<<"The least-squares solution is:\n"
14         <<A.bdcSvd(ComputeThinU|ComputeThinV).solve(b)<<endl;
15     return 0;
16 }

The results are as follows:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325095327&siteId=291194637