slam十四讲:ch3之三维空间刚体运动和eigen事件总结

[复制链接]
 楼主| keer_zu 发表于 2022-8-26 09:44 | 显示全部楼层 |阅读模式

总结:

86070630825757183a.jpg



eigen代码:
  1. #include <iostream>

  2. using namespace std;

  3. #include <ctime>
  4. // Eigen 核心部分
  5. #include <Eigen/Core>
  6. // 稠密矩阵的代数运算(逆,特征值等)
  7. #include <Eigen/Dense>

  8. using namespace Eigen;

  9. #define MATRIX_SIZE 50

  10. /****************************
  11. * 本程序演示了 Eigen 基本类型的使用
  12. ****************************/

  13. int main(int argc, char **argv) {
  14.   // Eigen 中所有向量和矩阵都是Eigen::Matrix,它是一个模板类。它的前三个参数为:数据类型,行,列
  15.   // 声明一个2*3的float矩阵
  16.   Matrix<float, 2, 3> matrix_23;

  17.   // 同时,Eigen 通过 typedef 提供了许多内置类型,不过底层仍是Eigen::Matrix
  18.   // 例如 Vector3d 实质上是 Eigen::Matrix<double, 3, 1>,即三维向量
  19.   Vector3d v_3d;
  20.   // 这是一样的
  21.   Matrix<float, 3, 1> vd_3d;

  22.   // Matrix3d 实质上是 Eigen::Matrix<double, 3, 3>
  23.   Matrix3d matrix_33 = Matrix3d::Zero(); //初始化为零
  24.   // 如果不确定矩阵大小,可以使用动态大小的矩阵
  25.   Matrix<double, Dynamic, Dynamic> matrix_dynamic;
  26.   // 更简单的
  27.   MatrixXd matrix_x;
  28.   // 这种类型还有很多,我们不一一列举

  29.   // 下面是对Eigen阵的操作
  30.   // 输入数据(初始化)
  31.   matrix_23 << 1, 2, 3, 4, 5, 6;
  32.   // 输出
  33.   cout << "matrix 2x3 from 1 to 6: \n" << matrix_23 << endl;

  34.   // 用()访问矩阵中的元素
  35.   cout << "print matrix 2x3: " << endl;
  36.   for (int i = 0; i < 2; i++) {
  37.     for (int j = 0; j < 3; j++) cout << matrix_23(i, j) << "\t";
  38.     cout << endl;
  39.   }

  40.   // 矩阵和向量相乘(实际上仍是矩阵和矩阵)
  41.   v_3d << 3, 2, 1;
  42.   vd_3d << 4, 5, 6;

  43.   // 但是在Eigen里你不能混合两种不同类型的矩阵,像这样是错的
  44.   // Matrix<double, 2, 1> result_wrong_type = matrix_23 * v_3d;
  45.   // 应该显式转换
  46.   Matrix<double, 2, 1> result = matrix_23.cast<double>() * v_3d;
  47.   cout << "[1,2,3;4,5,6]*[3,2,1]=" << result.transpose() << endl;

  48.   Matrix<float, 2, 1> result2 = matrix_23 * vd_3d;
  49.   cout << "[1,2,3;4,5,6]*[4,5,6]: " << result2.transpose() << endl;

  50.   // 同样你不能搞错矩阵的维度
  51.   // 试着取消下面的注释,看看Eigen会报什么错
  52.   // Eigen::Matrix<double, 2, 3> result_wrong_dimension = matrix_23.cast<double>() * v_3d;

  53.   // 一些矩阵运算
  54.   // 四则运算就不演示了,直接用+-*/即可。
  55.   matrix_33 = Matrix3d::Random();      // 随机数矩阵
  56.   cout << "random matrix: \n" << matrix_33 << endl;
  57.   cout << "transpose: \n" << matrix_33.transpose() << endl;      // 转置
  58.   cout << "sum: " << matrix_33.sum() << endl;            // 各元素和
  59.   cout << "trace: " << matrix_33.trace() << endl;          // 迹
  60.   cout << "times 10: \n" << 10 * matrix_33 << endl;               // 数乘
  61.   cout << "inverse: \n" << matrix_33.inverse() << endl;        // 逆
  62.   cout << "det: " << matrix_33.determinant() << endl;    // 行列式

  63.   // 特征值
  64.   // 实对称矩阵可以保证对角化成功
  65.   SelfAdjointEigenSolver<Matrix3d> eigen_solver(matrix_33.transpose() * matrix_33);
  66.   cout << "Eigen values = \n" << eigen_solver.eigenvalues() << endl;
  67.   cout << "Eigen vectors = \n" << eigen_solver.eigenvectors() << endl;

  68.   // 解方程
  69.   // 我们求解 matrix_NN * x = v_Nd 这个方程
  70.   // N的大小在前边的宏里定义,它由随机数生成
  71.   // 直接求逆自然是最直接的,但是求逆运算量大

  72.   Matrix<double, MATRIX_SIZE, MATRIX_SIZE> matrix_NN
  73.       = MatrixXd::Random(MATRIX_SIZE, MATRIX_SIZE);
  74.   matrix_NN = matrix_NN * matrix_NN.transpose();  // 保证半正定
  75.   Matrix<double, MATRIX_SIZE, 1> v_Nd = MatrixXd::Random(MATRIX_SIZE, 1);

  76.   clock_t time_stt = clock(); // 计时
  77.   // 直接求逆
  78.   Matrix<double, MATRIX_SIZE, 1> x = matrix_NN.inverse() * v_Nd;
  79.   cout << "time of normal inverse is "
  80.        << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
  81.   cout << "x = " << x.transpose() << endl;

  82.   // 通常用矩阵分解来求,例如QR分解,速度会快很多
  83.   time_stt = clock();
  84.   x = matrix_NN.colPivHouseholderQr().solve(v_Nd);
  85.   cout << "time of Qr decomposition is "
  86.        << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
  87.   cout << "x = " << x.transpose() << endl;

  88.   // 对于正定矩阵,还可以用cholesky分解来解方程
  89.   time_stt = clock();
  90.   x = matrix_NN.ldlt().solve(v_Nd);
  91.   cout << "time of ldlt decomposition is "
  92.        << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
  93.   cout << "x = " << x.transpose() << endl;

  94.   return 0;
  95. }



