【问题标题】:EIGEN C++ Cannot Inverse MatrixXdEIGEN C++ 不能逆矩阵Xd
【发布时间】:2018-02-19 02:46:39
【问题描述】:

我尝试在下面运行此代码,反之亦然:

#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;
} 

但我正在尝试逆矩阵Xd,这可能吗?

我正在尝试这个:

        MatrixXd m(2,2);
        m << 3, 5,
            -7, 2;
        cout << m.inverse() << endl;

这是错误: enter image description here

谢谢!

【问题讨论】:

  • 什么错误?程序硬崩溃,给出意想不到的答案?将实际代码和输出放在问题中,您的示例显示 2x2 而不是 5x5。应该以同样的方式工作。
  • 抱歉,在我的示例中它是一个 2x2 矩阵(在我的实际程序中,我需要逆一个 5x5,所以我不能使用 Matrix2d)。我使用 MatrixXd 时无法编译,您可以在图像上看到错误。你知道更好的反演方法吗?
  • @JonasVasconcellos 不,我们看不到图像中的错误。您开始创建minimal reproducible example,如果您希望人们能够帮助您,请完成它。
  • 否,您是否尝试过运行这些示例?我无法编译第二个。

标签: c++ matrix inverse


【解决方案1】:

您使用 MatrixXd 的方式与使用 Matrix2d 的方式相同,只是您必须告诉它制作矩阵的大小。这是 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;
}

B 可以是任何你想要的大小。所有MatrixXd 都是Matrix&lt;double, Dynamic, Dynamic&gt; 的类型定义。你也可以用

Matrix<double, 5, 5> C = Matrix<double, 5, 5>::Random(5, 5);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    相关资源
    最近更新 更多