特征不能给出正确的矩阵逆(c++)
Eigen is Failing to Give Correct Matrix Inverse (c++)
我正在使用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
这意味着矩阵是奇异的!这意味着矩阵不能反转!
你选择矩阵的方式非常不幸。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么我的for循环不能正确获取argv
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 为什么模板类中的对象不能返回值
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 数组长度,为什么从命令行获取时不能使用它?
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- 为什么我不能使用 EGL 创建无头 OpenGl 上下文?
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 为什么我不能将 rand() 与数组的大小一起使用?
- 为什么虚函数不能是静态的和全局的?
- 为什么我不能在 while 循环中创建线程?
- 为什么这个音频包络不能通过开关的情况?
- 不能将复制初始化与隐式转换的多个步骤一起使用