[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Find a matrix inverse in C#

[Find a matrix inverse in C#]

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:

[Find a matrix inverse in C#]

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

[Find a matrix inverse in C#]

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.

[Find a matrix inverse in C#]

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.

[Find a matrix inverse in C#]

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

[Find a matrix inverse in C#]

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.

[Find a matrix inverse in C#]

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

[Find a matrix inverse in C#]

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 more information about Gaussian elimination, go to Wolfram MathWorld or Wikipedia.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.