本征未能给出正确的矩阵求逆(c ++)

本征未能给出正确的矩阵求逆(c ++)

问题描述:

我正在使用Eigen(与c ++一起使用的免费线性代数软件包),并试图求小矩阵的求逆.遵循Eigen官方文档后,我得到以下信息:

I'm using Eigen (the free linear algebra package for use with c++) and trying to invert a small matrix. After following the official Eigen documentation, I get the following:

#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设为一个随机分配的矩阵,而是创建一个矩阵然后自己分配所有值,则会给我带来错误的逆.例如,以下代码将给我错误的逆数.

And this gives me the correct answer. However, if instead of making k a randomly assigned matrix, if I create a matrix and then assign all of the values myself, it gives me the wrong inverse. For example, the following code will give me the wrong inverse.

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会这样做?

I want to be able to invert a matrix for which I've assigned the values myself. Can anyone tell me what is going on here? Why Eigen is doing this?

您的矩阵是这样的:

0    3    6
1    4    7
2    5    8

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

and if you subtract row1 from row2 and row3, you get:

0    3    6
1    1    1
2    2    2

然后从第3行中减去2 * row2,您将得到:

and then, subtract 2*row2 from row3, you get:

0    3    6
1    1    1
0    0    0

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

which means that the matrix is singular! This means that the matrix cannot be inverted!

不幸的是,您选择矩阵的方式.

The way you picked your matrix was just very unfortunate.