0

I am using Eigen Library to do some matrix calculations.

I have such calculation

MatrixXf A = MatrixXf::Random(3, 3);
MatrixXf B = A.inverse();
MatrixXf C = A*B;
MatrixXf D = C - Matrix3f::Identity();

The result I got for C is:

           1            0  1.19209e-07
 1.19209e-07            1            0
-1.19209e-07            0            1

and for D is

           0            0  1.19209e-07
 1.19209e-07            0            0
-1.19209e-07            0  1.19209e-07

I would like to reduce this error caused by the floating-point precision. the reason is that I need to multiply a very big coefficient (e+09) onto this matrix which amplifies these errors significantly. I tried using double precision, but by multiplying with (e+09), the error become a real problem by twice the multiplication.

BruceZ
  • 11
  • 3
  • What is the actual problem you want to solve? To get the highest precision of `A * A.inverse()` you could directly write `Matrix3f::Identity()` (that is unlikely what you need ...) – chtz Feb 15 '22 at 17:17
  • @chtz so I want to get the exact identity matrix for C and zero-matrix for D. I just use `A*A.inverse ()` as an example. In my actual case, in the next loops, it is not `A*A.inverse()` anymore, but the error caused by that is preserved, which broke my calculation – BruceZ Feb 15 '22 at 17:21
  • @chtz additionally, my actual calculation is like `(A+X) * A.inverse()` and at first step X is 0. X is derived from previous step of `(A+X) * A.inverse()` – BruceZ Feb 15 '22 at 17:23
  • I think this could be a possible solution: [Rounding numbers below a certain threshold to zero in Eigen](https://stackoverflow.com/questions/54503795/rounding-numbers-below-a-certain-threshold-to-zero-in-eigen). But a roundup function could be better – BruceZ Feb 15 '22 at 17:46
  • You should already be much better by doing `Identity() + X * A.inverse()`. Then there are more tricks to improve the accuracy of the second term, if needed. Is `A` always 3x3 (or at least not bigger than 4x4)? Otherwise, you should usually not calculate the inverse explicitly (but again, this depends on what you are doing overall). – chtz Feb 15 '22 at 19:51

0 Answers0