minisparrow / aboutAI

keep track of the state of art AI
1 stars 0 forks source link

矩阵运算求解方程Ax=v #1

Open minisparrow opened 4 years ago

minisparrow commented 4 years ago

使用QR分解,要比直接求逆运算速度要快。这是为什么。

minisparrow commented 4 years ago

使用求逆的时间

    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<double,MATRIX_SIZE,1> x = matrix_NN.inverse()*v_Nd;
    cout <<"time use in normal inverse is " << 1000* (clock() - time_stt)/(double)CLOCKS_PER_SEC << "ms"<< endl;
minisparrow commented 4 years ago

使用QR分解的时间

    // 通常用矩阵分解来求,例如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;
minisparrow commented 4 years ago

实例中MAXTIRX_SIZE = 50 time use in normal inverse is 6.499ms time use in Qr decomposition is 0.106ms

QR decompostition is faster 61 times than normal inverse.