1.1Slam学习笔记

Eigen库的学习

本文学习来源主要基于—slam14讲

1基本的矩阵定义、赋值

1.1声明

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

//   // Matrix3d 实质上是 Eigen::Matrix<double, 3, 3>
//   Matrix3d matrix_33 = Matrix3d::Zero(); //初始化为零
//   // 如果不确定矩阵大小,可以使用动态大小的矩阵
//   Matrix<double, Dynamic, Dynamic> matrix_dynamic;
//   // 更简单的
//   MatrixXd matrix_x;

1.2赋值

matrix_23 <<1,2,3,7,8,9;
matrix_31 <<3,2,1;
v_3d <<3,2,1;
vd_3d <<4,5,6;

1.3遍历数据

cout <<"matrix_23 from 1 to 6:\n"<< matrix_23<<endl;
cout << "matrix_23  2*3 \n"<<endl;
     for(int i=0 ; i <2 ; i++)//i 需要加上类型说明
     {
          for(int j=0; j<3; j++)
              {
                    cout << matrix_23(i,j)<<"\t";
              }
     cout << endl;
     }

2矩阵的操作

2.1常规操作

//矩阵相乘
     cout << "矩阵格式匹配相乘\n"<<"right_resuit:matrix_23 *  matrix_31=\n"<< matrix_23 *  matrix_31<<"\n"<<endl; 
     // cout << "矩阵格式不匹配相乘\n"<<"error_resuit:matrix_23 *  v_3d=\n"<< matrix_23 *  v_3d<<"\n";      
     cout << "矩阵格式不匹配,修改后相乘\n"<<"vertify_resuit:matrix_23(转置成double) *  v_3d=\n"<< matrix_23.cast<double>()*  v_3d<<"\n"<<endl;      
  
  //矩阵转置
     Matrix<float, 2, 1>result1 = matrix_23 *  matrix_31;
     cout <<"result1= matrix_23 *  matrix_31\n"<<result1<<"\n"<<endl;
     Matrix<float, 1, 2>result2 = result1.transpose();  \
     cout <<"result2 = result1.transpose();\n"<<result2<<"\n"<<endl;  

// 矩阵运算   //转置// 各元素和// 迹// 数乘// 逆// 行列式
     Matrix<double, 3, 3> matrix_33 ;
     matrix_33 = Matrix3d::Random(); 
     cout << "matrix = \n "<< matrix_33 << endl;
     cout << "matrix_transpose =  "<< matrix_33.transpose() << "\n"<< endl;
     cout << "matrix_sum=  "<< matrix_33.sum() << "\n"<< endl;
     cout << "matrix_trace=  "<< matrix_33.trace() << "\n"<< endl;
     cout << "matrix_10=  "<< 10* matrix_33  << "\n"<< endl;
     cout << "matrix_inverse=  "<< matrix_33.inverse() << "\n"<< endl;
     cout << "matrix_determinant=  "<< matrix_33.determinant() << "\n"<< 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;//特征向量

2.2三种求解方程组的方法

  clock_t time_stt = clock(); // 计时
  // 直接求逆,使用了0.109s
  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分解讲解

  // 通常用矩阵分解来求,例如QR分解,速度会快很多,使用了0.056s
  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分解

  // 对于正定矩阵,还可以用cholesky分解来解方程  使用了0.019s
  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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值