//
// Created by qian on 19-7-16.
// /* 相机位姿用四元数表示 q = [0.35, 0.2, 0.3, 0.1] x,y,z,w
* 注意:输入时Quaterniond(w,x,y,z) W 在前!!!
* 实现:输出四元素对应的旋转矩阵,旋转矩阵的转置,
* 旋转矩阵的逆矩阵,旋转矩阵乘以自身的转置,验证旋转矩阵的正交性
* Vector3.normalized的特点是当前向量是不改变的并且返回一个新的规范化的向量;
* Vector3.Normalize的特点是改变当前向量,也就是当前向量长度是1
*/ #include <iostream> using namespace std; #include <ctime>
// Eigen 核心部分
#include <Eigen/Core>
// 稠密矩阵的代数运算(逆,特征值等)
#include <Eigen/Dense>
#include <iomanip>
using namespace Eigen; #define MATRIX_SIZE 50 /****************************
* 本程序演示了 Eigen 基本类型的使用
****************************/ int main(int argc, char **argv) { Quaterniond q = Quaterniond(0.1, 0.35, 0.2, 0.3).normalized();//初始化四元数,并归一化
cout << "归一化后的四元素矩阵:"<<endl << q.x()<<" "<<q.y()<<" "<<q.z()<<" "<<q.w()<< endl;
Matrix3d matrix_T = q.toRotationMatrix();
cout << "四元数的旋转矩阵:" << endl << matrix_T << endl;
Matrix3d matrix_transposeT = matrix_T.transpose();
cout << "旋转矩阵的转置:" << endl << matrix_transposeT << endl;
Matrix3d matrix_invT = matrix_T.inverse();
cout << "旋转矩阵的逆矩阵:" << endl << matrix_invT << endl;
Matrix3d matrix_T1 = matrix_T * matrix_transposeT;
cout << "旋转矩阵乘以自身的转置:" << endl << matrix_T1 << endl;
// 验证旋转矩阵的正交性用定义:直接计算 AA^T, 若 等于单位矩阵E, 就是正交矩阵
cout.setf(ios::fixed);//用定点格式显示浮点数;
cout << "验证旋转矩阵的正交性:" << endl
<< fixed << setprecision() << matrix_T1<< endl;//setprecision(5):显示小数点后5位
cout.unsetf(ios::fixed);//关闭
cout << "matrix_T * matrix_transposeT 是单位矩阵,即旋转矩阵是正交矩阵"; // Eigen 中所有向量和矩阵都是Eigen::Matrix,它是一个模板类。它的前三个参数为:数据类型,行,列
// 声明一个2*3的float矩阵
Matrix<float, , > matrix_23; // 同时,Eigen 通过 typedef 提供了许多内置类型,不过底层仍是Eigen::Matrix
// 例如 Vector3d 实质上是 Eigen::Matrix<double, 3, 1>,即三维向量
Vector3d v_3d;
// 这是一样的
Matrix<float, , > vd_3d; // Matrix3d 实质上是 Eigen::Matrix<double, 3, 3>
Matrix3d matrix_33 = Matrix3d::Zero(); //初始化为零
// 如果不确定矩阵大小,可以使用动态大小的矩阵
Matrix<double, Dynamic, Dynamic> matrix_dynamic;
// 更简单的
MatrixXd matrix_x;
// 这种类型还有很多,我们不一一列举 // 下面是对Eigen阵的操作
// 输入数据(初始化)
matrix_23 << , , , , , ;
// 输出
cout << "matrix 2x3 from 1 to 6: \n" << matrix_23 << endl; // 用()访问矩阵中的元素
cout << "print matrix 2x3: " << endl;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) cout << matrix_23(i, j) << "\t";
cout << endl;
} // 矩阵和向量相乘(实际上仍是矩阵和矩阵)
v_3d << , , ;
vd_3d << , , ; // 但是在Eigen里你不能混合两种不同类型的矩阵,像这样是错的
// Matrix<double, 2, 1> result_wrong_type = matrix_23 * v_3d;
// 应该显式转换
Matrix<double, , > result = matrix_23.cast<double>() * v_3d;
cout << "[1,2,3;4,5,6]*[3,2,1]=" << result.transpose() << endl; Matrix<float, , > result2 = matrix_23 * vd_3d;
cout << "[1,2,3;4,5,6]*[4,5,6]: " << result2.transpose() << endl; // 同样你不能搞错矩阵的维度
// 试着取消下面的注释,看看Eigen会报什么错
// Eigen::Matrix<double, 2, 3> result_wrong_dimension = matrix_23.cast<double>() * v_3d; // 一些矩阵运算
// 四则运算就不演示了,直接用+-*/即可。
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" << * 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 这个方程
// N的大小在前边的宏里定义,它由随机数生成
// 直接求逆自然是最直接的,但是求逆运算量大 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, > v_Nd = MatrixXd::Random(MATRIX_SIZE, ); clock_t time_stt = clock(); // 计时
// 直接求逆
Matrix<double, MATRIX_SIZE, > x = matrix_NN.inverse() * v_Nd;
cout << "time of normal inverse is "
<< * (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 "
<< * (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 "
<< * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
cout << "x = " << x.transpose() << endl; return ;
}

Eigen几何模块

#include <iostream>
#include <cmath> using namespace std; #include <Eigen/Core>
#include <Eigen/Geometry> 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 / , Vector3d(, , )); //沿 Z 轴旋转 45 度
cout.precision();
cout << "rotation matrix =\n" << rotation_vector.matrix() << endl; //用matrix()转换成矩阵
// 也可以直接赋值
rotation_matrix = rotation_vector.toRotationMatrix();
// 用 AngleAxis 可以进行坐标变换
Vector3d v(, , );
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(, , ); // 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)
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(, , , ) * q.inverse()).coeffs().transpose() << endl; return ;
}
cmake_minimum_required(VERSION 3.14)
project(SlamPractice) #添加头文件
INCLUDE_DIRECTORIES(“usr/include/eigen3”)
set(CMAKE_CXX_STANDARD ) 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. (转)Python学习笔记(1)__name__变量

    Python使用缩进对齐组织代码的执行,所有没有缩进的代码,都会在载入时自动执行.每个文件(模块)都可以任意写一些没有缩进的代码,并在载入时自动执行.为了区分 主执行代码和被调用文件,Python引入 ...

  2. C在结构体里面使用共用体

    在做链表的时候我们设计每个节点都是一个结构体,每个节点的数据用一个共用体表示,每创建malloc一个结构体节点我们也要相应的malloc共用体并把它付进去. 这是定义: typedef union E ...

  3. codeforces round#524 D - Olya and magical square /// 大概算是数学规律题?

    题目大意: t 个测试用例  (1≤t≤103) 给定n k  (1≤n≤10^9,1≤k≤10^18) 表示有一个边长为2^n的正方形格子 每次操作只能将一个格子切割为左上左下右上右下的四等分格子 ...

  4. 取地址栏query

    GetQueryParm () { var name, value       var str = window.location.href       var num = str.indexOf(' ...

  5. CF475F meta-universe

    题意:给你一个无限大矩形中有一些planet,每次可以选择某一没有planet的行或列分割开矩形(分割开以后要求矩形不为空).问最后能分割成几个矩形? 标程: #include<bits/std ...

  6. leetcode-17-电话号码的字母组合’

    题目描述: 方法一:回溯 class Solution: def letterCombinations(self, digits): """ :type digits: ...

  7. 简单的 js手写轮播图

    html: <div class="na1">   <div class="pp">    <div class="na ...

  8. thingkphp 路由实例

    我们已经了解了如何定义路由规则,下面我们来举个例子加深印象. 假设我们定义了News控制器如下(代码实现仅供参考): namespace Home\Controller; use Think\Cont ...

  9. Java 对系统信号的通知获取

    主要介绍Java 如何对系统信号通知进行获取和处理.直接上demo @SuppressWarnings("restriction")public class Test1 imple ...

  10. Ubunto 无法连接ssh客服端

    解决办法: (1)查看ip地址是否冲突 我在单位的虚拟机ip地址是192.168.14.85,与其它机器冲突了.改成了192.168.14.83   (2)关闭Ubuntu14.04的防火墙 root ...