Invert A Matrix
Goal¶
This post aims to show how to invert a matrix using numpy i.e., calculating a inverse matrix $A^{-1}$ from $A$
For example, if we have
$$A = \begin{bmatrix} a & b \\ c & d \end{bmatrix} $$Then, $A^{-1}$ should meet with
$$A A^{-1} = I = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} $$Reference:
Library¶
In [1]:
import numpy as np
from numpy.linalg import inv
Create a matrix¶
In [2]:
arr = np.array([[1, 2], [3, 4]])
arr
Out[2]:
Invert a matrix¶
In [3]:
arr_inv = inv(arr)
arr_inv
Out[3]:
Check $A A^{-1} = I$¶
In [4]:
np.dot(arr_inv, arr)
Out[4]:
In [5]:
np.dot(arr, arr_inv)
Out[5]:
Comments
Comments powered by Disqus