Eigen 线性问题求解(最小二乘等)

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

基本的线性求解

问题:假设有一个系统方程写成如下矩阵的形式
Ax=b
其中A,b是矩阵,b也可以是向量,当想要求解x时,可以选择多种分解方式,取决于矩阵A的形式以及考虑的速度和精度,下面是一个简单的例子。

#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;
}

求解为:

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的对象,因此那句话也可以写成

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

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

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

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

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚餐男孩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值