代码笔记2 eigen库的实践1

1 环境

  系统 Ubuntu1804,版本 gcc/g++ 7, IDE Vscode(你也可以不把vscode当IDE哈哈哈,其实比较想用KDEVELOP,但是本人喜欢黑色界面,kdevelope属实差点意思),eigen3

2 代码展示

  也是在学高博的SLAM十四讲,不过许多东西高博也没有细讲。
cmakelist.txt

cmake_minimum_required(VERSION 2.8)
project(useEigen)

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

# 添加Eigen头文件
include_directories("/usr/include/eigen3")
add_executable(eigenMatrix eigenMatrix.cpp)

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

说点奇怪的,就是即使cmakelist添加了头文件的路径,vscode仍然会标红,但好像运行不报错。但我我看了很难受,就根据[1]建议的建立了软连接。
myeigenlearn.cpp

#include <iostream>
using namespace std;
#include <ctime>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Cholesky>
#include <Eigen/LU>
#include <Eigen/QR>
#include <Eigen/SVD>

using namespace Eigen;

int main(int argc, char **argv)
{
    Matrix<float, 3, 3> matrix_33;
    matrix_33 << 1,2,3,4,5,6,7,8,9;
    cout<< matrix_33<<endl;
    cout<<matrix_33.transpose()<<endl;
    Vector3d v3d;
    v3d<<1,2,3;
    Matrix<double,3,1> output = matrix_33.cast<double>()*v3d;
    cout<< "output = "<< output<<endl;

    Matrix<double,10,10> randommatrix = MatrixXd::Random(10,10);
    
    randommatrix = randommatrix.transpose()*randommatrix;
    cout<< "random = \n"<<randommatrix<<endl;
    Matrix<double,10,1> v10d = MatrixXd::Random(10,1);
    cout<<"v10d = \n"<<v10d<<endl;

    //x = randommatrix**(-1)*v10d
    Matrix<double,Dynamic,Dynamic> x5;
    clock_t time_stt = clock();

    Matrix<double,10,1> x1,x2,x3,x4,x6;   
    x1 = randommatrix.ldlt().solve(v10d); 
    cout << "time of x1 decomposition is "
    << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
    cout<<"x1= \n"<<x1.transpose()<<endl;

    time_stt = clock();
    x2 = randommatrix.llt().solve(v10d);  
    cout << "time of x2 decomposition is "
    << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
    cout<<"x2= \n"<<x2.transpose()<<endl;

    time_stt = clock();
    x3 = randommatrix.lu().solve(v10d);
    cout << "time of x3 decomposition is "
    << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
    cout<<"x3= \n"<<x3.transpose()<<endl;

    time_stt = clock();
    x4 = randommatrix.colPivHouseholderQr().solve(v10d);
    cout << "time of x4 decomposition is "
    << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
    cout<<"x4= \n"<<x4.transpose()<<endl;

    time_stt = clock();
    x5 = randommatrix.inverse()*v10d;
    cout << "time of x5 decomposition is "
    << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
    cout<<"x5= \n"<<x5.transpose()<<endl;


    return 0;
}

主要是一些基本功能的应用,并实现了利用已有的函数进行了线性方程的求解,以及各个方法所需要的实现。但我没学会怎么用函数bscSVD()和jacobiSVD()来求解。希望以后还能拓展吧。

time of x1 decomposition is 0.014ms
x1= 
-6.38902 -9.03016 -5.13948  8.55177  4.72832   8.1699 0.999135  7.72978 -5.40814 0.592483
time of x2 decomposition is 0.004ms
x2= 
-6.38902 -9.03016 -5.13948  8.55177  4.72832   8.1699 0.999135  7.72978 -5.40814 0.592483
time of x3 decomposition is 0.005ms
x3= 
-6.38902 -9.03016 -5.13948  8.55177  4.72832   8.1699 0.999135  7.72978 -5.40814 0.592483
time of x4 decomposition is 0.009ms
x4= 
-6.38902 -9.03016 -5.13948  8.55177  4.72832   8.1699 0.999135  7.72978 -5.40814 0.592483
time of x5 decomposition is 0.009ms
x5= 
-6.38902 -9.03016 -5.13948  8.55177  4.72832   8.1699 0.999135  7.72978 -5.40814 0.592483

不过这个速度也不是特别稳定,可能是我的矩阵规格本身不大。
根据eigen库本身的文档说明[2]所示,有这么些方法可以用作解线性方程
image
以后有困难了细读这个网址
还有一个将matlab和eigen相对应的,写的也相当不错[3]

References

[1]https://blog.csdn.net/qq_34213260/article/details/107701717?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-0.pc_relevant_default&spm=1001.2101.3001.4242.1&utm_relevant_index=3
[2]http://eigen.tuxfamily.org/dox/group__TutorialLinearAlgebra.html
[3]https://www.cnblogs.com/python27/p/EigenQuickRef.html

posted @ 2022-04-28 17:28  The1912  阅读(64)  评论(0编辑  收藏  举报