구해 행렬 방정식 소모 시간 비교(직접 역구, Qr분해, LU분해)

2176 단어 slam

테스트 환경:

  • C++
  • 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

    좋은 웹페이지 즐겨찾기