运行结果:
  1. matrix 2x3 from 1 to 6:
  2. 1 2 3
  3. 4 5 6
  4. print matrix 2x3:
  5. 1       2       3
  6. 4       5       6
  7. [1,2,3;4,5,6]*[3,2,1]=10 28
  8. [1,2,3;4,5,6]*[4,5,6]: 32 77
  9. random matrix:
  10. 0.680375   0.59688 -0.329554
  11. -0.211234  0.823295  0.536459
  12. 0.566198 -0.604897 -0.444451
  13. transpose:
  14. 0.680375 -0.211234  0.566198
  15.   0.59688  0.823295 -0.604897
  16. -0.329554  0.536459 -0.444451
  17. sum: 1.61307
  18. trace: 1.05922
  19. times 10:
  20. 6.80375   5.9688 -3.29554
  21. -2.11234  8.23295  5.36459
  22. 5.66198 -6.04897 -4.44451
  23. inverse:
  24. -0.198521   2.22739    2.8357
  25.   1.00605 -0.555135  -1.41603
  26. -1.62213   3.59308   3.28973
  27. det: 0.208598
  28. Eigen values =
  29. 0.0242899
  30. 0.992154
  31.   1.80558
  32. Eigen vectors =
  33. -0.549013 -0.735943  0.396198
  34. 0.253452 -0.598296 -0.760134
  35. -0.796459  0.316906 -0.514998
  36. time of normal inverse is 0ms
  37. x = -55.7896 -298.793  130.113 -388.455 -159.312  160.654 -40.0416 -193.561  155.844  181.144  185.125 -62.7786  19.8333 -30.8772 -200.746  55.8385 -206.604  26.3559 -14.6789  122.719 -221.449   26.233  -318.95 -78.6931  50.1446  87.1986 -194.922  132.319  -171.78 -4.19736   11.876 -171.779  48.3047  84.1812 -104.958 -47.2103 -57.4502 -48.9477 -19.4237  28.9419  111.421  92.1237 -288.248 -23.3478  -275.22 -292.062  -92.698  5.96847 -93.6244  109.734
  38. time of Qr decomposition is 0ms
  39. x = -55.7896 -298.793  130.113 -388.455 -159.312  160.654 -40.0416 -193.561  155.844  181.144  185.125 -62.7786  19.8333 -30.8772 -200.746  55.8385 -206.604  26.3559 -14.6789  122.719 -221.449   26.233  -318.95 -78.6931  50.1446  87.1986 -194.922  132.319  -171.78 -4.19736   11.876 -171.779  48.3047  84.1812 -104.958 -47.2103 -57.4502 -48.9477 -19.4237  28.9419  111.421  92.1237 -288.248 -23.3478  -275.22 -292.062  -92.698  5.96847 -93.6244  109.734
  40. time of ldlt decomposition is 10ms
  41. x = -55.7896 -298.793  130.113 -388.455 -159.312  160.654 -40.0416 -193.561  155.844  181.144  185.125 -62.7786  19.8333 -30.8772 -200.746  55.8385 -206.604  26.3559 -14.6789  122.719 -221.449   26.233  -318.95 -78.6931  50.1446  87.1986 -194.922  132.319  -171.78 -4.19736   11.876 -171.779  48.3047  84.1812 -104.958 -47.2103 -57.4502 -48.9477 -19.4237  28.9419  111.421  92.1237 -288.248 -23.3478  -275.22 -292.062  -92.698  5.96847 -93.6244  109.734


您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1478

主题

12917

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1478

主题

12917

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部