简介
这里介绍线性系统的解析,如何进行各种分解计算,如LU,QR,SVD,特征值分解等。
简单线性求解
在一个线性系统,常如下表示,其中A,b分别是一个矩阵,需要求x:
A x = b Ax \:= \: bAx=b
在Eigen中,我们可以根据需要的精确性要求,性能要求,从而从好几种方法中选择一种来求解这个线性系统。
下面的示例,可以看到一种求解方法。
//matrxi_decomp1.cpp
#include
#include
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 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
-
-<