Find a matrix inverse in C#

[matrix inverse]

This example finds a matrix inverse for a square matrix. The inverse of a matrix is another matrix that, when multiplied by the first, gives the identity matrix as a result. In the identity matrix, all entries are 0 except the diagonal entries which are 1.

Enter the matrix data, separating rows by carriage returns and entries in rows by spaces. When you click the Invert button, the program parses the values you entered to build a matrix. It then uses Gauss-Jordan Elimination to find the matrix inverse.

For a rigorous description of Gauss-Jordan Elimination, see Wolfram MathWorld or Wikipedia, but here’s the basic idea.

Suppose the matrix looks like this:


[matrix inverse]

First make an augmented matrix by adding an identity matrix to the right of the original values.


[matrix inverse]

Now use Gaussian elimination to manipulate the matrix until the first three columns look like the identity matrix. To do that, you start at the first row. To make a 1 appear in the first column, you divide all of the elements in that row by its first entry. The following picture shows the modified augmented matrix.


[matrix inverse]

Now you need to zero-out the entries in the first column of the other rows. To zero-out the first column in row R, you subtract a multiple of the first row from row R’s elements.

For example, in the picture above row 1 has the value A10 in its first column. To fix row 1, you subtract A10 times each element in the first row from the corresponding elements in row R.

The entry in column 0 becomes A10 – A10 * 1 = 0 as desired.

The entry in column 1 becomes A11 – A10 * A01 / A00.

The entry in column 2 becomes A12 – A10 * A02 / A00.

You continue this for all of the columns in row 1 including the new columns in the augmented section on the right. The following picture shows the result after zeroing out the first column in row 1. Here I’ve replaced entries with messy values with question marks.


[matrix inverse]

Now you repeat the process for row 2. The following picture shows the result.


[matrix inverse]

At this point, column 1’s first row has value 1 and all of the other rows have value 0. You now repeat the process for the second column. That gives you the following matrix.


[matrix inverse]

When you repeat the process for the third column, you get the following matrix.


[matrix inverse]

Now the 3×3 left half of the augmented matrix looks like an identity matrix. At this point you’re done and the 3×3 matrix in the right half gives you the inverse of the original matrix.

There are still some details that I haven’t covered. For example, sometimes you may be trying to put a 1 in row N column N, but that entry already contains a 0. In that case, you can’t divide its entries by 0. However, you can swap that row with a later row and then continue.

Some matrices also have no inverse. In that case this method fails. For example, you may come to a point where there are no remaining rows with a non-zero value in the column where you are trying to place a 1. In that case you know there is no matrix inverse.

After the program finds the matrix inverse, it multiplies the original matrix by the calculated inverse and displays the result, which should be the identity matrix.

For additional details, download the example and look at the code, or go to Wolfram MathWorld or Wikipedia.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in Uncategorized and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *