Computing the Inverse of Matrix

The inverse of a matrix is used in many, many machine learning algorithms. In ordinary mathematics, the inverse of a number n is a number ni so that n * ni = 1. For example, if n = 5 the inverse ni is 1/5 = 0.20 because 5 * 1/5 = 1. The matrix inverse extends this idea. The inverse of a matrix M is another matric MI so that M * MI = I where the * means matrix multiplication and I is the identity matrix (a matrix with 1s on the diagonal [upper left to lower right] and 0s elsewhere).

For example, if M =

 3.0   7.0   2.0   5.0
 1.0   8.0   4.0   2.0
 2.0   1.0   9.0   3.0
 5.0   4.0   7.0   1.0

then the inverse MI is:

 0.0971  -0.1827  -0.1147   0.2242
-0.0194   0.1456  -0.0680   0.0097
-0.0874   0.0644   0.1033  -0.0018
 0.2039  -0.1200   0.1227  -0.1474

because M * MI =

 1   0   0   0
 0   1   0   0
 0   0   1   0
 0   0   0   1

Computing the inverse of a matrix is very difficult. The technique I most often use is indirect – decompose the original matrix M into two matrices, then operate on the decompositions. There are several decomposition algorithms; I prefer Crout’s algorithm to alternatives, notably Doolittle’s algorithm.

I coded up a demo using JavaScript (mostly because I’ve been using a lot of JS recently). The code is very similar in other languages such as C#, Python, and Java. If you examine the code for function matrixInverse() in the image, it doesn’t look very long. The reason is that most of the work is done by helper functions matrixDecompose() and invHelper().



I have a file directory named What The Heck where I collect images that amuse me. Left: An ax is hazardous? Who would have guessed?! Right: I saw this street (“NE Turing St”) under construction near my high tech company workplace. The street should have been named Turing Incomplete.

This entry was posted in JavaScript, Machine Learning. Bookmark the permalink.