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

测试环境:
  1. C++
  2. Egien库
代码
#include <iostream>
#include <ctime>

#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/LU>
#include <Eigen/Cholesky>

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<double, MATRIX_SIZE, 1> 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

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值