"### An Application of LU decomposition: Matrix Inversion\n",

"\n",

"One of those most common applications of of these approaches is to matrix inversion. In general, to invert a matrix $M$, we want to find the matrix $M^{-1}$ such that $MM^{-1}=I$, where $I$ is the identity matrix.\n",

"\n",

"Say we have a two by two matrix, we could write this as\n",

"where we want to solve for $m_{ij}'$. We can break this down and solve for one column of $M^{-1}$ at a time, giving us 2 linear systems to solve here, but $n$ for an $n\\times n$ matrix.\n",

"So we have a linear system, of the type $\\mathbf M \\mathbf x = \\mathbf b$, where we want to solve for a set of different $\\mathbf b$ but the same $\\mathbf M$. We saw here that LU decomposition gives a good general way to solve this set of systems and find the elements of the inverse.\n",

"\n",

"This is exactly how the the [scipy.linalg.inv](https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.inv.html) function operates (via an interface to LAPACK routines that actually do the work). Note that special types of matrices can have other approaches that are more optimal for computing an inverse, but there is no numpy or scipy function that calls these LAPACK routines implementing these methods currently. New features are added to SciPy all the time, so if you do know your matrix may have a better algorithm for inversion and it's large enough that it may be a time consuming part of your code I suggest double checking the SciPy documentation, or considering building an interface to a more optimal method.\n",

"\n",

"Note while you may be tempted to solve a linear system $\\mathbf M \\mathbf x = \\mathbf b$ by calculating $\\mathbf M^{-1}$ and evaluating $\\mathbf x = \\mathbf M^{-1}\\mathbf b$, it should be clear that this is a very sub-optimal approach as you will end up solving $N$ linear systems to invert the matrix, and accruing all the numerical errors associated with that, rather than solving the single linear system you had to begin with."