Eigen是一个 C++ 开源线性代数库。它提供了快速的有关矩阵的线性代数运算,还包括解方程等功能。
可以通过sudo apt install libeigen3-dev命令进行安装,也可以去官网下载需要的版本进行安装。
若出现错误:Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable) E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
依次输入以下命令,操作成功没有显示,重新进行安装即可。
sudo rm /var/lib/dpkg/lock-frontend sudo rm /var/lib/dpkg/lock sudo apt install libeigen3-devEigen 头文件的默认位置在“/usr/include/eigen3/” 中,因此在程序中导入包的时候,应该是eigen3/Eigen
Eigen库的引用
#include <eigen3/Eigen/Core> #include <eigen3/Eigen/Dense>Eigen 以矩阵为基本数据单元。它是一个模板类。它的前三个参数为:数据类型,行,列,Eigen 通过 typedef 提供了许多内置类型,还有int,double等等,例如声明一个 2*3 的 float 矩阵。
Eigen::Matrix<float, 2, 3> matrix_23;还有Vector3d类型,它 实质上是 Eigen::Matrix<double, 3, 1>
Eigen::Vector3d v_3d;矩阵的初始化操作:
Eigen::Matrix3d matrix_33 = Eigen::Matrix3d::Zero(); //初始化为零不明确矩阵大小时,采用动态初始化:
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > matrix_dynamic;数据填充:
输入数据(初始化)
matrix_23 << 1, 2, 3, 4, 5, 6;输出
cout << "matrix 2x3 from 1 to 6: \n" << matrix_23 << endl;
for循环打印矩阵中的元素
cout << "print matrix 2x3: " << endl; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) cout << matrix_23(i, j) << "\t"; cout << endl; }矩阵乘法:
Matrix<double, 2, 1> result = matrix_23.cast<double>() * v_3d; cout << "[1,2,3;4,5,6]*[3,2,1]=" << result.transpose() << endl; Matrix<float, 2, 1> result2 = matrix_23 * vd_3d; cout << "[1,2,3;4,5,6]*[4,5,6]: " << result2.transpose() << endl; 随机数矩阵相关运算: matrix_33 = Matrix3d::Random(); // 随机数矩阵 cout << "random matrix: \n" << matrix_33 << endl; cout << "transpose: \n" << matrix_33.transpose() << endl; // 转置 cout << "sum: " << matrix_33.sum() << endl; // 各元素和 cout << "trace: " << matrix_33.trace() << endl; // 迹 cout << "times 10: \n" << 10 * matrix_33 << endl; // 数乘 cout << "inverse: \n" << matrix_33.inverse() << endl; // 逆 cout << "det: " << matrix_33.determinant() << endl; // 行列式特征值与特征向量:
SelfAdjointEigenSolver<Matrix3d> eigen_solver(matrix_33.transpose() * matrix_33); cout << "Eigen values = \n" << eigen_solver.eigenvalues() << endl; cout << "Eigen vectors = \n" << eigen_solver.eigenvectors() << endl;解方程,例如求解 matrix_NN * x = v_Nd 这个方程:
Matrix<double, MATRIX_SIZE, MATRIX_SIZE> matrix_NN = MatrixXd::Random(MATRIX_SIZE, MATRIX_SIZE); matrix_NN = matrix_NN * matrix_NN.transpose(); // 保证半正定 Matrix<double, MATRIX_SIZE, 1> v_Nd = MatrixXd::Random(MATRIX_SIZE, 1);①直接求逆(简单明了,但计算量大)
clock_t time_stt = clock(); // 计时 // 直接求逆 Matrix<double, MATRIX_SIZE, 1> x = matrix_NN.inverse() * v_Nd; cout << "time of normal inverse is " << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl; cout << "x = " << x.transpose() << endl;②矩阵分解来求,例如QR分解,速度会快很多
time_stt = clock(); x = matrix_NN.colPivHouseholderQr().solve(v_Nd); cout << "time of Qr decomposition is " << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl; cout << "x = " << x.transpose() << endl;③对于正定矩阵,还可以用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; 4.Eigen几何模块 // 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顺序,即yaw-pitch-roll顺序 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; // 用常规向量乘法表示,则应该如下计算 cout << "should be equal to " << (q * Quaterniond(0, 1, 0, 0) * q.inverse()).coeffs().transpose() << endl;