Eigen密集矩阵求解 1 - 线性代数及矩阵分解

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

简介

这里介绍线性系统的解析,如何进行各种分解计算,如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=P1LUQ1

  • ColPivHouseholderQR:
    A   P = Q   R \mathbf{A} \, \mathbf{P} = \mathbf{Q} \, \mathbf{R} AP=QR

  • Full

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值