TExcelTAD
Home › Linear Algebra & Matrix Theory › Application: A Formula for the Inverse

Application: A Formula for the Inverse

Cofactors give an explicit formula for the inverse of any invertible matrix: A−1=adj⁡(A)/det⁡AA^{-1} = \operatorname{adj}(A) / \det A, where the adjugate adj⁡(A)\operatorname{adj}(A) is the transposed matrix of cofactors. For 2×22 \times 2 it is the familiar "swap, negate, divide" rule; for 3×33 \times 3 and 4×44 \times 4 it is what game math libraries hard-code; and it explains why integer matrices with determinant ±1\pm 1 have integer inverses.

The adjugate

For an n×nn \times n matrix AA with cofactors CijC_{ij} (Minors and Cofactors), the cofactor matrix is C=[Cij]C = [C_{ij}] and the adjugate (classical adjoint) is its transpose:

adj⁡(A)=CT,i.e.adj⁡(A)ij=Cji\operatorname{adj}(A) = C^T, \qquad \text{i.e.} \quad \operatorname{adj}(A)_{ij} = C_{ji}

Theorem. For every square matrix AA,

A⋅adj⁡(A)=adj⁡(A)⋅A=(det⁡A)⋅IA \cdot \operatorname{adj}(A) = \operatorname{adj}(A) \cdot A = (\det A) \cdot I

so if det⁡A≠0\det A \ne 0,

A−1=1det⁡A⋅adj⁡(A)A^{-1} = \frac{1}{\det A} \cdot \operatorname{adj}(A)

Proof. The (i,k)(i, k) entry of A⋅adj⁡(A)A \cdot \operatorname{adj}(A) is ∑jaij⋅adj⁡(A)jk=∑jaijCkj\sum_j a_{ij} \cdot \operatorname{adj}(A)_{jk} = \sum_j a_{ij}C_{kj}.

  • If k=ik = i, this is the cofactor expansion of det⁡A\det A along row ii, so the diagonal entries equal det⁡A\det A.
  • If k≠ik \ne i, it is the cofactor expansion along row kk of the matrix A′A' obtained from AA by replacing row kk with a copy of row ii (the cofactors CkjC_{kj} do not involve row kk, so they are the same for AA and A′A'). A′A' has two equal rows, so its determinant is 0.

Hence A⋅adj⁡(A)=(det⁡A)⋅IA \cdot \operatorname{adj}(A) = (\det A) \cdot I. The other order follows the same way with column expansions. ∎

The 2×22 \times 2 case

For A=[abcd]A = \begin{bmatrix} a & b \\ c & d \end{bmatrix} the cofactors are C11=dC_{11} = d, C12=−cC_{12} = -c, C21=−bC_{21} = -b, C22=aC_{22} = a, so

C=[d−c−ba],adj⁡(A)=CT=[d−b−ca],A−1=1ad−bc⋅[d−b−ca]C = \begin{bmatrix} d & -c \\ -b & a \end{bmatrix}, \qquad \operatorname{adj}(A) = C^T = \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}, \qquad A^{-1} = \frac{1}{ad - bc} \cdot \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}

exactly the formula from Matrix Inverses.

A 3×33 \times 3 example

python · try it yourself
from sympy import Matrix

A = Matrix([[2, 3, 1],
            [1, 2, 1],
            [1, 1, 1]])

