这是您要求的本征示例。它遵循 Christian Clason 评论中的方法,并且使用显式逆进行比较进行相同的计算。
#include <iostream>
using std::cout;
using std::endl;
#include <ctime>
#include <Eigen/Core>
#include <Eigen/LU>
void compareInvAndFactor()
{
typedef Eigen::MatrixXd Matrix;
const int n = 1000, m = 600;
Matrix B = Matrix::Random(n, n);
Matrix A = Matrix::Random(m, n);
std::clock_t start = std::clock();
auto Clu = B.transpose().lu().solve(A.transpose()).transpose();
cout << "Elapsed time using LU factorization = " << (std::clock() - start) /
(double)CLOCKS_PER_SEC << " seconds." << endl;
start = std::clock();
auto Cinv = A*B.inverse();
cout << "Elapsed time using inverse = " << (std::clock() - start) /
(double)CLOCKS_PER_SEC << " seconds." << endl;
double diff = (Cinv - Clu).cwiseAbs().maxCoeff();
cout << "max difference in C matrices =" << diff << endl;
}