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;
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;
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?