Lesson 9 of 18

Matrix Inverse

The Matrix Inverse

The inverse of a matrix mathbfAmathbf{A} is the matrix mathbfA1mathbf{A}^{-1} such that:

mathbfAcdotmathbfA1=mathbfA1cdotmathbfA=Imathbf{A} cdot mathbf{A}^{-1} = mathbf{A}^{-1} cdot mathbf{A} = I

When Does an Inverse Exist?

A matrix is invertible (non-singular) if and only if its determinant is non-zero:

def det(A):
    if len(A) == 1: return A[0][0]
    if len(A) == 2: return A[0][0]*A[1][1] - A[0][1]*A[1][0]
    return sum(((-1)**j)*A[0][j]*det([[A[i][k] for k in range(len(A)) if k!=j]
                                      for i in range(1,len(A))]) for j in range(len(A)))

A = [[2, 1], [1, 1]]
S = [[1, 2], [2, 4]]  # det = 0 (singular)

print(abs(det(A)) > 1e-10)   # True
print(abs(det(S)) > 1e-10)   # False

Why Invertibility Matters

The equation mathbfAmathbfx=mathbfbmathbf{A}mathbf{x} = mathbf{b} can be solved as mathbfx=mathbfA1mathbfbmathbf{x} = mathbf{A}^{-1}mathbf{b}if mathbfAmathbf{A} is invertible. In practice, use Gaussian elimination instead (it's faster and more numerically stable).

Your Task

Implement is_invertible(A) that returns True if the matrix is invertible, False otherwise. Use the determinant.

Pyodide loading...
Loading...
Click "Run" to execute your code.