C = Matrix(3, 3, lambda i, j: A.cofactor(i, j))      # cofactor matrix
adj = C.T
print("cofactors:", C.tolist())
print("adj(A)   :", adj.tolist(), " same as SymPy:", adj == A.adjugate())
print("det A    :", A.det())
print("A adj(A) :", (A * adj).tolist())
print("A^-1     :", (adj / A.det()).tolist(), " correct:", adj / A.det() == A.inv())
cofactors: [[1, 0, -1], [-2, 1, 1], [1, -1, 1]]
adj(A)   : [[1, -2, 1], [0, 1, -1], [-1, 1, 1]]  same as SymPy: True
det A    : 1
A adj(A) : [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
A^-1     : [[1, -2, 1], [0, 1, -1], [-1, 1, 1]]  correct: True

Because det⁡A=1\det A = 1, the inverse is the adjugate itself and has integer entries.

Consequences

Integer inverses. If AA has integer entries, so does adj⁡(A)\operatorname{adj}(A) (cofactors are sums of products of entries). So A−1A^{-1} has integer entries whenever det⁡A=±1\det A = \pm 1. Such matrices are called unimodular. Conversely, if AA and A−1A^{-1} are both integer matrices, det⁡A⋅det⁡A−1=1\det A \cdot \det A^{-1} = 1 with both factors integers, so det⁡A=±1\det A = \pm 1.

Inverses mod mm. The formula only divides once, by det⁡A\det A. So AA is invertible modulo mm exactly when det⁡A\det A has an inverse mod mm, and then A−1≡(det⁡A)−1⋅adj⁡(A)(modm)A^{-1} \equiv (\det A)^{-1} \cdot \operatorname{adj}(A) \pmod{m}. This is how the Hill cipher key is inverted mod 26 (Application: Cryptography).

Continuity. Each entry of A−1A^{-1} is a polynomial in the entries of AA divided by det⁡A\det A, so A−1A^{-1} depends smoothly on AA as long as det⁡A\det A stays away from 0, and blows up as det⁡A→0\det A \to 0.

Cramer's rule, the next lesson, is the same formula applied to a system: x=A−1b=adj⁡(A)b/det⁡A\mathbf{x} = A^{-1}\mathbf{b} = \operatorname{adj}(A)\mathbf{b} / \det A.

In software

  • Fixed-size inverses in game engines. GLM, DirectXMath, Unity and Unreal invert 3×33 \times 3 and 4×44 \times 4 matrices with hand-unrolled cofactor formulas: the 2×22 \times 2 sub-determinants are computed once and reused, giving a branch-free routine that maps well to SIMD. For general nn, libraries use LU instead (Matrix Inverse).
  • Do you even need it? For rotation-plus-translation matrices, the inverse is cheaper still: transpose the rotation and rotate the negated translation. And for solving systems, prefer solve over any explicit inverse.
  • Cost for large nn: computing n2n^2 cofactors, each an (n−1)×(n−1)(n-1) \times (n-1) determinant, is O(n5)O(n^5) even with fast determinants (and factorial with naive expansion), versus O(n3)O(n^3) for Gauss-Jordan.
python · try it yourself
import numpy as np

def inv3(m):
    # cofactor-based 3x3 inverse, the way small-matrix libraries write it
    a, b, c = m[0]; d, e, f = m[1]; g, h, i = m[2]
    A, B, C = e * i - f * h, -(d * i - f * g), d * h - e * g     # cofactors of row 1
    det = a * A + b * B + c * C
    adj = np.array([[A, -(b * i - c * h), b * f - c * e],
                    [B, a * i - c * g, -(a * f - c * d)],
                    [C, -(a * h - b * g), a * e - b * d]])
    return adj / det

M = np.array([[2.0, 0.5, 1.0], [0.0, 1.5, -1.0], [1.0, 2.0, 3.0]])
print(np.allclose(inv3(M), np.linalg.inv(M)), np.allclose(M @ inv3(M), np.eye(3)))
True True

Exercises

Exercise 1: 2×22 \times 2 by the formula

Find the inverse of [7352]\begin{bmatrix} 7 & 3 \\ 5 & 2 \end{bmatrix} using the adjugate.

Answer

[−235−7]\begin{bmatrix} -2 & 3 \\ 5 & -7 \end{bmatrix}.

Solution
det⁡=7⋅2−3⋅5=−1adj⁡=[2−3−57]A−1=adj⁡/(−1)=[−235−7]check: 7⋅(−2)+3⋅5=1,7⋅3+3⋅(−7)=0 ✓\begin{aligned} \det &= 7 \cdot 2 - 3 \cdot 5 = -1 \\ \operatorname{adj} &= \begin{bmatrix} 2 & -3 \\ -5 & 7 \end{bmatrix} \\ A^{-1} &= \operatorname{adj} / (-1) = \begin{bmatrix} -2 & 3 \\ 5 & -7 \end{bmatrix} \\ \text{check: } & 7 \cdot (-2) + 3 \cdot 5 = 1, \quad 7 \cdot 3 + 3 \cdot (-7) = 0 \ \checkmark \end{aligned}

Exercise 2: One entry only

For A=[210131014]A = \begin{bmatrix} 2 & 1 & 0 \\ 1 & 3 & 1 \\ 0 & 1 & 4 \end{bmatrix}, find only the entry in row 1, column 3 of A−1A^{-1}.

Answer

(A−1)13=118(A^{-1})_{13} = \frac{1}{18}.

Solution

(A−1)13=adj⁡(A)13/det⁡A=C31/det⁡A(A^{-1})_{13} = \operatorname{adj}(A)_{13} / \det A = C_{31} / \det A.

C31=(−1)3+1⋅det⁡[1031]=1det⁡A=2(12−1)−1(4−0)+0=22−4=18(A−1)13=118\begin{aligned} C_{31} &= (-1)^{3+1} \cdot \det \begin{bmatrix} 1 & 0 \\ 3 & 1 \end{bmatrix} = 1 \\ \det A &= 2(12 - 1) - 1(4 - 0) + 0 = 22 - 4 = 18 \\ (A^{-1})_{13} &= \frac{1}{18} \end{aligned}
python · try it yourself
from sympy import Matrix
A = Matrix([[2, 1, 0], [1, 3, 1], [0, 1, 4]])
print(A.inv()[0, 2], A.det())
1/18 18

Exercise 3: Integer inverse?

Which of these integer matrices have integer inverses: P=[3512]P = \begin{bmatrix} 3 & 5 \\ 1 & 2 \end{bmatrix}, Q=[2413]Q = \begin{bmatrix} 2 & 4 \\ 1 & 3 \end{bmatrix}, R=[110011001]R = \begin{bmatrix} 1 & 1 & 0 \\ 0 & 1 & 1 \\ 0 & 0 & 1 \end{bmatrix}?

Answer

PP and RR do (det⁡1\det 1); QQ does not (det⁡2\det 2).

Solution

det⁡P=6−5=1\det P = 6 - 5 = 1, det⁡Q=6−4=2\det Q = 6 - 4 = 2, det⁡R=1\det R = 1 (triangular). Q−1=12[3−4−12]Q^{-1} = \frac{1}{2}\begin{bmatrix} 3 & -4 \\ -1 & 2 \end{bmatrix} contains halves.

Exercise 4: Prove det⁡(adj⁡A)=(det⁡A)n−1\det(\operatorname{adj} A) = (\det A)^{n-1}

For an invertible n×nn \times n matrix, derive det⁡(adj⁡A)=(det⁡A)n−1\det(\operatorname{adj} A) = (\det A)^{n-1} and check it for the 3×33 \times 3 matrix of Exercise 2.

Answer

det⁡(adj⁡A)=182=324\det(\operatorname{adj} A) = 18^2 = 324 for that matrix.

Solution

Take determinants of A⋅adj⁡(A)=(det⁡A)⋅IA \cdot \operatorname{adj}(A) = (\det A) \cdot I: det⁡A⋅det⁡(adj⁡A)=(det⁡A)n\det A \cdot \det(\operatorname{adj} A) = (\det A)^n. Divide by det⁡A≠0\det A \ne 0.

python · try it yourself
from sympy import Matrix
A = Matrix([[2, 1, 0], [1, 3, 1], [0, 1, 4]])
print(A.adjugate().det(), A.det() ** 2)
324 324

Test yourself

What is adj⁡(A)\operatorname{adj}(A) for A=[4162]A = \begin{bmatrix} 4 & 1 \\ 6 & 2 \end{bmatrix}?

  1. [2−6−14]\begin{bmatrix} 2 & -6 \\ -1 & 4 \end{bmatrix}
  2. [2−1−64]\begin{bmatrix} 2 & -1 \\ -6 & 4 \end{bmatrix}
  3. [4−1−62]\begin{bmatrix} 4 & -1 \\ -6 & 2 \end{bmatrix}
  4. [−216−4]\begin{bmatrix} -2 & 1 \\ 6 & -4 \end{bmatrix}

What is A⋅adj⁡(A)A \cdot \operatorname{adj}(A) for any square matrix AA?

  1. (det⁡A)⋅I(\det A) \cdot I
  2. II
  3. ATA^T
  4. 0

An integer matrix has det⁡A=−1\det A = -1. What can you say about A−1A^{-1}?

  1. It does not exist
  2. It has entries that are multiples of 12\frac{1}{2}
  3. It equals AA
  4. It has integer entries