Matrix Inverses
A square matrix is invertible when some matrix "undoes" it: . This lesson proves the inverse is unique, gives the formula and the row-reduction method for any size, and explains why numerical code almost never computes explicitly.
Definition
Let be . A matrix is an inverse of if
If such a exists, is invertible (or non-singular) and we write . Otherwise is singular. Only square matrices can have two-sided inverses.
The inverse is unique. If and are both inverses of , then
using only associativity. So "the" inverse is well defined.
Not every non-zero matrix is invertible. sends both and to ; no matrix can map back to two different vectors, so has no inverse.
The formula
For ,
Swap the diagonal, negate the off-diagonal, divide by . The number is the determinant; if it is 0 the matrix is singular (Determinants of 2×2 and 3×3 Matrices). Multiplying out confirms : the entry is and the entry is .
Inverting by row reduction
For any matrix, row-reduce the matrix :
- if the left half becomes , the right half is : ;
- if the left half gets a zero row, is singular.
This works because each row operation is multiplication by an elementary matrix, and the product of all of them is the matrix that turns into , namely . It is the method of Gauss-Jordan Elimination applied to systems at once.
from sympy import Matrix, eye
A = Matrix([[2, 1, 1],
[1, 3, 2],
[1, 0, 0]])
aug = A.row_join(eye(3))
R, pivots = aug.rref()
print("rref of [A | I]:")
for r in range(3):
print(" ", list(R.row(r)))
Ainv = R[:, 3:]
print("A^-1 == A.inv():", Ainv == A.inv())
print("A * A^-1 == I:", A * Ainv == eye(3))
S = Matrix([[1, 2], [2, 4]])
print("singular rref:", S.row_join(eye(2)).rref()[0].tolist())rref of [A | I]:
[1, 0, 0, 0, 0, 1]
[0, 1, 0, -2, 1, 3]
[0, 0, 1, 3, -1, -5]
A^-1 == A.inv(): True
A * A^-1 == I: True
singular rref: [[1, 2, 0, 1/2], [0, 0, 1, -1/2]]
The singular case ends with the zero row on the left, so no inverse exists.
Properties
| Property | Note |
|---|---|
| undoes | |
| reverse order, like taking off socks and shoes | |
| for | |
| see The Transpose | |
| If is invertible, has the unique solution | |
| For square , already implies | one side is enough |
Proof of the reversal rule: , and similarly in the other order.
The invertible matrix theorem collects equivalent conditions for an matrix : . Later lessons add more items to this list.
In software: solve, don't invert
Mathematically . Numerically, computing inv(A) and then multiplying is slower and less accurate than solving directly with np.linalg.solve, which uses an LU factorisation (elimination) and back substitution:
- Cost: solve needs about flops; forming the inverse needs about , and you still have to multiply by .
- Accuracy: on ill-conditioned matrices the explicit inverse amplifies rounding errors, so the residual is larger.
import numpy as np
from scipy.linalg import hilbert
n = 12
A = hilbert(n) # famously ill-conditioned: entries 1/(i+j+1)
x_true = np.ones(n)
b = A @ x_true
x_solve = np.linalg.solve(A, b)
x_inv = np.linalg.inv(A) @ b
print(f"condition number: {np.linalg.cond(A):.1e}")
print(f"residual solve: {np.linalg.norm(A @ x_solve - b):.1e}")
print(f"residual inv : {np.linalg.norm(A @ x_inv - b):.1e}")
print("solve residual smaller:", np.linalg.norm(A @ x_solve - b) < np.linalg.norm(A @ x_inv - b))condition number: 1.8e+16
residual solve: 7.7e-16
residual inv : 1.1e-01
solve residual smaller: True
The rule of thumb in NumPy, SciPy, MATLAB (A\b) and Julia is the same: if you see inv(A) @ b, replace it with solve(A, b). If you need to solve with the same many times, factorise once (scipy.linalg.lu_factor) and reuse it. In games, the inverse of a rotation-plus-translation matrix is written down directly instead of computed (Matrix Inverse), and Numerical Linear Algebra covers conditioning in depth.
Exercises
Exercise 1: inverse
Find the inverse of and check .
Answer
.
Solution
Exercise 2: Which k makes it singular?
For which value of is not invertible?
Answer
.
Solution
The matrix is singular exactly when , i.e. . Then the rows and are both multiples of .
Exercise 3: Row-reduce to invert
Use on , and check with SymPy.
Answer
.
Solution
from sympy import Matrix
A = Matrix([[1, 2, 0], [0, 1, 0], [2, 0, 1]])
print(A.inv().tolist())[[1, -2, 0], [0, 1, 0], [-2, 4, 1]]
Exercise 4: Reverse order
Show with the matrices and that equals but not .
Answer
, while .
Solution
from sympy import Matrix
P = Matrix([[1, 1], [0, 1]]); Q = Matrix([[2, 0], [0, 1]])
print("(PQ)^-1 :", (P*Q).inv().tolist())
print("Q^-1 P^-1:", (Q.inv()*P.inv()).tolist())
print("P^-1 Q^-1:", (P.inv()*Q.inv()).tolist())(PQ)^-1 : [[1/2, -1/2], [0, 1]]
Q^-1 P^-1: [[1/2, -1/2], [0, 1]]
P^-1 Q^-1: [[1/2, -1], [0, 1]]
Test yourself
What is the inverse of ?
- It is singular
If and are invertible matrices, what is ?
- only if they commute
- It need not exist
Why does numerical code prefer np.linalg.solve(A, b) over np.linalg.inv(A) @ b?
- inv only works for matrices
- solve works without A being invertible
- They give identical results, it is style
- solve is faster and more accurate