特征不能给出正确的矩阵逆(c++)

Eigen is Failing to Give Correct Matrix Inverse (c++)

本文关键字:c++ 不能 特征      更新时间:2023-10-16

我正在使用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;

我希望能够反转一个矩阵,我自己为它赋值。有人能告诉我这里发生了什么事吗?艾根为什么这么做?

您的矩阵是这样的:

0    3    6
1    4    7
2    5    8

如果从第2行和第3行减去第1行,则得到:

0    3    6
1    1    1
2    2    2

然后,从第3行减去2*第2行,得到:

0    3    6
1    1    1
0    0    0

这意味着矩阵是奇异的!这意味着矩阵不能反转!

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