1. //
  2. // Created by qian on 19-7-16.
  3. //
  4.  
  5. /* 相机位姿用四元数表示 q = [0.35, 0.2, 0.3, 0.1] x,y,z,w
  6. * 注意:输入时Quaterniond(w,x,y,z) W 在前!!!
  7. * 实现:输出四元素对应的旋转矩阵,旋转矩阵的转置,
  8. * 旋转矩阵的逆矩阵,旋转矩阵乘以自身的转置,验证旋转矩阵的正交性
  9. * Vector3.normalized的特点是当前向量是不改变的并且返回一个新的规范化的向量;
  10. * Vector3.Normalize的特点是改变当前向量,也就是当前向量长度是1
  11. */
  12.  
  13. #include <iostream>
  14.  
  15. using namespace std;
  16.  
  17. #include <ctime>
  18. // Eigen 核心部分
  19. #include <Eigen/Core>
  20. // 稠密矩阵的代数运算(逆,特征值等)
  21. #include <Eigen/Dense>
  22. #include <iomanip>
  23. using namespace Eigen;
  24.  
  25. #define MATRIX_SIZE 50
  26.  
  27. /****************************
  28. * 本程序演示了 Eigen 基本类型的使用
  29. ****************************/
  30.  
  31. int main(int argc, char **argv) {
  32.  
  33. Quaterniond q = Quaterniond(0.1, 0.35, 0.2, 0.3).normalized();//初始化四元数,并归一化
  34. cout << "归一化后的四元素矩阵:"<<endl << q.x()<<" "<<q.y()<<" "<<q.z()<<" "<<q.w()<< endl;
  35. Matrix3d matrix_T = q.toRotationMatrix();
  36. cout << "四元数的旋转矩阵:" << endl << matrix_T << endl;
  37. Matrix3d matrix_transposeT = matrix_T.transpose();
  38. cout << "旋转矩阵的转置:" << endl << matrix_transposeT << endl;
  39. Matrix3d matrix_invT = matrix_T.inverse();
  40. cout << "旋转矩阵的逆矩阵:" << endl << matrix_invT << endl;
  41. Matrix3d matrix_T1 = matrix_T * matrix_transposeT;
  42. cout << "旋转矩阵乘以自身的转置:" << endl << matrix_T1 << endl;
  43. // 验证旋转矩阵的正交性用定义:直接计算 AA^T, 若 等于单位矩阵E, 就是正交矩阵
  44. cout.setf(ios::fixed);//用定点格式显示浮点数;
  45. cout << "验证旋转矩阵的正交性:" << endl
  46. << fixed << setprecision() << matrix_T1<< endl;//setprecision(5):显示小数点后5位
  47. cout.unsetf(ios::fixed);//关闭
  48. cout << "matrix_T * matrix_transposeT 是单位矩阵,即旋转矩阵是正交矩阵";
  49.  
  50. // Eigen 中所有向量和矩阵都是Eigen::Matrix,它是一个模板类。它的前三个参数为:数据类型,行,列
  51. // 声明一个2*3的float矩阵
  52. Matrix<float, , > matrix_23;
  53.  
  54. // 同时,Eigen 通过 typedef 提供了许多内置类型,不过底层仍是Eigen::Matrix
  55. // 例如 Vector3d 实质上是 Eigen::Matrix<double, 3, 1>,即三维向量
  56. Vector3d v_3d;
  57. // 这是一样的
  58. Matrix<float, , > vd_3d;
  59.  
  60. // Matrix3d 实质上是 Eigen::Matrix<double, 3, 3>
  61. Matrix3d matrix_33 = Matrix3d::Zero(); //初始化为零
  62. // 如果不确定矩阵大小,可以使用动态大小的矩阵
  63. Matrix<double, Dynamic, Dynamic> matrix_dynamic;
  64. // 更简单的
  65. MatrixXd matrix_x;
  66. // 这种类型还有很多,我们不一一列举
  67.  
  68. // 下面是对Eigen阵的操作
  69. // 输入数据(初始化)
  70. matrix_23 << , , , , , ;
  71. // 输出
  72. cout << "matrix 2x3 from 1 to 6: \n" << matrix_23 << endl;
  73.  
  74. // 用()访问矩阵中的元素
  75. cout << "print matrix 2x3: " << endl;
  76. for (int i = ; i < ; i++) {
  77. for (int j = ; j < ; j++) cout << matrix_23(i, j) << "\t";
  78. cout << endl;
  79. }
  80.  
  81. // 矩阵和向量相乘(实际上仍是矩阵和矩阵)
  82. v_3d << , , ;
  83. vd_3d << , , ;
  84.  
  85. // 但是在Eigen里你不能混合两种不同类型的矩阵,像这样是错的
  86. // Matrix<double, 2, 1> result_wrong_type = matrix_23 * v_3d;
  87. // 应该显式转换
  88. Matrix<double, , > result = matrix_23.cast<double>() * v_3d;
  89. cout << "[1,2,3;4,5,6]*[3,2,1]=" << result.transpose() << endl;
  90.  
  91. Matrix<float, , > result2 = matrix_23 * vd_3d;
  92. cout << "[1,2,3;4,5,6]*[4,5,6]: " << result2.transpose() << endl;
  93.  
  94. // 同样你不能搞错矩阵的维度
  95. // 试着取消下面的注释,看看Eigen会报什么错
  96. // Eigen::Matrix<double, 2, 3> result_wrong_dimension = matrix_23.cast<double>() * v_3d;
  97.  
  98. // 一些矩阵运算
  99. // 四则运算就不演示了,直接用+-*/即可。
  100. matrix_33 = Matrix3d::Random(); // 随机数矩阵
  101. cout << "random matrix: \n" << matrix_33 << endl;
  102. cout << "transpose: \n" << matrix_33.transpose() << endl; // 转置
  103. cout << "sum: " << matrix_33.sum() << endl; // 各元素和
  104. cout << "trace: " << matrix_33.trace() << endl; // 迹
  105. cout << "times 10: \n" << * matrix_33 << endl; // 数乘
  106. cout << "inverse: \n" << matrix_33.inverse() << endl; // 逆
  107. cout << "det: " << matrix_33.determinant() << endl; // 行列式
  108.  
  109. // 特征值
  110. // 实对称矩阵可以保证对角化成功
  111. SelfAdjointEigenSolver<Matrix3d> eigen_solver(matrix_33.transpose() * matrix_33);
  112. cout << "Eigen values = \n" << eigen_solver.eigenvalues() << endl;
  113. cout << "Eigen vectors = \n" << eigen_solver.eigenvectors() << endl;
  114.  
  115. // 解方程
  116. // 我们求解 matrix_NN * x = v_Nd 这个方程
  117. // N的大小在前边的宏里定义,它由随机数生成
  118. // 直接求逆自然是最直接的,但是求逆运算量大
  119.  
  120. Matrix<double, MATRIX_SIZE, MATRIX_SIZE> matrix_NN
  121. = MatrixXd::Random(MATRIX_SIZE, MATRIX_SIZE);
  122. matrix_NN = matrix_NN * matrix_NN.transpose(); // 保证半正定
  123. Matrix<double, MATRIX_SIZE, > v_Nd = MatrixXd::Random(MATRIX_SIZE, );
  124.  
  125. clock_t time_stt = clock(); // 计时
  126. // 直接求逆
  127. Matrix<double, MATRIX_SIZE, > x = matrix_NN.inverse() * v_Nd;
  128. cout << "time of normal inverse is "
  129. << * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
  130. cout << "x = " << x.transpose() << endl;
  131.  
  132. // 通常用矩阵分解来求,例如QR分解,速度会快很多
  133. time_stt = clock();
  134. x = matrix_NN.colPivHouseholderQr().solve(v_Nd);
  135. cout << "time of Qr decomposition is "
  136. << * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
  137. cout << "x = " << x.transpose() << endl;
  138.  
  139. // 对于正定矩阵,还可以用cholesky分解来解方程
  140. time_stt = clock();
  141. x = matrix_NN.ldlt().solve(v_Nd);
  142. cout << "time of ldlt decomposition is "
  143. << * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
  144. cout << "x = " << x.transpose() << endl;
  145.  
  146. return ;
  147. }

