Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invert 4x4 matrix - Numerical most stable solution needed

I want to invert a 4x4 matrix. My numbers are stored in fixed-point format (1.15.16 to be exact).

With floating-point arithmetic I usually just build the adjoint matrix and divide by the determinant (e.g. brute force the solution). That worked for me so far, but when dealing with fixed point numbers I get an unacceptable precision loss due to all of the multiplications used.

Note: In fixed point arithmetic I always throw away some of the least significant bits of immediate results.

So - What's the most numerical stable way to invert a matrix? I don't mind much about the performance, but simply going to floating-point would be to slow on my target architecture.

like image 515
Nils Pipenbrinck Avatar asked Oct 01 '08 00:10

Nils Pipenbrinck


People also ask

How do you invert a 4x4 matrix?

How do you find the inverse of a 4x4 matrix? The inverse of a square matrix can be found through row reduction of the augmented matrix, created by attaching a copy of the identity matrix. If the matrix can be reduced to the identity, then in parallel the identity matrix will transform to the inverse matrix.

What is the easiest way to find the inverse of a 4x4 matrix?

There are mainly two ways to obtain the inverse matrix. One is to use Gauss-Jordan elimination and the other is to use the adjugate matrix. We employ the latter, here. The inverse matrix has the property that it is equal to the product of the reciprocal of the determinant and the adjugate matrix.

What is the condition for inversion of a matrix?

Requirements to have an InverseThe matrix must be square (same number of rows and columns). The determinant of the matrix must not be zero (determinants are covered in section 6.4). This is instead of the real number not being zero to have an inverse, the determinant must not be zero to have an inverse.

Which method is used for matrix inversion?

We can find the matrix inverse only for square matrices, whose number of rows and columns are equal such as 2 × 2, 3 × 3, etc. In simple words, inverse matrix is obtained by dividing the adjugate of the given matrix by the determinant of the given matrix.


2 Answers

I'd like to second the question Jason S raised: are you certain that you need to invert your matrix? This is almost never necessary. Not only that, it is often a bad idea. If you need to solve Ax = b, it is more numerically stable to solve the system directly than to multiply b by A inverse.

Even if you have to solve Ax = b over and over for many values of b, it's still not a good idea to invert A. You can factor A (say LU factorization or Cholesky factorization) and save the factors so you're not redoing that work every time, but you'd still solve the system each time using the factorization.

like image 169
John D. Cook Avatar answered Sep 28 '22 06:09

John D. Cook


Meta-answer: Is it really a general 4x4 matrix? If your matrix has a special form, then there are direct formulas for inverting that would be fast and keep your operation count down.

For example, if it's a standard homogenous coordinate transform from graphics, like:

[ux vx wx tx]
[uy vy wy ty]
[uz vz wz tz]
[ 0  0  0  1]

(assuming a composition of rotation, scale, translation matrices)

then there's an easily-derivable direct formula, which is

[ux uy uz -dot(u,t)]
[vx vy vz -dot(v,t)]
[wx wy wz -dot(w,t)]
[ 0  0  0     1    ]

(ASCII matrices stolen from the linked page.)

You probably can't beat that for loss of precision in fixed point.

If your matrix comes from some domain where you know it has more structure, then there's likely to be an easy answer.

like image 44
Adrian Avatar answered Sep 28 '22 04:09

Adrian