“Matrix Inverse from Scratch Using QR Decomposition with C#” in Visual Studio Magazine

I wrote an article titled “Matrix Inverse from Scratch Using QR Decomposition with C#” in the January 2024 edition of Microsoft Visual Studio Magazine. See https://visualstudiomagazine.com/Articles/2024/01/03/matrix-inverse.aspx.

There are several algorithms to compute a matrix inverse, and each algorithm has several variations. Three common algorithms are LUP (“lower upper permutation”), SVD (“singular value decomposition”) and QR (not an acronym). My article presents a from-scratch C# language implementation of matrix inverse using the Householder version of the QR algorithm.

I explained QR matrix inverse using an example. The demo sets up a small 4-by-4 matrix A:

  4.00  7.00  1.00  2.00
  6.00  0.00  3.00  5.00
  8.00  1.00  9.00  2.00
  2.00  5.00  6.00 -3.00

Then the demo computes the inverse Ai:

  0.5735  -1.2426   1.0221  -1.0074
  0.0000   0.2500  -0.2500   0.2500
 -0.4118   0.8088  -0.5735   0.6912
 -0.4412   1.2059  -0.8824   0.7941

The demo concludes by computing Ai * A to verify the result is the Identity matrix:

  1.0000   0.0000   0.0000   0.0000
  0.0000   1.0000   0.0000   0.0000
  0.0000   0.0000   1.0000   0.0000
  0.0000   0.0000   0.0000   1.0000

The demo MatInverseQR() function is based on QR decomposition. If you have an n-by-n matrix A and apply QR decomposition, the result is an n-by-n matrix Q and an n-by-n matrix R such that Q * R = A. The Q matrix is special because its inverse equals the transpose of Q (rows and columns exchanged). The R matrix is special because it is upper triangular — the values below the diagonal are 0s — and its inverse is easy to compute.

To compute the inverse of a matrix A = Q * R, the math derivation is:

A = Q * R
inv(A) = inv(Q * R)       
       = inv(R) * inv(Q)
       = inv(R) * trans(Q)

The implementation of QR-Householder matrix inverse presented in the article emphasizes simplicity and ease-of-modification over robustness and performance. Two other versions of the QR algorithm are Gram-Schmidt and Givens.



A mirror reflection is related to, but not the same as, an inverse image. I’m not a big fan of Dracula movies but some of the vampire lore is interesting. For example, vampires do not show a reflection in a mirror. Left: In “The Return of Dracula” (1958), Dracula travels from central Europe to a small town in Southern California. Low-budget film but it has a certain appeal. I give this movie a C+ grade. Center: In “Van Helsing” (2004), the Count throws a masquerade ball. When your dance partner doesn’t appear in a mirror, you should probably leave quickly. I give this movie a B grade. Right: In “House of Dracula” (1945), Dracula is looking for a cure for his vampirism. He doesn’t find it and he also runs into the Wolf Man and Frankenstein. I give this move a B- grade.


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