Game Math: How to Eyeball the Inverse of a Matrix

This post is part of my Game Math Series.

Matrix Multiplication as Linear Combinations of Columns/Rows

As shown in this post, matrix multiplication can be viewed as linear combinations of columns from the left matrix or rows from the right matrix. We can reverse this process and utilize this fact to help us eyeball inverses of matrices without having to do any calculation by hand or calculator.

The product of a matrix with its inverse gives the identity matrix:

    \[ M M^{-1} = I \]

Viewing from the column perspective, columns of the identity matrix are linear combinations of the columns from the left matrix M, where the coefficients of linear combination are from the columns of the right matrix M^{-1}.

Let us use a 3-by-3 matrix as an example:

    \[ M =  {\left[ {\begin{array}{ccc}    0 & 1 & 1 \\    1 & 2 & 3 \\    0 & 0 & 1 \\   \end{array} } \right]} \]

We want to linearly combine the columns to form the three columns from the 3-by-3 identity matrix, namely {\left[ {\begin{array}{c} 1 \\ 0 \\ 0 \end{array} } \right]}, {\left[ {\begin{array}{c} 0 \\ 1 \\ 0 \end{array} } \right]}, and {\left[ {\begin{array}{c} 0 \\ 0 \\ 1 \end{array} } \right]}.

For the first column, we can find the coefficients by eyeballing the columns from M:

    \[ {\left[ {\begin{array}{c} 1 \\ 0 \\ 0 \end{array} } \right]}  =  -2 {\left[ {\begin{array}{c} 0 \\ 1 \\ 0 \end{array} } \right]} + 1 {\left[ {\begin{array}{c} 1 \\ 2 \\ 0 \end{array} } \right]} + 0 {\left[ {\begin{array}{c} 1 \\ 3 \\ 1 \end{array} } \right]} \]

Thus, the first column of M^{-1} is {\left[ {\begin{array}{c} -2 \\ 1 \\ 0 \end{array} } \right]}.

We can figure out the rest of the coefficients for the other two columns as well:

    \[ {\left[ {\begin{array}{c} 0 \\ 1 \\ 0 \end{array} } \right]}  =  1 {\left[ {\begin{array}{c} 0 \\ 1 \\ 0 \end{array} } \right]} + 0 {\left[ {\begin{array}{c} 1 \\ 2 \\ 0 \end{array} } \right]} + 0 {\left[ {\begin{array}{c} 1 \\ 3 \\ 1 \end{array} } \right]} \]

    \[ {\left[ {\begin{array}{c} 0 \\ 0 \\ 1 \end{array} } \right]}  =  -1 {\left[ {\begin{array}{c} 0 \\ 1 \\ 0 \end{array} } \right]} - 1 {\left[ {\begin{array}{c} 1 \\ 2 \\ 0 \end{array} } \right]} + 1 {\left[ {\begin{array}{c} 1 \\ 3 \\ 1 \end{array} } \right]} \]

So the second and third columns of M^{-1} are {\left[ {\begin{array}{c} 1 \\ 0 \\ 0 \end{array} } \right]} and {\left[ {\begin{array}{c} -1 \\ -1 \\ 1 \end{array} } \right]}, respectively.

And now we have our inverse figured out, hands-free!

    \[ M^{-1} =  {\left[ {\begin{array}{ccc}    -2 & 1 & -1 \\    1 & 0 & -1 \\    0 & 0 & 1 \\   \end{array} } \right]} \]

We can perform the same trick from the row perspective:

    \[ M^{-1} M = I \]

The rows of the 3-by-3 identity matrix are linear combinations of rows from M, and the coefficients of linear combination are from the rows of M^{-1}, which we can easily figure out just by looking at M.

For the first row of the identity matrix, we can quickly find out that:

    \[ {\left[ {\begin{array}{ccc} 1 & 0 & 0 \end{array} } \right]}  =  -2 {\left[ {\begin{array}{ccc} 0 & 1 & 1 \end{array} } \right]} + 1 {\left[ {\begin{array}{ccc} 1 & 2 & 3 \end{array} } \right]} - 1 {\left[ {\begin{array}{ccc} 0 & 0 & 1 \end{array} } \right]} \]

So the first row of M^{-1} is {\left[ {\begin{array}{ccc} -2 & 1 & -1 \end{array} } \right]}.

And for the other two rows of the identity matrix, we can see that:

    \[ {\left[ {\begin{array}{ccc} 0 & 1 & 0 \end{array} } \right]}  =  1 {\left[ {\begin{array}{ccc} 0 & 1 & 1 \end{array} } \right]} + 0 {\left[ {\begin{array}{ccc} 1 & 2 & 3 \end{array} } \right]} - 1 {\left[ {\begin{array}{ccc} 0 & 0 & 1 \end{array} } \right]} \]

    \[ {\left[ {\begin{array}{ccc} 0 & 0 & 1 \end{array} } \right]}  =  0 {\left[ {\begin{array}{ccc} 0 & 1 & 1 \end{array} } \right]} + 0 {\left[ {\begin{array}{ccc} 1 & 2 & 3 \end{array} } \right]} + 1 {\left[ {\begin{array}{ccc} 0 & 0 & 1 \end{array} } \right]} \]

