Slam第三讲笔记(2)之Eigen库

一、Eigen库简介

Eigen是一个C++开源线性代数库,能快速地进行有关矩阵地线性代数运算、解方程等功能。它是一个用纯头文件搭建起来地库,你只能找到它地头文件,而没有动态链接库文件(.so)和静态链接库文件(.a)的二进制文件,使用时引入Eigen的头文件即可。

二、Linux 安装Eigen库

终端输入

sudo apt-get install libeigen3-dev

三、Eigen基本类型的使用

1.头文件

// Eigen 核心
#include <Eigen/Core>
//稠密矩阵的代数运算
#include <Eigen/Dense>
//命名空间
using namespace Eigen;

2. Matrix

是个模板类,前三个参数为数据类型、行、列,是Eigen中所有向量和矩阵都是使用Eigen::Matrix。

//1.申明一个2*3的float矩阵
Matrix<float,2,3> mat_23;

//2.输入数据
mat_23 << 1,2,3,4,5,6

//3.输出
cout << "matrix_23:" << mat_23 << endl;

//4.遍历、访问矩阵某个元素
for(int i = 0;i<2;i++){
	for(int j = 0;i<3;i++)
		cout << mat_23(i,j)<< "\t";
	cout<< endl;
}

2. Vector3d

三维向量,实质上是Eigen::Matrix<double,3,1>。

//1.申明一个2*3的float矩阵
Matrix<float,2,3> matrix_23;

//2.声明三维向量, vec_3d与Matirx<double,3,1> mat_3d相同.
Vector3d vec_3d;

//3.数据输入
matrix_23 << 1,2,3,4,5,6
vec_3d << 1,2,3;

//4.矩阵和向量相乘,(某种意义下,向量可以看作只有的一列的矩阵)
//Eigen不能混合使用不同类型的矩阵,需要显示转换: matrix_23.cast<double>()
Matrix<double,2,1> res = matrix_23.cast<double>() * v_3d;

//5.输出 矩阵转置
cout << "[1,2,3;4,5,6]*[1,2,3]" << res.transpose() <<endl; 

3. Matrix3d 与矩阵的运算函数

3 × 3 3\times3 3×3矩阵,实质上是Eigen::Matrix<double,3,3>。
矩阵的运算,以Matrix3d 类为例子,其他矩阵运算函数类似。

Matrix3d mat_33 = Martirx3d::Zero();//初始化为零
//1.矩阵运算,+-*/四则运算直接使用
mat_33 = Matrix3d::Random();//随机矩阵
cout << "transpose:" << mat_33.transpose() << endl; //转置
cout << "sum = " << mat_33.sum << endl; 			//各元素和
cout << "trace:" << mat_33.trace() << endl;			//迹,求矩阵的对角线元素之和
cout << "times 10: \n" << 10 * mat_33 << endl;		//数乘
cout << "inverse: \n" << mat_33.inverse() << endl;	//逆
cout << "det: " << mat_33.determinant() << endl;	//行列式

4. 特征值

实对称矩阵可以保证对角化成功

Matrix3d mat_33 = Matrix3d::Random();
SelfAdjointEigenSolver<Matrix3d> eigen_solver(mat_33.transpose() * mat_33);
cout << "Eigen values = \n" << eigen_solver.eigenvalues() << endl ;
cout << "Eigen vectors = \n" << eigen_solver.eigenvectors() << endl;

5. 解方程

#include <ctime> //计时头文件

Matrix<double,100,100> matrix_NN = MatirxXd::Random(100,100);
matrix_NN = matirx_NN * matrix_NN.transpose(); //保证半正定
Matrix<double,100,1> v_Nd = MatrixXd::Random(100,1);
clock_t time_stt = clock();//计时

//1. 直接求逆
Matrix<double,100,1> x = matrix_NN.inverse() * v_Nd;
cout << "time of normal inverser is"
	<<1000 * (clock() - time_stt) /(double)CLOCKS_PER_SEC << "ms" <<endl;
cout << "x = " << x.transpose() <<endl;

// 2. 常用的矩阵分解求解,例如 QR分解,速度会快很多
time_stt  = clock();
x = matrix_NN.colPivHouseholderQr().solve(v_Nd);
cout << "times of ldlt decomposition is"
	 << 1000 * (clock() - time_stt) / (double)CLOCKS_PER_SEC << "ms" << endl;
cout << "x = " << x.transpose() << endl;

//3. 正定矩阵,还可以用cholesky分解解方程
time_stt= clock();
x = matrix_NN.ldlt().solve(v_Nd);
cout << "time of ldlt decomposition is "
	<< 1000	* (clock() - time_stt) /(double) CLOCKS_PER_SEC << "ms" << endl;
cout << "x = " << x.transpose() << endl; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ai_Sj

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

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

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

打赏作者

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

抵扣说明:

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

余额充值