How To Do Matrix Inversion Using C#

I wrote an article titled “How To Invert a Matrix Using C#” in the April 2020 edition of Visual Studio magazine. See https://visualstudiomagazine.com/Home.aspx.

Inverting a matrix is one of the most common tasks in data science and machine learning. In my article I explain why inverting a matrix is very difficult and present C# code that can be used as-is, or as a starting point for custom matrix inversion code. Specifically, my article presents an implementation of matrix inversion using Crout’s decomposition.



There are many different techniques to invert a matrix. The Wikipedia article on matrix inversion lists 10 categories of techniques, and each category has many variations. The fact that there are so many different ways to invert a matrix is an indirect indication of how difficult the problem is. Briefly, relatively simple matrix inversion techniques such as using cofactors and adjugates only work well for small matrices (roughly 10 x 10 or smaller). For larger matrices you should write code that involves a complex technique called matrix decomposition.

In regular arithmetic, the inverse of a number z is a number that when multiplied by z gives 1. For example, if z = 4, the inverse of z is 1/4 = 0.25 because 4 * (1/4) = 1.

Matrix inversion extends this idea. The inverse of an n x n (called a square matrix because the number of rows equals the number of columns) matrix m is a matrix inv such that m * inv = I where I is the identity matrix (1s on the diagonal, 0s elsewhere).

And in regular arithmetic you can decompose a number z into two values a and b so that the product a * b equals z. For example if z = 30, one decomposition is a = 5 and b = 6. Notice that there are many possible decompositions of a number. Matrix decomposition extends this idea by finding two matrices that when multiplied together give a source matrix.



Although it’s not at all obvious, the inverse of a matrix can be computed from the upper and lower decompositions. Somewhat surprisingly, even though matrix decomposition is difficult, it’s easier to compute a decomposition of a source matrix and then use the decomposition to compute the inverse of the source matrix, than it is to compute the inverse directly.


Three images from an Internet search for “inversion”. Left: By photographer Serge Averbukh. Center: By artist Heather Patterson. Right: By photographer Marianne Breslauer (1909-2001) who is well-known for photos of the pre-war German Weimar Republic.

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

1 Response to How To Do Matrix Inversion Using C#

  1. Thorsten Kleppe's avatar Thorsten Kleppe says:

    Hello Dr. McCaffrey,
    the download link is broken, but replace .cs with .zip works.

    The article is really nicely explained, hopefully I will learn it quick.

    A cool image inversion trick is “contrastful gamma”
    1. invert image
    2. increase gamma
    3. normalize the image

    Instead of a milky bright picture a more colorful image rises up.

Comments are closed.