Thus, the second and third rows of M^{-1} are {\left[ {\begin{array}{ccc} 1 & 0 & -1 \end{array} } \right]} and {\left[ {\begin{array}{ccc} 0 & 0 & 1 \end{array} } \right]}, respectively.

We have found our inverse, hands-free again:

    \[ M^{-1} =  {\left[ {\begin{array}{ccc}    -2 & 1 & -1 \\    1 & 0 & -1 \\    0 & 0 & 1 \\   \end{array} } \right]} \]

Inverses of Perspective Projection Matrices

Okay, the example above might be a little bit contrived with pretty numbers. Now let’s look at something in the real world that we can apply this technique to.

The perspective projection matrix (negative Z being the view direction in camera space) is of the form:

    \[ P =  {\left[ {\begin{array}{cccc}    A & 0 & 0 & 0 \\    0 & B & 0 & 0 \\    0 & 0 & C & D \\    0 & 0 & -1 & 0 \\   \end{array} } \right]} \]

If you look up 3D Mouse Picking, you’ll see that we need the inverse of the perspective projection matrix to perform this operation.

“No big deal,” you might say, “Our math library already provides a function to invert a 4-by-4 matrix.”

But is it really necessary to use a full-blown 4-by-4 matrix inversion calculation when we can achieve the same thing with just a few scalar operations? We can do better.

Let’s see how we can linearly combine the four columns of P to produce the columns of the 4-by-4 identity matrix, namely {\left[ {\begin{array}{c} 1 \\ 0 \\ 0 \\ 0 \end{array} } \right]}, {\left[ {\begin{array}{c} 0 \\ 1 \\ 0 \\ 0 \end{array} } \right]}, {\left[ {\begin{array}{c} 0 \\ 0 \\ 1 \\ 0 \end{array} } \right]}, and {\left[ {\begin{array}{c} 0 \\ 0 \\ 0 \\ 1 \end{array} } \right]}.

    \[ {\left[ {\begin{array}{c} 1 \\ 0 \\ 0 \\ 0 \end{array} } \right]}  =  \frac{1}{A} {\left[ {\begin{array}{c} A \\ 0 \\ 0 \\ 0 \end{array} } \right]} + 0 {\left[ {\begin{array}{c} 0 \\ B \\ 0 \\ 0 \end{array} } \right]} + 0 {\left[ {\begin{array}{c} 0 \\ 0 \\ C \\ -1 \end{array} } \right]} + 0 {\left[ {\begin{array}{c} 0 \\ 0 \\ D \\ 0 \end{array} } \right]} \]

    \[ {\left[ {\begin{array}{c} 0 \\ 1 \\ 0 \\ 0 \end{array} } \right]}  =  0 {\left[ {\begin{array}{c} A \\ 0 \\ 0 \\ 0 \end{array} } \right]} + \frac{1}{B} {\left[ {\begin{array}{c} 0 \\ B \\ 0 \\ 0 \end{array} } \right]} + 0 {\left[ {\begin{array}{c} 0 \\ 0 \\ C \\ -1 \end{array} } \right]} + 0 {\left[ {\begin{array}{c} 0 \\ 0 \\ D \\ 0 \end{array} } \right]} \]

    \[ {\left[ {\begin{array}{c} 0 \\ 0 \\ 1 \\ 0 \end{array} } \right]}  =  0 {\left[ {\begin{array}{c} A \\ 0 \\ 0 \\ 0 \end{array} } \right]} + 0 {\left[ {\begin{array}{c} 0 \\ B \\ 0 \\ 0 \end{array} } \right]} + 0 {\left[ {\begin{array}{c} 0 \\ 0 \\ C \\ -1 \end{array} } \right]} + \frac{1}{D} {\left[ {\begin{array}{c} 0 \\ 0 \\ D \\ 0 \end{array} } \right]} \]

    \[ {\left[ {\begin{array}{c} 0 \\ 0 \\ 0 \\ 1 \end{array} } \right]}  =  0 {\left[ {\begin{array}{c} A \\ 0 \\ 0 \\ 0 \end{array} } \right]} + 0 {\left[ {\begin{array}{c} 0 \\ B \\ 0 \\ 0 \end{array} } \right]} -1 {\left[ {\begin{array}{c} 0 \\ 0 \\ C \\ -1 \end{array} } \right]} + \frac{C}{D} {\left[ {\begin{array}{c} 0 \\ 0 \\ D \\ 0 \end{array} } \right]} \]

So the inverse of the perspective projection matrix is:

    \[ P^{-1} =  {\left[ {\begin{array}{cccc}    \frac{1}{A} & 0 & 0 & 0 \\    0 & \frac{1}{B} & 0 & 0 \\    0 & 0 & 0 & -1 \\    0 & 0 & \frac{1}{D} & \frac{C}{D} \\   \end{array} } \right]} \]

Using this formula is definitely more efficient than using a generic 4-by-4 matrix inversion. The best part is that you don’t even have to memorize the formula for P^{-1} after seeing how you can easily derive it.

End of How to Eyeball The Inverse of A Matrix

Viewing matrix multiplication as linear combinations of columns of the left matrix or rows of the right matrix is a valuable technique. It grants us the ability to quickly eyeball the inverse of a reasonably-sized matrix.

About Allen Chou

Physics / Graphics / Procedural Animation / Visuals
This entry was posted in Gamedev, Math, Uncategorized. Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.