TExcelTAD
Home › Linear Algebra & Matrix Theory › Matrix Inverses

Matrix Inverses

A square matrix AA is invertible when some matrix "undoes" it: A−1A=AA−1=IA^{-1}A = AA^{-1} = I. This lesson proves the inverse is unique, gives the 2×22 \times 2 formula and the row-reduction method for any size, and explains why numerical code almost never computes A−1A^{-1} explicitly.

Definition

Let AA be n×nn \times n. A matrix BB is an inverse of AA if

AB=IandBA=I(I=In)AB = I \quad \text{and} \quad BA = I \qquad (I = I_n)

If such a BB exists, AA is invertible (or non-singular) and we write B=A−1B = A^{-1}. Otherwise AA is singular. Only square matrices can have two-sided inverses.

The inverse is unique. If BB and CC are both inverses of AA, then

B=BI=B(AC)=(BA)C=IC=CB = BI = B(AC) = (BA)C = IC = C

using only associativity. So "the" inverse is well defined.

Not every non-zero matrix is invertible. A=[1224]A = \begin{bmatrix} 1 & 2 \\ 2 & 4 \end{bmatrix} sends both [2,−1][2, -1] and [0,0][0, 0] to [0,0][0, 0]; no matrix can map 0\mathbf{0} back to two different vectors, so AA has no inverse.

The 2×22 \times 2 formula

For A=[abcd]A = \begin{bmatrix} a & b \\ c & d \end{bmatrix},

A−1=1ad−bc⋅[d−b−ca]provided ad−bc≠0A^{-1} = \frac{1}{ad - bc} \cdot \begin{bmatrix} d & -b \\ -c & a \end{bmatrix} \qquad \text{provided } ad - bc \ne 0

Swap the diagonal, negate the off-diagonal, divide by ad−bcad - bc. The number ad−bcad - bc is the determinant; if it is 0 the matrix is singular (Determinants of 2×2 and 3×3 Matrices). Multiplying out confirms AA−1=IAA^{-1} = I: the (1,1)(1, 1) entry is (ad−bc)/(ad−bc)=1(ad - bc)/(ad - bc) = 1 and the (1,2)(1, 2) entry is (−ab+ba)/(ad−bc)=0(-ab + ba)/(ad - bc) = 0.

Inverting by row reduction

For any n×nn \times n matrix, row-reduce the n×2nn \times 2n matrix [A∣I][A \mid I]:

  • if the left half becomes II, the right half is A−1A^{-1}: [A∣I]→[I∣A−1][A \mid I] \to [I \mid A^{-1}];
  • if the left half gets a zero row, AA 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 AA into II, namely A−1A^{-1}. It is the method of Gauss-Jordan Elimination applied to nn systems at once.

python · try it yourself
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 [0,0∣…][0, 0 \mid \ldots] on the left, so no inverse exists.

Properties

Property Note
(A−1)−1=A(A^{-1})^{-1} = A AA undoes A−1A^{-1}
(AB)−1=B−1A−1(AB)^{-1} = B^{-1}A^{-1} reverse order, like taking off socks and shoes
(cA)−1=(1/c)A−1(cA)^{-1} = (1/c)A^{-1} for c≠0c \ne 0
(Ak)−1=(A−1)k(A^k)^{-1} = (A^{-1})^k
(AT)−1=(A−1)T(A^T)^{-1} = (A^{-1})^T see The Transpose
If AA is invertible, Ax=bA\mathbf{x} = \mathbf{b} has the unique solution x=A−1b\mathbf{x} = A^{-1}\mathbf{b}
For square AA, AB=IAB = I already implies BA=IBA = I one side is enough

Proof of the reversal rule: (AB)(B−1A−1)=A(BB−1)A−1=AIA−1=AA−1=I(AB)(B^{-1}A^{-1}) = A(BB^{-1})A^{-1} = AIA^{-1} = AA^{-1} = I, and similarly in the other order.

The invertible matrix theorem collects equivalent conditions for an n×nn \times n matrix AA: A is invertible  ⟺  rref⁡(A)=I  ⟺  A has n pivots  ⟺  Ax=0 has only the solution x=0  ⟺  Ax=b has a unique solution for every b  ⟺  det⁡A≠0A \text{ is invertible} \iff \operatorname{rref}(A) = I \iff A \text{ has } n \text{ pivots} \iff A\mathbf{x} = \mathbf{0} \text{ has only the solution } \mathbf{x} = \mathbf{0} \iff A\mathbf{x} = \mathbf{b} \text{ has a unique solution for every } \mathbf{b} \iff \det A \ne 0. Later lessons add more items to this list.

In software: solve, don't invert

Mathematically x=A−1b\mathbf{x} = A^{-1}\mathbf{b}. Numerically, computing inv(A) and then multiplying is slower and less accurate than solving Ax=bA\mathbf{x} = \mathbf{b} directly with np.linalg.solve, which uses an LU factorisation (elimination) and back substitution:

  • Cost: solve needs about 23n3\frac{2}{3}n^3 flops; forming the inverse needs about 2n32n^3, and you still have to multiply by b\mathbf{b}.
  • Accuracy: on ill-conditioned matrices the explicit inverse amplifies rounding errors, so the residual ∥Ax−b∥\lVert A\mathbf{x} - \mathbf{b} \rVert is larger.
