eigen在c++的基本使用,矩阵运算,几何运算

6 篇文章 0 订阅

矩阵运算

CMakeLists.txt文件

cmake_minimum_required(VERSION 3.2)
project(useEigen)

set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-O3")

include_directories("/usr/include/eigen3")
add_executable(eigenMatrix eigenMatrix.cpp)

eigenMatrix.cpp文件

#include <iostream>

using namespace std;

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

using namespace Eigen;

#define MATRIX_SIZE 10

int main(int argc, char **argv) {
    Matrix<float, 2, 3> matrixf_23;
    matrixf_23 << 1, 2, 3, 4, 5, 6;
    cout << "matrix 2x3 from 1 to 6: \n" << matrixf_23 << endl;

    Vector3d v_3d; // //本质是Matrix<double,3,1>
    v_3d << 3, 2, 1;

    Matrix3d matrix_33 = Matrix3d::Zero(); //本质是Matrix<double,3,3>,这里初始化为零
    Matrix<double, Dynamic, Dynamic> matrix_x; // 动态矩阵,不确定矩阵大小,等效于MatrixXd matrix_x;

    // Eigen里你不能混合两种不同类型的矩阵,像这样是错的
    // Matrix<double, 2, 1> result_wrong_type = matrixf_23 * v_3d;
    // 应该显式转换
    Matrix<double, 2, 1> result = matrixf_23.cast<double>() * v_3d;
    cout << "[1,2,3;4,5,6]*[3,2,1]=" << result.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;         // 行列式

    // 特征值与特征向量, 这里使用AD分解,实对称矩阵可以保证对角化成功
    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;
    return 0;
}

几何运算

CMakeLists.txt文件

cmake_minimum_required(VERSION 3.2)
project(useEigen)

set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-O3")

include_directories("/usr/include/eigen3")
add_executable(eigenGeometry eigenGeometry.cpp)

eigenGeometry.cpp文件

#include <iostream>
#include <cmath>

using namespace std;

#include <eigen3/Eigen/Core>
#include <eigen3/Eigen/Geometry>

using namespace Eigen;

// Eigen几何模块的使用方法,Geometry模块提供了各种旋转和平移的表示

int main(int argc, char **argv) {
    cout.precision(3); // 设置了cout流的输出精度,使其仅保留小数点后三位
    Vector3d v(1, 0, 0);

    // 3D旋转矩阵,直接使用 Matrix3d 或 Matrix3f
    Matrix3d rotation_matrix = Matrix3d::Identity();

    // 旋转向量,使用AngleAxis, 它底层不直接是Matrix,但运算可以当作矩阵(因为重载了运算符)
    AngleAxisd rotation_vector(M_PI/4,Vector3d(0,0,1));  // 沿Z轴旋转 45 度
    cout << "rotation matrix =\n" << rotation_vector.matrix() << endl;      // 将旋转向量转换成旋转矩阵
    rotation_matrix = rotation_vector.toRotationMatrix();                   // 也可以直接赋值
    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;

    // 欧氏变换矩阵使用Isometry
    Isometry3d T = Isometry3d::Identity();              // 虽然称为3d,实质上是4*4的矩阵
    T.rotate(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;
    // 对于仿射和射影变换,使用Affine3d和Projective3d即可,略

    // 四元数Quaterniond
    Quaterniond q = Quaterniond(rotation_vector);  // 可以直接把旋转向量赋值给四元数,反之亦然
    cout << "quaternion from rotation vector = " << q.coeffs().transpose() << endl;   // coeffs的顺序是(v,s)
    q = Quaterniond(rotation_matrix); // 也可以把旋转矩阵赋给四元数
    cout << "quaternion from rotation matrix = " << q.coeffs().transpose() << endl;
    v_rotated = q*v;                        // 使用四元数旋转一个向量,使用重载的乘法即可
    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;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值