eigen的简单用法汇总

Eigen帮助文档的地址:http://eigen.tuxfamily.org/dox/pages.html
Eigen的论坛:http://forum.kde.org/viewforum.php?f=74

1.一些基本运算

#include <iostream>
using namespace std;
#include <ctime>
//核心部分
#include <Eigen/Core>
//稠密矩阵的运算
#include <Eigen/Dense>
using namespace Eigen;

#define MATRIX_SIZE 50
int main() {
    //eigen中的所有向量和矩阵都是Eigen::Matrix,三个参数为数据类型,行,列
    //声明一个2*3的float矩阵
    Matrix<float,2,3> matrix_23;
    
    Matrix3d m1; //旋转矩阵3*3 双精度,也可改为f
    AngleAxisd m2; //旋转向量 3*1
    Vector3d m3; //欧拉角 3*1
    Quaterniond m4; //四元数 4*1
    Isometry3d m5; //欧氏变换矩阵 4*4
    Affine3d m6; //仿射变换4*4
    Projective3d m7; //射影变换4*4

    //Vector3d 本质上还是Eigen::Matrix<double,3,1>即三维向量
    Vector3d v_3d;
    Matrix<float,3,1> vd_3d; //类似

    //本质上是Eigen::Matrix<double,3,3>
    Matrix3d matrix_33 = Matrix3d::Zero();  //初始化为0

    //不确定矩阵大小,使用动态矩阵
    Matrix<double, Dynamic,Dynamic> matrix_dynamic;

    //更简单的:
    MatrixXd matrix_x;
    
    matrix_23 << 1,2,3,4,5,6;
    
    cout << matrix_23 << endl;

    v_3d << 3,2,1;
    vd_3d << 4,5,6;

    //乘法,不同类型需要显性的转换
    Matrix<double,2,1> result = matrix_23.cast<double>() * v_3d;
    cout << "[1,2,3;4,5,6]*[3,2,1]=
" << result << endl;
    
    /*******矩阵运算*********/
    matrix_33 = Matrix3d::Random();
    cout << "random matrix33:
" << matrix_33 << endl;
    cout << "transpose:
" << matrix_33.transpose() << endl; //转置
    cout << "sum:" << matrix_33.sum() << endl; //各元素求和
    cout << "trace:" << matrix_33.trace() << endl; //迹
    cout << "times 10:
" << matrix_33 * 10 << endl; //数乘
    cout << "inverse:
" << matrix_33.inverse() << endl; //逆
    cout << "det:" << matrix_33.determinant() << endl; //行列式

    /***********************/

    //特征值
    //实对称矩阵可以保证对角化成功
    SelfAdjointEigenSolver<Matrix3d> eigen_solver(matrix_33.transpose() * matrix_33);
    cout << "eigen values = 
" << eigen_solver.eigenvalues() << endl;
    cout << "Eigen vectors = 
" << 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_str = clock();

    //直接求逆
    Matrix<double,MATRIX_SIZE,1> x = matrix_NN.inverse() * v_Nd;
    cout << "time is:" << 1000*(clock() - time_str) / (double) CLOCKS_PER_SEC << "ms" << endl;
    cout << "x=" << x.transpose() << endl;

    //QR分解,速度快很多
    time_str = clock();
    x = matrix_NN.colPivHouseholderQr().solve(v_Nd);
    cout << "time is:" << 1000*(clock() - time_str) / (double) CLOCKS_PER_SEC << "ms" << endl;
    cout << "x=" << x.transpose() << endl;

    //对于正定矩阵,还可以用cholesky分解来解方程
     time_str = clock();
    x = matrix_NN.ldlt().solve(v_Nd);
    cout << "time is:" << 1000*(clock() - time_str) / (double) CLOCKS_PER_SEC << "ms" << endl;
    cout << "x=" << x.transpose() << endl;

    //旋转
    double theta = n * 2 * M_PI / (poseNums * 4); // 1/4 圆
    R = Eigen::AngleAxisd(theta, Eigen::Vector3d::UnitZ());

    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)

project (main)

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

add_definitions(-std=c++11)

include_directories(inc)

aux_source_directory(src DIR_SRCS)

SET(SOUR_FILE ${DIR_SRCS})
include_directories("/usr/include/eigen3")

add_executable(main ${SOUR_FILE})

原文地址:https://www.cnblogs.com/penuel/p/11650187.html