python · try it yourself
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 AA 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: 2×22 \times 2 inverse

Find the inverse of A=[3512]A = \begin{bmatrix} 3 & 5 \\ 1 & 2 \end{bmatrix} and check AA−1=IAA^{-1} = I.

Answer

A−1=[2−5−13]A^{-1} = \begin{bmatrix} 2 & -5 \\ -1 & 3 \end{bmatrix}.

Solution
ad−bc=3⋅2−5⋅1=1A−1=(1/1)⋅[2−5−13]check: [3⋅2+5(−1), 3(−5)+5⋅3]=[1,0][1⋅2+2(−1), 1(−5)+2⋅3]=[0,1] ✓\begin{aligned} ad - bc &= 3 \cdot 2 - 5 \cdot 1 = 1 \\ A^{-1} &= (1/1) \cdot \begin{bmatrix} 2 & -5 \\ -1 & 3 \end{bmatrix} \\ \text{check: } & [3 \cdot 2 + 5(-1),\ 3(-5) + 5 \cdot 3] = [1, 0] \\ & [1 \cdot 2 + 2(-1),\ 1(-5) + 2 \cdot 3] = [0, 1] \ \checkmark \end{aligned}

Exercise 2: Which k makes it singular?

For which value of kk is [k436]\begin{bmatrix} k & 4 \\ 3 & 6 \end{bmatrix} not invertible?

Answer

k=2k = 2.

Solution

The matrix is singular exactly when ad−bc=6k−12=0ad - bc = 6k - 12 = 0, i.e. k=2k = 2. Then the rows [2,4][2, 4] and [3,6][3, 6] are both multiples of [1,2][1, 2].

Exercise 3: Row-reduce to invert

Use [A∣I]→[I∣A−1][A \mid I] \to [I \mid A^{-1}] on A=[120010201]A = \begin{bmatrix} 1 & 2 & 0 \\ 0 & 1 & 0 \\ 2 & 0 & 1 \end{bmatrix}, and check with SymPy.

Answer

A−1=[1−20010−241]A^{-1} = \begin{bmatrix} 1 & -2 & 0 \\ 0 & 1 & 0 \\ -2 & 4 & 1 \end{bmatrix}.

Solution
[120100010010201001]R3−2R1:[0−41−201]R3+4R2:[001−241]R1−2R2:[1001−20]→A−1=[1−20010−241]\begin{aligned} & \left[\begin{array}{ccc|ccc} 1 & 2 & 0 & 1 & 0 & 0 \\ 0 & 1 & 0 & 0 & 1 & 0 \\ 2 & 0 & 1 & 0 & 0 & 1 \end{array}\right] \\ R_3 - 2R_1\text{:} \quad & \left[\begin{array}{ccc|ccc} 0 & -4 & 1 & -2 & 0 & 1 \end{array}\right] \\ R_3 + 4R_2\text{:} \quad & \left[\begin{array}{ccc|ccc} 0 & 0 & 1 & -2 & 4 & 1 \end{array}\right] \\ R_1 - 2R_2\text{:} \quad & \left[\begin{array}{ccc|ccc} 1 & 0 & 0 & 1 & -2 & 0 \end{array}\right] \\ \to A^{-1} &= \begin{bmatrix} 1 & -2 & 0 \\ 0 & 1 & 0 \\ -2 & 4 & 1 \end{bmatrix} \end{aligned}
python · try it yourself
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 P=[1101]P = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix} and Q=[2001]Q = \begin{bmatrix} 2 & 0 \\ 0 & 1 \end{bmatrix} that (PQ)−1(PQ)^{-1} equals Q−1P−1Q^{-1}P^{-1} but not P−1Q−1P^{-1}Q^{-1}.

Answer

(PQ)−1=Q−1P−1=[12−1201](PQ)^{-1} = Q^{-1}P^{-1} = \begin{bmatrix} \frac{1}{2} & -\frac{1}{2} \\ 0 & 1 \end{bmatrix}, while P−1Q−1=[12−101]P^{-1}Q^{-1} = \begin{bmatrix} \frac{1}{2} & -1 \\ 0 & 1 \end{bmatrix}.

Solution
python · try it yourself
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 [4724]\begin{bmatrix} 4 & 7 \\ 2 & 4 \end{bmatrix}?

  1. [4−7−24]\begin{bmatrix} 4 & -7 \\ -2 & 4 \end{bmatrix}
  2. [2−3.5−12]\begin{bmatrix} 2 & -3.5 \\ -1 & 2 \end{bmatrix}
  3. [1/41/71/21/4]\begin{bmatrix} 1/4 & 1/7 \\ 1/2 & 1/4 \end{bmatrix}
  4. It is singular

If AA and BB are invertible n×nn \times n matrices, what is (AB)−1(AB)^{-1}?

  1. B−1A−1B^{-1}A^{-1}
  2. A−1B−1A^{-1}B^{-1}
  3. (BA)−1(BA)^{-1} only if they commute
  4. It need not exist

Why does numerical code prefer np.linalg.solve(A, b) over np.linalg.inv(A) @ b?

  1. inv only works for 2×22 \times 2 matrices
  2. solve works without A being invertible
  3. They give identical results, it is style
  4. solve is faster and more accurate