<p>I found a solution to my specific problem. Instead of computing the determinant and hitting the precision wall, I use the Gauss-Jordan method step by step.</p>

<p>In my specific case of affine transformation matrices and the range of values I use, I don't hit any precision problem this way.</p>

CC BY-SA 3.0
2016-03-04T21:10:52.300Z
2016-03-04T21:10:52.300Z
solendil
<p>I'm using an affine transformation matrix to transform 2D coordinates from screen (magnitude 10e3) to small parts of fractal sets (magnitude as little as 10e-15).</p>

<p>I also need to map the other way round, so I simply invert the matrix. It works when the magnitudes are not too far apart, but it fails when the determinant gets too close to zero to be represented with standard double numbers.</p>

<p>The matrices I use are of the form:</p>

<pre><code>a c e
b d f
0 0 1
</code></pre>

<p>And the inversion algorithm I have is:</p>

<pre><code>var dt = a * d - b * c;
return new Matrix(d/dt, -b/dt, -c/dt, a/dt, (c * f - d * e) / dt, -(a * f - b * e) / dt);
</code></pre>

<p>Is there an alternate way of inverting the matrix? I'm not very good at mathematics, so I would need a simple solution, and one I could find the code, or an algorithm, for.</p>

<p>An example of such a matrix:</p>

<pre><code> 3.26091378894248e-9   -1.882689453850103e-9   -0.7172216437740687
-1.882689453814925e-9  -3.2609137888815494e-9  -0.23371832131832268
 0                      0                       1
</code></pre>

CC BY-SA 3.0
2016-03-04T18:29:46.107Z
Perhaps your problem might stem from the order of ops in your calculation. You might have something like  (BigValue + SmallValue) - BigValue, and instead of getting "SmalValue" you get 0.
Simon F
CC BY-SA 3.0
2016-03-04T18:46:03.690Z
Just occurred to me that you seem to, effectively, have a rotation, R, followed a translation T.  If you can keep these separate, to get Inverse(R*T) you can just do Inverse(T)*Inverse(R), both of which are trivial. Would that help?
Simon F
CC BY-SA 3.0
2016-03-04T21:10:12.203Z
Simon, you're right about the problem stemming from multiplying numbers of different magnitudes. The matrix is more complex than an R*T; it's also a scale, and can be a multiplication of any affine.. I managed to fix the problem, however, see below. Thanks for your help.
solendil
CC BY-SA 3.0
2016-03-04T21:16:23.010Z
You should ask on the math forum !
Fabrice NEYRET
CC BY-SA 3.0
2016-03-07T08:52:34.917Z
@solendil sorry yes I meant to mention scaling as well, but missed the edit timeout. Scaling is, obviously, also trivial to include particularly if it's the same for both dimensions. FWIW I used this trick in an API for some early PC graphics chips. Anyway, glad you've resolved your issues.
Simon F
<transformations><mathematics><2d><affine-transformations>
How to invert an affine matrix with small values?
solendil
# How to invert an affine matrix with small values?

(Asked by: username_0 on 2016-03-04)

I'm using an affine transformation matrix to transform 2D coordinates from screen (magnitude 10e3) to small parts of fractal sets (magnitude as little as 10e-15).

I also need to map the other way round, so I simply invert the matrix. It works when the magnitudes are not too far apart, but it fails when the determinant gets too close to zero to be represented with standard double numbers.

The matrices I use are of the form:

```
a c e
b d f
0 0 1

```

And the inversion algorithm I have is:

```
var dt = a * d - b * c;
return new Matrix(d/dt, -b/dt, -c/dt, a/dt, (c * f - d * e) / dt, -(a * f - b * e) / dt);

```

Is there an alternate way of inverting the matrix? I'm not very good at mathematics, so I would need a simple solution, and one I could find the code, or an algorithm, for.

An example of such a matrix:

```
 3.26091378894248e-9   -1.882689453850103e-9   -0.7172216437740687
-1.882689453814925e-9  -3.2609137888815494e-9  -0.23371832131832268
 0                      0                       1

```

**Tags:** transformations, mathematics, 2d, affine-transformations

> **Comments:**
> **username_1**: Perhaps your problem might stem from the order of ops in your calculation. You might have something like  (BigValue + SmallValue) - BigValue, and instead of getting "SmalValue" you get 0.  
> **username_1**: Just occurred to me that you seem to, effectively, have a rotation, R, followed a translation T.  If you can keep these separate, to get Inverse(R*T) you can just do Inverse(T)*Inverse(R), both of which are trivial. Would that help?  
> **username_0**: username_1, you're right about the problem stemming from multiplying numbers of different magnitudes. The matrix is more complex than an R*T; it's also a scale, and can be a multiplication of any affine.. I managed to fix the problem, however, see below. Thanks for your help.  
> **username_2**: You should ask on the math forum !  
> **username_1**: @username_0 sorry yes I meant to mention scaling as well, but missed the edit timeout. Scaling is, obviously, also trivial to include particularly if it's the same for both dimensions. FWIW I used this trick in an API for some early PC graphics chips. Anyway, glad you've resolved your issues.  

---

## 1 answers

### Answer by username_0 on 2016-03-04. Score: 7 | Accepted answer

I found a solution to my specific problem. Instead of computing the determinant and hitting the precision wall, I use the Gauss-Jordan method step by step.

In my specific case of affine transformation matrices and the range of values I use, I don't hit any precision problem this way.


---

