Background
I wrote this code back on the second semester of my freshman year in university for an advanced level assignment. At the time I was still very much inexperienced with programming and it took a lot of pains to convert ideas to code. We can easily find the inverse of a matrix using libraries such as numpy or scipy, but I thought doing so would miss the intent of the question and would not net me the extra grade. To this day I do not know if I did end up getting it or not.
I had to follow video lectures on Youtube to learn about LU (lower-upper) decomposition. You can find the videos at the bottom of this post.
LU decomposition is computationally cheaper over Gauss-Jordan elimination (also linked a video below). Having said that, the python code I created will always be slower compared to the equivalent scipy or numpy functions which are implemented in C++ using LAPACK. No beating that :)
The Code
Identity Matrix Function
This function is used to create an identity matrix. It will be used in the inverse matrix function to create an identity matrix that is the same size as the input.
|
|
Inverse Matrix Function
Now the interesting part. As the name implies, we will first decompose our input nxn matrix into a lower matrix and an upper matrix. We do that using the code below. Note that a matrix in python is represented as a list of lists.
|
|
The first step of LU decomposition is Forward Elimination, which turns the lower triangular elements to zeros by creating, as I like to call it, eliminator rows using a multiplier calculated from the non-zero element divided by the diagonal matrix element directly above it. Subtract the row where the non-zero lower triangular element resides and cancel it out to 0. Repeat until we have our upper triangular matrix. This process of checking non-zeros goes column by column.
The multipliers used to create the subtraction rows are actually the elements of our lower triangular matrix. Their positions in the matrix are the same as the positions of the non-zero elements they helped cancel out. The diagonal elements of the lower triangular matrix are 1s and the upper triangle elements are 0s.
|
|
work in progress…