Lesson 9 of 18
Matrix Inverse
The Matrix Inverse
The inverse of a matrix is the matrix such that:
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 can be solved as — if 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.