# How does numpy.linalg.inv calculate the inverse of a matrix?

What is the algorithm behind this routine and is there documentation available for it?

• Did you check the numpy source code?
– qwr
Commented Feb 10 at 22:08

The source code of this function is here, which is a wrapper for a C++ function here, which is calling some BLAS/LAPACK library functions. Specifically, in order to compute the inverse of a matrix $$A$$, it calls the "SGESV" routine to solve the equation $$AX = I$$ for the matrix $$X$$. (note that SGESV can solve arbitrary systems $$AX = B$$; $$B$$ does not have to be the identity matrix)
The SGESV routine first computes an LU-decomposition of $$A$$: it finds a lower triangular matrix $$L$$ and upper triangular matrix $$U$$ such that $$L U = A$$. Solving a system with a triangular matrix is easy, as we can go row by row; we only need to solve an equation with one unknown variable for each row. So, we can solve $$AX=B$$ as follows: let $$X':=UX$$, then we have $$LX' = A$$ and $$UX= X'$$. These are both systems with a triangular matrix, and solving them gives $$X$$.