c++ Eigen(一)

  • Post author:
  • Post category:其他


摘自:

  1. https://github.com/gaoxiang12/slambook2/tree/master/ch3/useEigen
  2. C++各种格式转换

    link
  3. C++强制类型转换

    link

做了些稍微的修改,

添加了一些注解,总结,当做练习。

#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <ctime>

#define MATRIX_SIZE 12


int main(int argc, char **argv){

    Eigen::Matrix<float, 2, 3> matrix_23;

    // Vector 向量
    Eigen::Vector3d v_3d; //Eigen::Vector3d = Eigen::Matrix<double, 3, 1>
    Eigen::Matrix<float, 3, 1> vf_3d;
    // Matrix 矩阵
    Eigen::Matrix3d matrix_33 = Eigen::Matrix3d::Zero(); //Eigen::Matrix3d = Eigen::Matrix<double, 3, 3>

    // MatrixXd 可以动态确定矩阵的大小,行、列都可以动态设置
    Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> matrix_dynamic;
    Eigen::MatrixXd matrix_x;

    matrix_23 << 1, 2, 3, 4, 5, 6;
   // 可以直接打印
    std::cout << "matrix 2x3 from 1 to 6: \n" << matrix_23 << std::endl;

    std::cout << "print matrix 2x3: " << std::endl;
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 3; j++){
            std::cout << matrix_23(i,j) << "\t";
        }
        std::cout << std::endl;
    }

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

    //.cast<double>() 显式转化
    Eigen::Matrix<double, 2, 1> result = matrix_23.cast<double>() * v_3d;
    std::cout << "[1,2,3;4,5,6] * [3,2,1] =" << result.transpose() << std::endl;

    Eigen::Matrix<float, 2, 1> result1 = matrix_23 * vf_3d;
    std::cout << "[1,2,3;4,5,6] * [4,5,6] =" << result1.transpose() << std::endl;

    matrix_33 = Eigen::Matrix3d::Random();
    std::cout << "random matrix : \n" << matrix_33 << std::endl;
    std::cout << "transpose: \n" << matrix_33.transpose() << std::endl;
    std::cout << "sum: \n" << matrix_33.sum() << std::endl;
    std::cout << "trace: \n" << matrix_33.trace() << std::endl;
    std::cout << "inverse: \n" << matrix_33.inverse() << std::endl;
    std::cout << "times 10: \n" << 10 * matrix_33 << std::endl;
    std::cout << "det: \n" << matrix_33.determinant() << std::endl; // 行列式

    //特征值、特征向量
    //实对称矩阵可以保证对角化成功
    Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigen_solver(matrix_33.transpose() * matrix_33);
    std::cout << "Eigen values : \n" << eigen_solver.eigenvalues() << std::endl;
    std::cout << "Eigen vectors : \n" << eigen_solver.eigenvectors() << std::endl;

    // 解方程
    // 我们求解 matrix_NN * x = v_Nd 这个方程
    // N的大小在前边的宏里定义,它由随机数生成
    // 直接求逆自然是最直接的,但是求逆运算量大
    Eigen::Matrix<double, MATRIX_SIZE, MATRIX_SIZE> matrix_NN = Eigen::MatrixXd::Random(MATRIX_SIZE, MATRIX_SIZE);
    matrix_NN = matrix_NN * matrix_NN.transpose(); //保证半正定
    Eigen::Matrix<double, MATRIX_SIZE, 1> v_Nd = Eigen::MatrixXd::Random(MATRIX_SIZE, 1);

    clock_t time_stt = clock(); //__STD_TYPE __CLOCK_T_TYPE __clock_t;	/* Type of CPU usage counts.  */
    //直接求逆    #define CLOCKS_PER_SEC  ((__clock_t) 1000000)
    Eigen::Matrix<double, MATRIX_SIZE, 1> x = matrix_NN.inverse() * v_Nd;
    std::cout << "time of normal inverse is "
         << 1000 * (clock() -time_stt)/  (double )CLOCKS_PER_SEC << "ms" << std::endl;
    std::cout << "x = " << x.transpose() << std::endl;

    // 通常用矩阵分解来求,例如QR分解,速度会快很多
    time_stt = clock();
    x = matrix_NN.colPivHouseholderQr().solve(v_Nd);
    std::cout << "time of Qr decomposition is "
         << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << std::endl;
    std::cout << "x = " << x.transpose() << std::endl;

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

    return 0;
}

cmakelist.txt

cmake_minimum_required(VERSION 3.17)
project(useEigen)

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

include_directories(/usr/include/eigen3)
add_executable(eigenMatrix1 eigenMatrix1.cpp)

总结:

1.计算时间

#include <ctime>

int main(int argc, char **argv){
	//__STD_TYPE __CLOCK_T_TYPE __clock_t;	/* Type of CPU usage counts.  */
	clock_t time_stt0 = clock(); 
    //中间代码
    clock_t time_stt1 = clock()
    clock_t time_del = 1000 * (time_stt1 - time_stt0)/ (double )CLOCKS_PER_SEC
    std::cout << "time of del is "
         << time_del  << "ms" << std::endl;

	return 0;
}

2.转化

1(double )CLOCKS_PER_SEC
(2.cast<double>()




版权声明:本文为weixin_40378209原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。