【问题标题】:Eigen is Failing to Give Correct Matrix Inverse (c++)Eigen 未能给出正确的逆矩阵(c++)
【发布时间】:2014-02-27 04:48:26
【问题描述】:

我正在使用 Eigen(与 c++ 一起使用的免费线性代数包)并尝试反转一个小矩阵。遵循官方 Eigen 文档后,我得到以下信息:

#include <iostream>
using namespace std;
#include <Eigen/LU>
#include <Eigen/Dense>
using namespace Eigen;

Matrix3d k = Matrix3d::Random();
cout << "Here is the matrix k:" << endl << k << endl;
cout << "Its inverse is:" << endl << k.inverse() << endl;
cout << "The product of the two (supposedly the identity) is:" << endl << k.inverse()*k << endl;

这给了我正确的答案。但是,如果不是让 k 成为随机分配的矩阵,而是创建一个矩阵然后自己分配所有值,它会给我错误的逆矩阵。例如,下面的代码会给我错误的逆。

Matrix3d m;
Matrix3d mi;
for (int i = 0; i < 3; ++ i)
    for (int j = 0; j < 3; ++ j)
        m (i, j) = i + 3.0*j;

std::cout <<  "m is " << m << std::endl;
mi = m.inverse();
std::cout <<  "mi is "  <<  mi << std::endl;
std::cout <<  "product of m and m_inverse is "  <<  (mi*m) << std::endl;

我希望能够反转我自己为其分配值的矩阵。谁能告诉我这里发生了什么? Eigen为什么要这样做?

【问题讨论】:

  • 矩阵不可逆是通常的原因......
  • 你的矩阵是不可逆的。你期待什么样的结果?
  • 啊,就是这样。我也尝试过其他几个矩阵以防万一,但事实证明它们也是奇异的。感谢收看

标签: c++ matrix eigen matrix-inverse


【解决方案1】:

你的矩阵是这样的:

0    3    6
1    4    7
2    5    8

如果你从 row2 和 row3 中减去 row1,你会得到:

0    3    6
1    1    1
2    2    2

然后,从 row3 中减去 2*row2,得到:

0    3    6
1    1    1
0    0    0

这意味着矩阵是奇异的!这意味着矩阵不能倒置!

您选择矩阵的方式非常不幸。

【讨论】:

  • +1。光看定义就知道是单数,但懒得写了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多