当前位置:   article > 正文

Eigen学习(九)稠密问题之线性代数和分解_eigen的solve()

eigen的solve()

本节介绍如何求解线性系统,计算几种分解,比如LU,QR,SVD等。

基本的线性求解

问题:假设有一个系统方程写成如下矩阵的形式

其中A,b是矩阵,b也可以是向量,当想要求解x时,可以选择多种分解方式,取决于矩阵A的形式以及考虑的速度和精度,下面是一个简单的例子

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

解为

Here is the matrix A:
 1  2  3
 4  5  6
 7  8 10
Here is the vector b:
3
3
4
The solution is:
-2
 1
 1

例子中colPivHouseholderQr()方法返回一个类ColPivHouseholderQR的对象,因此那句话也可以写成

  1. ColPivHouseholderQR<Matrix3f> dec(A);
  2. Vector3f x = dec.solve(b);

这里ColPivHouseholderQR是QR分解的意思,适用于各种矩阵并且速度够快,下面是几种分解的对比

DecompositionMethodRequirements
on the matrix
Speed
(small-to-medium)
Speed
(large)
Accuracy
PartialPivLUpartialPivLu()Invertible+++++
FullPivLUfullPivLu()None-- -+++
HouseholderQRhouseholderQr()None+++++
ColPivHouseholderQRcolPivHouseholderQr()None++-+++
FullPivHouseholderQRfullPivHouseholderQr()None-- -+++
LLTllt()Positive definite+++++++
LDLTldlt()Positive or negative
semidefinite
++++++
JacobiSVDjacobiSvd()None- -- - -+++

以上分解都提供了solve()方法。比如当矩阵是正定的时候,表中说明LLT和LDLT是不错的选择:

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

计算特征值和特征向量

这里需要特征分解,确保矩阵是自伴矩阵。下面是一个使用SelfAdjointEigenSolver的例子,使用EigenSolver或者ComplexEigenSolver,可以容易的适用到普通矩阵上。计算特征值和特征向量时不要求一定收敛,当然这种情况很少见。

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

计算逆和行列式

尽管逆和行列式是基本的数学概念,但是在数值/线性代数中却不像纯数学中那么受欢迎。求逆运算通常由solve()代替,行列式一般并不是检测矩阵是否可逆的好方法。当然对于比较小的矩阵时行列式和逆还是比较有用的。

尽管存在以上矩阵分解的方法,你仍然可以直接调用inverse()方法和determinant()方法。如果你的矩阵尺寸比较小(4x4)那么Eigen可以避免使用LU分解,而是使用数学公式,这样更高效。

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

最小二乘解

求最小二乘解最精确的方式是使用SVD分解。Eigen提供了JacobiSVD类,他的solve()方法计算了最小二乘解。

  1. #include <iostream>
  2. #include <Eigen/Dense>
  3. using namespace std;
  4. using namespace Eigen;
  5. int main()
  6. {
  7. MatrixXf A = MatrixXf::Random(3, 2);
  8. cout << "Here is the matrix A:\n" << A << endl;
  9. VectorXf b = VectorXf::Random(3);
  10. cout << "Here is the right hand side b:\n" << b << endl;
  11. cout << "The least-squares solution is:\n"
  12. << A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b) << endl;
  13. }
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
其他方法,比如Cholesky分解或QR分解,会更快一些但是解稍微不那么可靠。

将求解和构造分开

上面的例子中,当分解对象创建时分解就开始计算了(写在同一句中),当然可以有方法将他们独立开来:

  1. #include <iostream>
  2. #include <Eigen/Dense>
  3. using namespace std;
  4. using namespace Eigen;
  5. int main()
  6. {
  7. Matrix2f A, b;
  8. LLT<Matrix2f> llt;
  9. A << 2, -1, -1, 3;
  10. b << 1, 2, 3, 1;
  11. cout << "Here is the matrix A:\n" << A << endl;
  12. cout << "Here is the right hand side b:\n" << b << endl;
  13. cout << "Computing LLT decomposition..." << endl;
  14. llt.compute(A);
  15. cout << "The solution is:\n" << llt.solve(b) << endl;
  16. A(1,1)++;
  17. cout << "The matrix A is now:\n" << A << endl;
  18. cout << "Computing LLT decomposition..." << endl;
  19. llt.compute(A);
  20. cout << "The solution is now:\n" << llt.solve(b) << endl;
  21. }
Here is the matrix A:
 2 -1
-1  3
Here is the right hand side b:
1 2
3 1
Computing LLT decomposition...
The solution is:
1.2 1.4
1.4 0.8
The matrix A is now:
 2 -1
-1  4
Computing LLT decomposition...
The solution is now:
    1  1.29
    1 0.571

最终,你可以告诉分解的构造函数去预先分配一个给定尺寸矩阵的内存,这样之后分解矩阵时就不会执行动态内存分配了(当矩阵是固定尺寸时,根本不会有动态内存分配这回事),可以通过传递给分解的构造函数尺寸作为参数来实现,比如

  1. HouseholderQR<MatrixXf> qr(50,50);
  2. MatrixXf A = MatrixXf::Random(50,50);
  3. qr.compute(A); // no dynamic memory allocation
















声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/78194
推荐阅读
相关标签
  

闽ICP备14008679号