Eigen学习之简单线性方程与矩阵分解

  Eigen提供了解线性方程的计算方法,包括LU分解法,QR分解法,SVD(奇异值分解)、特征值分解等。对于一般形式如下的线性系统:

        

  解决上述方程的方式一般是将矩阵A进行分解,当然最基本的方法是高斯消元法。

  先来看Eigen 官方的第一个例程:

 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 }

运行结果如下:

Eigen内置的解线性方程组的算法如下表所示:

使用这些接口也可以解决矩阵相乘的问题:

 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 }



运行结果如下:

Eigen也提供了计算特征值和特征向量的算法:

下面是一个简单的例子:

 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 }

运行结果如下:

Eigen 也提供了求逆矩阵和求矩阵行列式的算法,但是这两种算法对于大型矩阵来说都是非常不经济的算法,当需要对大型矩阵做这种的操作时,需要自己判断到底需不需这样做。但是对于小型矩阵 则可以没有顾虑地使用。

下面是一个例子:

 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 }

运行结果如下:

Eigen也提供了解最小二乘问题的解法,并给出两种实现,分别是BDCSVD和JacobiSVD,其中推荐使用的一种是BDCSVD。下面是一个例子:

 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 }

运行结果如下:

猜你喜欢

转载自www.cnblogs.com/wangxiaoyong/p/8977343.html