特征C++不能反矩阵

特征C++不能反矩阵,c++,matrix,inverse,C++,Matrix,Inverse,我试着运行下面的代码,结果正好相反: #include <iostream> #include <Eigen/Dense> using namespace std; using namespace Eigen; int main() { Matrix2d A; A << 3, 5, -7, 2; cout << "Here is the matrix A:\n" << A << endl;

我试着运行下面的代码,结果正好相反:

#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
   Matrix2d A;
   A << 3, 5,
        -7, 2;
   cout << "Here is the matrix A:\n" << A << endl;
   cout << "The determinant of A is " << A.determinant() << endl;
   cout << "The inverse of A is:\n" << A.inverse() << endl;
} 
但我正在尝试逆矩阵,这可能吗

我正在尝试这个:

        MatrixXd m(2,2);
        m << 3, 5,
            -7, 2;
        cout << m.inverse() << endl;
这就是错误:

谢谢

使用矩阵x2d的方法与使用矩阵x2d的方法相同,只是需要告诉矩阵的大小。以下是VS 2013和Eigen 3.3.4的工作示例

#include <iostream>
#include <Eigen\Dense>
using namespace Eigen;

int main(int argc, char * argv[]){

    Matrix2d A;
    A << 3, 5, -7, 2;
    std::cout << "Here is the matrix A:\n" << A << '\n';
    std::cout << "The determinant of A is:" << A.determinant() << '\n';
    std::cout << "The inverse of A is:\n" << A.inverse() << '\n';

    MatrixXd B(5, 5);
    B = MatrixXd::Random(5, 5);
    std::cout << "Here is the matrix B:\n" << B << '\n';
    std::cout << "The determinant of B is:" << B.determinant() << '\n';
    std::cout << "The inverse of B is:\n" << B.inverse() << '\n';

    return 0;
}

如果您想要更好的解释,您必须包括您遇到的实际错误。

什么错误?程序是否硬崩溃,给出了意外的答案?将实际代码和输出放在问题中,您的示例显示的是2x2而不是5x5。应该以同样的方式工作。很抱歉,在我的实际程序中,我的示例中是2x2矩阵,我需要一个5x5的逆矩阵,所以我不能使用Matrix2d。当我使用MatrixXd时,我无法编译,您可以在图像上看到错误。“你知道更好的反演方法吗?”@JonasVasconcellos不,我们看不到图像中的错误。如果你希望别人能帮助你,你就开始做一个,完成它。不,你试过运行这些例子吗?我无法编译第二个。
Matrix<double, 5, 5> C = Matrix<double, 5, 5>::Random(5, 5);