求解矩阵方程耗时比较(直接求逆,Qr分解,LU分解)

测试环境:

  1. C++
  2. Egien库

代码

#include 
#include 

#include 
#include 
#include 
#include 

using namespace std;
using namespace Eigen;
/***********************
* solve equation: matrix_NN * x = v_Nd
************************/
const int MATRIX_SIZE = 100;
// https://blog.csdn.net/weixin_41074793/article/details/84241776
int main()
{
        Matrix< double, MATRIX_SIZE, MATRIX_SIZE > matrix_NN;
        matrix_NN = MatrixXd::Random( MATRIX_SIZE, MATRIX_SIZE );
        Matrix v_Nd;
        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 use in normal inverse is       " << 1000.0 * (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 composition is       " << 1000 * (clock() - time_stt) / (double)
            CLOCKS_PER_SEC << "ms" << endl;

        //cholesky分解
        time_stt = clock();
        // 使得NN成为正定矩阵,才能分解
        matrix_NN = matrix_NN.transpose() * matrix_NN;
        x = matrix_NN.partialPivLu().solve(v_Nd);
        cout << "time use in cholesky composition is " << 1000 * (clock() - time_stt) / (double)
            CLOCKS_PER_SEC << "ms" << endl;

        //LU分解
        time_stt = clock();
        x = matrix_NN.partialPivLu().solve(v_Nd);
        cout << "time use in LU composition is       " << 1000 * (clock() - time_stt) / (double)
            CLOCKS_PER_SEC << "ms" << endl;

    return 0;
}


#### 结果

time use in normal inverse is 1547.72ms
time use in Qr composition is 37.475ms
time use in cholesky composition is 13.074ms
time use in LU composition is 5.242ms

你可能感兴趣的:(slam)