eigen库使用

2019-07-13 01:56发布

#include
#include
#include
#include
// Eigen 部分
#include
// 稠密矩阵的代数运算(逆,特征值等)
#include
#define MATRIX_SIZE 100
using namespace std;
int main( int argc, char** argv )
{
    Eigen::Matrix< double, MATRIX_SIZE, MATRIX_SIZE > matrix_NN;
    matrix_NN = Eigen::MatrixXd::Random( MATRIX_SIZE, MATRIX_SIZE );
    Eigen::Matrix< double, MATRIX_SIZE,  1> v_Nd;
    v_Nd = Eigen::MatrixXd::Random( MATRIX_SIZE,1 );


    clock_t time_stt = clock(); // 计时
    // 直接求逆
    Eigen::Matrix x = matrix_NN.inverse()*v_Nd;
    Eigen::Matrix x1= matrix_NN.inverse()*v_Nd;
    cout <<"time use in normal inverse is " << 1000* (clock() - time_stt)/(double)CLOCKS_PER_SEC << "ms"<< endl;
    
// 通常用矩阵分解来求,例如QR分解,速度会快很多
    time_stt = clock();
    x = matrix_NN.colPivHouseholderQr().solve(v_Nd);
    cout <<"time use in Qr decomposition is " <<1000*  (clock() - time_stt)/(double)CLOCKS_PER_SEC <<"ms" << endl;
    x1 = matrix_NN.llt().solve(v_Nd);
    cout <<"time use in Qr decomposition is " <<1000*  (clock() - time_stt)/(double)CLOCKS_PER_SEC <<"ms" << endl;
    return 0;
}使译cmake文件
  1. mkdir -p build/release
  2. mkdir -p build/debug
  3. cmake -E chdir build/release cmake -G "Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=release ../../
  4. cmake -E chdir build/debug cmake -G "Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=debug ../../
File->Import->General->Existing Projects into Workspace
点击 “Next”,在 Select root directory 选择
目录为project_dir/build
Eclipse会自动搜索到debug, release两个目录下的项目。
点击“Finish”按钮。eclipse将自动加入项目。