Eigen几何模块

  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. #include <Eigen/Core>
  7. #include <Eigen/Geometry>
  8.  
  9. using namespace Eigen;
  10.  
  11. // 本程序演示了 Eigen 几何模块的使用方法
  12.  
  13. int main(int argc, char **argv) {
  14.  
  15. // Eigen/Geometry 模块提供了各种旋转和平移的表示
  16. // 3D 旋转矩阵直接使用 Matrix3d 或 Matrix3f
  17. Matrix3d rotation_matrix = Matrix3d::Identity();
  18. // 旋转向量使用 AngleAxis, 它底层不直接是Matrix,但运算可以当作矩阵(因为重载了运算符)
  19. AngleAxisd rotation_vector(M_PI / , Vector3d(, , )); //沿 Z 轴旋转 45 度
  20. cout.precision();
  21. cout << "rotation matrix =\n" << rotation_vector.matrix() << endl; //用matrix()转换成矩阵
  22. // 也可以直接赋值
  23. rotation_matrix = rotation_vector.toRotationMatrix();
  24. // 用 AngleAxis 可以进行坐标变换
  25. Vector3d v(, , );
  26. Vector3d v_rotated = rotation_vector * v;
  27. cout << "(1,0,0) after rotation (by angle axis) = " << v_rotated.transpose() << endl;
  28. // 或者用旋转矩阵
  29. v_rotated = rotation_matrix * v;
  30. cout << "(1,0,0) after rotation (by matrix) = " << v_rotated.transpose() << endl;
  31.  
  32. // 欧拉角: 可以将旋转矩阵直接转换成欧拉角
  33. Vector3d euler_angles = rotation_matrix.eulerAngles(, , ); // ZYX顺序,即roll pitch yaw顺序
  34. cout << "yaw pitch roll = " << euler_angles.transpose() << endl;
  35.  
  36. // 欧氏变换矩阵使用 Eigen::Isometry
  37. Isometry3d T = Isometry3d::Identity(); // 虽然称为3d,实质上是4*4的矩阵
  38. T.rotate(rotation_vector); // 按照rotation_vector进行旋转
  39. T.pretranslate(Vector3d(, , )); // 把平移向量设成(1,3,4)
  40. cout << "Transform matrix = \n" << T.matrix() << endl;
  41.  
  42. // 用变换矩阵进行坐标变换
  43. Vector3d v_transformed = T * v; // 相当于R*v+t
  44. cout << "v tranformed = " << v_transformed.transpose() << endl;
  45.  
  46. // 对于仿射和射影变换,使用 Eigen::Affine3d 和 Eigen::Projective3d 即可,略
  47.  
  48. // 四元数
  49. // 可以直接把AngleAxis赋值给四元数,反之亦然
  50. Quaterniond q = Quaterniond(rotation_vector);
  51. cout << "quaternion from rotation vector = " << q.coeffs().transpose()
  52. << endl; // 请注意coeffs的顺序是(x,y,z,w),w为实部,前三者为虚部
  53. // 也可以把旋转矩阵赋给它
  54. q = Quaterniond(rotation_matrix);
  55. cout << "quaternion from rotation matrix = " << q.coeffs().transpose() << endl;
  56. // 使用四元数旋转一个向量,使用重载的乘法即可
  57. v_rotated = q * v; // 注意数学上是qvq^{-1}
  58. cout << "(1,0,0) after rotation = " << v_rotated.transpose() << endl;
  59. // 用常规向量乘法表示,则应该如下计算
  60. cout << "should be equal to " << (q * Quaterniond(, , , ) * q.inverse()).coeffs().transpose() << endl;
  61.  
  62. return ;
  63. }
  1. cmake_minimum_required(VERSION 3.14)
  2. project(SlamPractice)
  3.  
  4. #添加头文件
  5. INCLUDE_DIRECTORIES(“usr/include/eigen3”)
  6. set(CMAKE_CXX_STANDARD )
  7.  
  8. add_executable(SlamPractice main.cpp practice2.cpp)

