“Matrix Inverse Using Cayley-Hamilton with C#” in Visual Studio Magazine

I wrote an article titled “Matrix Inverse Using Cayley-Hamilton with C#” in the June 2025 edition of Microsoft Visual Studio Magazine. See https://visualstudiomagazine.com/articles/2025/06/16/matrix-inverse-using-cayley-hamilton-with-csharp.aspx.

Dozens of machine learning algorithms require computing the inverse of a matrix. Computing a matrix inverse is conceptually easy, but implementation is one of the most challenging tasks in numerical programming.

The Wikipedia article on matrix inverse lists 14 different algorithms, and each algorithm has multiple variations, and each variation has dozens of implementation alternatives.

My article presents a complete end-to-end demonstration of computing a matrix inverse using a technique called Cayley-Hamilton. Compared to other algorithms, Cayley-Hamilton is very simple and easiest to customize. The main disadvantage of Cayley-Hamilton is that it’s not suitable for matrices larger than about 200-by-200, and the algorithm can easily fail even for small matrices.

The output of the demo program is:

Begin matrix inverse using Cayley-Hamilton

Source matrix A:
   1.0   2.0   3.0   1.0   5.0
   0.0   5.0   4.0   1.0   4.0
   6.0   1.0   0.0   2.0   2.0
   1.0   4.0   5.0   3.0   2.0
   0.0   2.0   4.0   0.0   1.0

Inverse:
  -0.0738   0.0000   0.1967  -0.1066   0.1885
  -0.2984   0.4000   0.0623  -0.0754  -0.0820
   0.0836  -0.2000  -0.0230   0.0541   0.3197
   0.1082  -0.2000  -0.0885   0.4230  -0.4098
   0.2623   0.0000  -0.0328  -0.0656  -0.1148

Checking A * Ainv = I
   1.0000   0.0000   0.0000   0.0000   0.0000
   0.0000   1.0000   0.0000   0.0000   0.0000
   0.0000   0.0000   1.0000   0.0000   0.0000
   0.0000   0.0000   0.0000   1.0000   0.0000
   0.0000   0.0000   0.0000   0.0000   1.0000

Check PASS

End demo

The Cayley-Hamilton techniques has two main steps:

1. Compute the coefficients of the characteristic polynomial of the source matrix.
2. Use the coefficients to compute the inverse of the source matrix.

The coefficients of the characteristic polynomial are usually computed using the Faddeev-LeVerrier algorithm. (There are less commonly used algorithms too).

The Cayley-Hamilton technique for matrix inverse isn’t used very often. If the source matrix A is size 100-by-100, the algorithm must compute A^100 = A * A . . . * A (100 terms). This is slow, and if the cell values are all positive or all negative, the computation can easily cause arithmetic overflow.

But in some scenarios, the Cayley-Hamilton technique can be very useful



All matrix inverse algorithms, including Cayley-Hamilton, can fail. All baking attempts can fail. Especially cakes that dare you to eat them. Menacing clown. Threatening bunny. Intimidating unicorn.


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

Leave a Reply