Matrix Factorization in Python

Maniru Ibrahim
Matrix Factorization in Python
3 min readMay 18, 2020

To solve a linear system of equations Ax = b, we start with the matrix A and arrived at matrix U called the upper triangular matrix. Matrix Factorization allows matrix A to be factored into the product of a lower-triangular matrix L and an upper-triangular matrix U, i.e. A = LU.

Example

Theorem

If Gaussian elimination can be performed on the linear system Ax = b
without row interchanges, then the matrix A can be factored into the
product of a lower-triangular matrix L and an upper-triangular matrix U,
that is, A = LU.

Here we write a python program that will take augmented matrix of the system Ax=b as input and factored the matrix A into product of L and U, and also find the unknowns. The program checks if there is need for row exchange, if so, then it will display “The Matrix cannon be factored”

We start by defining the function LU which takes the augmented matrix and gives the L, U and the unknowns, and then extract b from the augmented matrix:

And declared and initialized L and U, and check if there is need for row exchange:

It is time to factor L and U: Let’s move fast

We now solve Ly=b to find y ’s:

Now, let’s solve Ux=y to find x:

The program will return the lower triangular matrix U, upper triangular matrix L, and the values of the unknowns.

We are done, lets enjoy.

IG: Manirmaths

Twitter:Manirmaths

--

--