视觉SLAM十四讲:[实践2] Eigen线性代数库

Eigen是C++开源的线性代数库,它是一个纯用头文件搭建起来的库。在使用时,只需引入Eigen的头文件即可,不需要链接库文件(它没有库文件…)

注意事项:
1.
// 在Eigen中不能将两种不同类型的矩阵混合运算,以下是错的 Matrix<double, 2, 1> result_wrong_type = matrix_23 * v_3d; // 后边式子是float类型*double类型
2.

// 内置类型 
    //例如,Vector3d实际上是Eigen::Matrix<double,3,1>
    Vector3d v_3d;
    // the same
    Matrix<float, 3, 1> vd_3d;

 // 如果不确定矩阵的大小,可以使用动态大小的矩阵
    Matrix<double, Dynamic, Dynamic> matrix_dynamic;
    // 更简单的
    MatrixXd matrix_x;

主要内容以代码形式呈现:

#include <iostream>
using namespace std;

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

#define MATRIX_SIZE 50

int main(int argc, char **argv){
    
    
    // Eigen中矩阵和向量都是模板类Eigen::Matrix,前三个参数分别是数据类型、行数、列数
    // 声明一个矩阵 2*3 float类型
    Matrix<float, 2, 3> matrix_23;
    
    // 内置类型 
    //例如,Vector3d实际上是Eigen::Matrix<double,3,1>
    Vector3d v_3d;
    // the same
    Matrix<float, 3, 1> vd_3d;
    //例如,Matrix3d实际上是Eigen::Matrix<double,3,3>
    Matrix3d matrix_33 = Matrix3d::Zero();// 初始化为0
    
    // 如果不确定矩阵的大小,可以使用动态大小的矩阵
    Matrix<double, Dynamic, Dynamic> matrix_dynamic;
    // 更简单的
    MatrixXd matrix_x;
    
    // Eigen阵的操作
    // 输入数据(初始化)
    matrix_23 <<1,2,3,4,5,6;
    // 输出
    cout << "matrix 2x3 from 1 to 6: \n" << matrix_23 << endl;
    
    // 用()访问矩阵中的的元素
    cout<< "print matrix 2x3:"<< endl;
    for (int i = 0; i < 2; i++){
        for(int j = 0; j < 3; j++)
            cout << matrix_23(i, j)<< "\t";
        cout << endl;
    }
    
    // 矩阵和向量相乘(实际上仍然是矩阵和矩阵的运算)
    v_3d << 1, 2, 3;
    vd_3d << 4, 5, 6;
    
    // 在Eigen中不能将两种不同类型的矩阵混合运算,以下是错的
    // Matrix<double, 2, 1> result_wrong_type = matrix_23 * v_3d;
    
    // 应该显示转换
    Matrix<double, 2, 1> result = matrix_23.cast<double>() * v_3d;
    cout << "[1,2,3;4,5,6] * [1,2,3] = " << result.transpose() << endl; //加上转置,结果为1x2
    
    Matrix<float, 2, 1> result2 = matrix_23 * vd_3d;
    cout << "[1,2,3;4,5,6] * [4,5,6] =" << result2.transpose() << endl;
    // 同时注意矩阵的维度问题!!!
    
    
    // 矩阵运算--》矩阵运算 +-/* 不再演示
    matrix_33 = Matrix3d::Random(); // 随机数矩阵
    cout << "random matrix : \n" << matrix_33 << endl;
    cout << "transpose : \n" << matrix_33.transpose() << endl; // 转置矩阵
    cout << "sum : \n" << matrix_33.sum() << endl; // 各元素求和
    cout << "trace : \n " << matrix_33.trace() << endl; // 迹
    cout << "times 10 : \n" << 10 * matrix_33 << endl; // 数乘
    cout << "inverse : \n " << matrix_33.inverse() << endl; // 逆
    cout << "det : \n " << matrix_33.determinant() << endl; // 行列式
    
    // 特征值
    // 我们求解 matrix_NN * x = v_Nd 方程
    // N的大小在前面宏中定义,由随机数生成
    // 直接求逆是最直接的,但是运算量太大
    
    // 特征值
  // 实对称矩阵可以保证对角化成功
  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, 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 2.8)
project(useEigen)

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

# 添加Eigen头文件
include_directories("/usr/include/eigen3")
add_executable(eigenMatrix eigenMatrix.cpp)
发布了38 篇原创文章 · 获赞 0 · 访问量 1530

猜你喜欢

转载自blog.csdn.net/weixin_40224537/article/details/104844738