eigen矩阵操作练习的更多相关文章

  1. Eigen 矩阵库学习笔记

    最近为了在C++中使用矩阵运算,简单学习了一下Eigen矩阵库.Eigen比Armadillo相对底层一点,但是只需要添加头文库即可使用,不使用额外的编译和安装过程. 基本定义 Matrix3f是3* ...

  2. Linear regression with one variable算法实例讲解(绘制图像,cost_Function ,Gradient Desent, 拟合曲线, 轮廓图绘制)_矩阵操作

    %测试数据 'ex1data1.txt', 第一列为 population of City in 10,000s, 第二列为 Profit in $10,000s 1 6.1101,17.592 5. ...

  3. iOS开发UI篇—Quartz2D使用(矩阵操作)

    iOS开发UI篇—Quartz2D使用(矩阵操作) 一.关于矩阵操作 1.画一个四边形 通过设置两个端点(长和宽)来完成一个四边形的绘制. 代码: - (void)drawRect:(CGRect)r ...

  4. 【iOS】Quartz2D矩阵操作

    前面画基本图形时,画四边形是由几条直线拼接成的,现在有更简便的方法. 一.关于矩阵操作 1.画一个四边形 通过设置两个端点(长和宽)来完成一个四边形的绘制. 代码: - (void)drawRect: ...

  5. C++实现离散余弦变换(参数为Eigen矩阵)

    C++实现离散余弦变换(参数为Eigen矩阵) 问题描述 昨天写了一个参数为二维指针为参数的离散余弦变换,虽然改进了参数为二维数组时,当数组大小不确定时声明函数时带来的困难,但使用指针作为参数也存在一 ...

  6. MATLAB命令大全和矩阵操作大全

    转载自: http://blog.csdn.net/dengjianqiang2011/article/details/8753807 MATLAB矩阵操作大全 一.矩阵的表示在MATLAB中创建矩阵 ...

  7. Matlab、R向量与矩阵操作 z

    已有 1849 次阅读 2012-8-2 15:15 |系统分类:科研笔记|关键词:矩阵 480 window border center Matlab.R向量与矩阵操作   描    述 Matla ...

  8. Python中的矩阵操作

    Numpy 通过观察Python的自有数据类型,我们可以发现Python原生并不提供多维数组的操作,那么为了处理矩阵,就需要使用第三方提供的相关的包. NumPy 是一个非常优秀的提供矩阵操作的包.N ...

  9. poj3735—Training little cats(特殊操作转化为矩阵操作)

    题目链接:http://poj.org/problem?id=3735 题目意思: 调教猫咪:有n只饥渴的猫咪,现有一组羞耻连续操作,由k个操作组成,全部选自: 1. g i 给第i只猫咪一颗花生 2 ...

