CH3 实践 Eigen库
模型 | 头文件 | 说明 |
---|---|---|
Core | #include <Eigen/Core> | 包含Matrix和Array类,基础的线性代数运算和数组操作。 |
Geometry | #include <Eigen/Geometry> | 包括平移、缩放、2D\3D旋转变换、四元数、轴旋转变换 |
LU | #include <Eigen/LU> | 包含求逆,行列式,LU分解 |
Cholesky | #include <Eigen/Cholesky> | 包含LLT和LDLT Cholesky分解 |
Householder | #include <Eigen/Householder> | householder变换,此模块由几个线性代数模块使用 |
SVD | #include <Eigen/SVD> | 奇异值分解 |
QR | #include <Eigen/QR> | QR分解 |
Eigenvalues | #include <Eigen/Eigenvalues> | 特征值、特征向量 |
Sparse | #include <Eigen/Sparse> | 稀疏矩阵存储及相关的基本线性代数 |
* | #include <Eigen/Eigen> | 包含Dense和Sparse头文件(Eigen全部库) |
矩阵类型
基本定义:Matrix<数据类型, 行数, 列数>
Eigen 通过 typedef 提供了许多内置类型
typedef Matrix<double,Dynamic,Dynamic> MatrixXd;
typedef Matrix<double, 3, 1> Vector3d
typedef Matrix<double, 3, 3> Matrix3d
矩阵的行列数可以是固定的也可以是动态的,能够确定大小的矩阵事先指定行列数处理起来会更快一些。
Eigen矩阵不支持自动类型转化,对两个不同类型的矩阵进行操作时,须手动转化
Matrix<double,2,1>result=matrix_23.cast<double>()*v_3d;
矩阵赋值
用“<<”操作符给矩阵赋值,从上到下从左到右
m1 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
分块赋值
int rows=5;
int cols=5;
MatrixXf m(rows, cols);
m << (Matrix3f() << 1, 2, 3, 4, 5, 6, 7, 8, 9).finished(),
MatrixXf::Zero(3, cols - 3),
MatrixXf::Zero(rows - 3, 3),
MatrixXf::Identity(rows - 3, cols - 3); // Zero()、Identity() 分别是0矩阵和单位矩阵。
cout << m << endl
输出结果
[
1
2
3
0
0
4
5
6
0
0
7
8
9
0
0
0
0
0
1
0
0
0
0
0
1
]
\begin{bmatrix}1&2&3&0&0\\4&5&6&0&0\\7&8&9&0&0\\0&0&0&1&0\\0&0&0&0&1\end{bmatrix}
⎣⎢⎢⎢⎢⎡1470025800369000001000001⎦⎥⎥⎥⎥⎤
矩阵运算
常见的四则运算等直接通过符号实现即可
转置:mymatrix.transpose()
迹:mymatrix.trace()
行列式:mymatrix.determinant()
逆:mymatrix.inverse()
特征值:mymatrix.eigenvalues()
特征向量:mymatrix.eigenvectors()
Eigen 几何模块 <Eigen/Geometry>
几何模块中提供了各种平移和旋转的表示
例程:
#include <iostream>
#include <cmath>
#include <Eigen/Core>
#include <Eigen/Geometry>
using namespace std;
using namespace Eigen;
// 本程序演示了 Eigen 几何模块的使用方法
int main(int argc, char **argv) {
// Eigen/Geometry 模块提供了各种旋转和平移的表示
// 3D 旋转矩阵直接使用 Matrix3d 或 Matrix3f
Matrix3d rotation_matrix = Matrix3d::Identity();
// 旋转向量使用 AngleAxis, 它底层不直接是Matrix,但运算可以当作矩阵(因为重载了运算符)
AngleAxisd rotation_vector(M_PI / 4, Vector3d(0, 0, 1)); //沿 Z 轴旋转 45 度
cout.precision(3); // 输出的浮点类型保留三位小数
cout << "rotation matrix =\n" << rotation_vector.matrix() << endl; //用matrix()转换成矩阵
// 也可以直接赋值
rotation_matrix = rotation_vector.toRotationMatrix();
// 用 AngleAxis 可以进行坐标变换
Vector3d v(1, 0, 0);
Vector3d v_rotated = rotation_vector * v;
cout << "(1,0,0) after rotation (by angle axis) = " << v_rotated.transpose() << endl;
// 或者用旋转矩阵
v_rotated = rotation_matrix * v;
cout << "(1,0,0) after rotation (by matrix) = " << v_rotated.transpose() << endl;
// 欧拉角: 可以将旋转矩阵直接转换成欧拉角
Vector3d euler_angles = rotation_matrix.eulerAngles(2, 1, 0); // ZYX顺序,即roll pitch yaw顺序
cout << "yaw pitch roll = " << euler_angles.transpose() << endl;
// 欧氏变换矩阵使用 Eigen::Isometry
Isometry3d T = Isometry3d::Identity(); // 虽然称为3d,实质上是4*4的矩阵
T.rotate(rotation_vector); // 按照rotation_vector进行旋转
T.pretranslate(Vector3d(1, 3, 4)); // 把平移向量设成(1,3,4)
cout << "Transform matrix = \n" << T.matrix() << endl;
// 用变换矩阵进行坐标变换
Vector3d v_transformed = T * v; // 相当于R*v+t
cout << "v tranformed = " << v_transformed.transpose() << endl;
// 对于仿射和射影变换,使用 Eigen::Affine3d 和 Eigen::Projective3d 即可,略
// 四元数
// 可以直接把AngleAxis赋值给四元数,反之亦然
Quaterniond q = Quaterniond(rotation_vector);
cout << "quaternion from rotation vector = " << q.coeffs().transpose()
<< endl; // 请注意coeffs的顺序是(x,y,z,w),w为实部,前三者为虚部
// 也可以把旋转矩阵赋给它
q = Quaterniond(rotation_matrix);
cout << "quaternion from rotation matrix = " << q.coeffs().transpose() << endl;
// 使用四元数旋转一个向量,使用重载的乘法即可
v_rotated = q * v; // 注意数学上是qvq^{-1}
cout << "(1,0,0) after rotation = " << v_rotated.transpose() << endl;
return 0;
}
v_rotated = q * v; // 注意数学上是qvq^{-1}
cout << "(1,0,0) after rotation = " << v_rotated.transpose() << endl;
return 0;
}