简介
这里介绍线性系统的解析,如何进行各种分解计算,如LU,QR,SVD,特征值分解等。
简单线性求解
在一个线性系统,常如下表示,其中A,b分别是一个矩阵,需要求x:
A x = b Ax \:= \: b Ax=b
在Eigen中,我们可以根据需要的精确性要求,性能要求,从而从好几种方法中选择一种来求解这个线性系统。
下面的示例,可以看到一种求解方法。
//matrxi_decomp1.cpp
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
Matrix3f A;
Vector3f b;
A << 1,2,3, 4,5,6, 7,8,10;
b << 3, 3, 4;
cout << "Here is the matrix A:\n" << A << endl;
cout << "Here is the vector b:\n" << b << endl;
Vector3f x = A.colPivHouseholderQr().solve(b);
cout << "The solution is:\n" << x << endl;
}
执行:
$ g++ -I /usr/local/include/eigen3 matrxi_decomp1.cpp -o matrxi_decomp1
$
$ ./matrxi_decomp1
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
0.999997
1
这个示例中,使用矩阵的colPivHouseholderQr()方法,其返回一个ColPivHouseholderQR实例对象。因为调用对象是一个Matrix3f类型,所以也可以如此设计
ColPivHouseholderQR<Matrix3f> dec(A);
Vector3f x = dec.solve(b);
正如名字所示,这里ColPivHouseholderQR是一个采用列旋转方法的QR分解。下面Eigen提供的表列出了你可以使用分解类型:
| Decomposition | Method | Requirements on the matrix | Speed(small-to-medium) | Speed(large) | Accuracy |
|---|---|---|---|---|---|
| PartialPivLU | partialPivLu() | Invertible(可逆) | ++ | ++ | + |
| FullPivLU | fullPivLu() | None | - | - - | +++ |
| HouseholderQR | householderQr() | None | ++ | ++ | + |
| ColPivHouseholderQR | colPivHouseholderQr() | None | + | - | +++ |
| FullPivHouseholderQR | fullPivHouseholderQr() | None | - | - - | +++ |
| CompleteOrthogonalDecomposition | completeOrthogonalDecomposition() | None | + | - | +++ |
| LLT | llt() | Positive definite(正定) | +++ | +++ | + |
| LDLT | ldlt() | Positive or negative semidefinite | +++ | + | ++ |
| BDCSVD | bdcSvd() | None | - | - | +++ |
| JacobiSVD | jacobiSvd() | None | - | - - - | +++ |
其中的部分分解公式:
-
FullPivLU:
A = P − 1 L U Q − 1 A = P^{-1} L U Q^{-1} A=P−1LUQ−1 -
ColPivHouseholderQR:
A P = Q R \mathbf{A} \, \mathbf{P} = \mathbf{Q} \, \mathbf{R} AP=QR -
Full

本文深入探讨线性系统的解析方法,包括LU、QR、SVD等分解计算,以及特征值分解。通过示例代码展示了如何使用Eigen库解决线性方程组,计算特征值与特征向量,求解最小二乘问题,以及矩阵的秩分解。
最低0.47元/天 解锁文章
2189

被折叠的 条评论
为什么被折叠?