随机推荐

  1. spring boot 结合jsp简单示例

    引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  2. mssql查询表在哪个数据库中

    mssql查询表在哪个数据库中 EXEC sp_MSforeachdb @command1='IF object_id(''?'' + ''..表名'') IS NOT NULL PRINT ''?' ...

  3. 04E: Sub-process /usr/bin/dpkg returned an error code (1)

  4. 通过Python SDK 获取tushare数据

    导入tushare import tushare as ts 这里注意, tushare版本需大于1.2.10 设置token ts.set_token('your token here') 以上方法 ...

  5. UVA Ananagrams /// map set

    https://vjudge.net/problem/UVA-156 题目大意: 输入文本,找出所有满足条件的单词——该单词不能通过字母重排而得到输入的文本中的另外一个单词. 在判断是否满足条件时,字 ...

  6. LinkedHashMap+Spring Aop实现简易的缓存系统

    之前介绍说要做在线文库的系统,当数据量大的时候,根据标签tag的对文档信息的查询将是一个很耗时的工作,原来分析LinkedHashMap源码的时候了解到它有一个双向链表的结构,可以通过将刚被访问的元素 ...

  7. mysql tar安装模式

    mysql解压版安装过程,之前安装mysql一直用linux yum和rpm方式.今天试了下tar包方式有点麻烦记录下1.安装lrzsz-0.12.20-27.1.el6.x86_64.rpm方便操作 ...

  8. luaj使用 方法签名规则 Cocos2dxLuaJavaBridge

    function AndroidHandler:getParamJson()     local args = {nil}     local ok,ret = luaj.callStaticMeth ...

  9. .Net Core 从MySql数据库生成实体类 Entity Model

    1.首先建测试库 2.新建一个.Net Core 项目 3. cd到项目里面执行命令: dotnet add package MySql.Data.EntityFrameworkCore 4.继续执行 ...

  10. AM8互联设置方法

    Am8互联设置 这个只需要部署在一个总部的AM8的 Oiorg所在机器上就可以 环境: Windows 2012 or windows 2008,IIS ,.Net4 AM8 数据库必须升级到:201 ...