Determinant and Trace
Before factoring a matrix it helps to have a couple of numbers that summarise it. This page gives two: the determinant, which is a signed volume and decides invertibility, and the trace, which is the sum of the diagonal.
Both are cheap, both are single numbers, and — the property that makes them worth defining — both are invariant under a change of basis. They describe the linear mapping, not the particular matrix you happened to write down for it.
What you’ll learn
Section titled “What you’ll learn”- The determinant for , the Laplace expansion for general , and the triangular shortcut.
- Why , invertibility, and full rank are the same condition (Theorems 4.1 and 4.3).
- The determinant as a signed volume, verified on the book’s Example 4.2 where the answer is .
- The seven properties of the determinant, each measured.
- The trace, its four defining properties, and the cyclic-permutation identity that makes it useful.
- Equation 4.21: the trace of a linear mapping is basis independent, measured over two thousand random changes of basis.
- The characteristic polynomial, whose constant term is the determinant and whose next coefficient is the trace.
Intuition: how much does this map stretch space?
Section titled “Intuition: how much does this map stretch space?”Feed the unit square through a matrix and you get a parallelogram. Its area is . Feed the unit cube through a matrix and you get a parallelepiped, whose volume is again .
That one sentence explains most of the determinant’s behaviour before any algebra:
- means the volume collapsed, so the map squashed space into a lower dimension, so it threw information away, so it cannot be inverted.
- because applying two maps in sequence multiplies their stretch factors.
- because scaling all edges of a box by scales its volume by .
- The sign records orientation: whether the map preserved handedness or mirrored it.
The trace has no such picture in general — it is a sum of diagonal entries, which are basis-dependent quantities. What makes it meaningful is that the sum is not.
flowchart TD A["a square matrix A"] A --> DET["det A = signed volume
Eq 4.1"] A --> TR["tr A = sum of the diagonal
Eq 4.18"] DET --> INV["det A ≠ 0 ⟺ invertible
⟺ full rank
Thm 4.1, 4.3"] DET --> PROD["det(AB) = det A · det B
det(λA) = λⁿ det A"] TR --> CYC["tr(AKL) = tr(KLA)
invariant under cyclic permutation
Eq 4.19"] DET --> BASIS["both survive A ↦ S⁻¹AS
Eq 4.21"] TR --> BASIS BASIS --> MAP["so they describe the MAPPING,
not the matrix"] DET --> CP["characteristic polynomial
p(λ) = det(A − λI)
Def 4.5"] TR --> CP CP --> EIG["→ eigenvalues, §4.2"]
The math
Section titled “The math”Small determinants
Section titled “Small determinants”The case is not arbitrary. Example 4.1 derives it: the inverse of a matrix is
so the matrix is invertible exactly when that denominator is nonzero. The determinant is not a formula someone invented; it is the quantity that has to be nonzero for the inverse to exist, extracted and named.
Triangular matrices, and the fast route
Section titled “Triangular matrices, and the fast route”This is the whole practical algorithm. Gaussian elimination brings a matrix to triangular form using only operations whose effect on the determinant is known — adding a multiple of one row to another changes nothing, scaling a row by multiplies by , swapping two rows flips the sign — so you eliminate, multiply the diagonal, and correct for what you did.
The Laplace expansion
Section titled “The Laplace expansion”The recursion is correct and unusable. Expanding an determinant needs sub-determinants of size , so the count satisfies — which grows like . Measured against Gaussian elimination’s :
| Laplace multiplications | Gaussian elimination | ratio | |
|---|---|---|---|
| 3 | |||
| 5 | |||
| 10 | |||
| 15 | |||
| 20 |
At the recursion is actually cheaper, which is why hand calculation uses it and why it feels reasonable. By it is fifteen orders of magnitude worse.
The seven properties
Section titled “The seven properties”For :
| property | statement | measured gap |
|---|---|---|
| multiplicative | ||
| transpose-invariant | ||
| inverse | ||
| similarity-invariant | similar matrices have the same determinant | see the figure |
| row addition | adding a multiple of a row to another leaves unchanged | |
| row scaling | scaling a row by scales by ; so | exact ratio and |
| row swap | swapping two rows flips the sign | exact ratio |
All measured on random matrices. The last three are what license Gaussian elimination as a determinant algorithm.
The trace
Section titled “The trace”The fourth property is stranger than it looks: is and is , so those are matrices of different sizes with the same trace. Measured on a times a : both traces come to , from a and a respectively.
It generalises to invariance under cyclic permutation (Equation 4.19):
and specialises, for vectors, to Equation 4.20:
That last identity is the one you will actually use. It turns the trace of an outer product into a single dot product, and it is how every “trace trick” derivation in machine learning starts.
Basis independence
Section titled “Basis independence”The proof is one application of cyclic invariance. This is the property that promotes both quantities from “facts about a matrix” to “facts about a mapping”, and the reason they belong in the same section as the eigenvalues, which share it.
The characteristic polynomial
Section titled “The characteristic polynomial”Set and the first identity is immediate. The second takes a little more work and is worth knowing because it means the determinant and the trace are two coefficients of the same polynomial — the bottom one and the top-but-one. §4.2 will show that the polynomial’s roots are the eigenvalues, at which point and follow as Theorems 4.16 and 4.17.
Worked example by hand
Section titled “Worked example by hand”Example 4.3 — Laplace expansion
Section titled “Example 4.3 — Laplace expansion”Expand along the first row, so in Equation 4.13:
Each by Equation 4.6:
| minor | working | value |
|---|---|---|
Cross-check with Sarrus’ rule (Equation 4.17):
Both routes give , matching Equations 4.16 and 4.17.
A third route is faster than either. The third row is , so expanding along that row leaves one term:
Laplace lets you choose the row or column, and choosing the one with the most zeros is free.
Example 4.2 — volume
Section titled “Example 4.2 — volume”Sarrus:
so , which is Equation 4.11. The determinant itself is : the three vectors form a left-handed frame.
import numpy as np
# Example 4.3
A = np.array([[1.0, 2.0, 3.0], [3.0, 1.0, 2.0], [0.0, 0.0, 1.0]])
print("det:", float(np.linalg.det(A)))
print("Laplace, first row: 1*(1) - 2*(3) + 3*(0) =", 1 * 1 - 2 * 3 + 3 * 0)
print("Laplace, third row: 1*(1*1 - 2*3) =", 1 * (1 * 1 - 2 * 3))
# Example 4.2
r, g, b = np.array([2.0, 0, -8]), np.array([6.0, 1, 0]), np.array([1.0, 4, -1])
V = np.stack([r, g, b], axis=1)
print("det [r, g, b] =", float(np.linalg.det(V)), " volume =", abs(float(np.linalg.det(V))))
# The seven properties, measured on random 5x5 matrices.
rng = np.random.default_rng(11)
X, Y = rng.normal(size=(5, 5)), rng.normal(size=(5, 5))
dX = float(np.linalg.det(X))
print("det(XY) - det(X)det(Y):", f"{abs(float(np.linalg.det(X @ Y)) - dX * float(np.linalg.det(Y))):.3e}")
print("det(X) - det(X^T) :", f"{abs(dX - float(np.linalg.det(X.T))):.3e}")
print("det(inv X) - 1/det(X) :", f"{abs(float(np.linalg.det(np.linalg.inv(X))) - 1 / dX):.3e}")
print("det(2.7 X)/det(X) =", round(float(np.linalg.det(2.7 * X)) / dX, 6), " 2.7^5 =", round(2.7 ** 5, 6))
Z = X.copy(); Z[1] += 3.4 * Z[0]
print("row addition changes det by:", f"{abs(float(np.linalg.det(Z)) - dX):.3e}")
Z = X.copy(); Z[[0, 2]] = Z[[2, 0]]
print("row swap det ratio:", round(float(np.linalg.det(Z)) / dX, 12))
Z = X.copy(); Z[2] *= 4.0
print("row scaling det ratio:", round(float(np.linalg.det(Z)) / dX, 10))
# Triangular shortcut.
T = np.triu(rng.normal(size=(6, 6)))
print("det(T):", f"{float(np.linalg.det(T)):.10f}", " prod(diag):", f"{float(np.prod(np.diag(T))):.10f}")det: -5.000000000000001
Laplace, first row: 1*(1) - 2*(3) + 3*(0) = -5
Laplace, third row: 1*(1*1 - 2*3) = -5
det [r, g, b] = -185.99999999999991 volume = 185.99999999999991
det(XY) - det(X)det(Y): 5.329e-15
det(X) - det(X^T) : 2.665e-15
det(inv X) - 1/det(X) : 5.551e-17
det(2.7 X)/det(X) = 143.48907 2.7^5 = 143.48907
row addition changes det by: 3.553e-15
row swap det ratio: -1.0
row scaling det ratio: 4.0
det(T): -0.1289325988 prod(diag): -0.1289325988Two things in that output. The determinant of the integer matrix comes out at
, not , because np.linalg.det runs an LU factorisation in floating point;
Sarrus’ rule on integers would give exactly . And the volume prints as for the
same reason — the book’s is the exact answer and this is the computed one.
See it move
Section titled “See it move”The first sketch is the determinant as a signed area, with the sign flip visible.
The second sketch is the three row operations, each with its known effect on the determinant. This is the justification for computing determinants by elimination.
And the matrix stepper, which reports the determinant and the trace alongside the geometry:
Watch the unit square's image: its area is the absolute determinant. The eigenvalues the stepper reports multiply to that determinant and add to the trace, which is section 4.2's Theorems 4.16 and 4.17.
From scratch
Section titled “From scratch”import numpy as np
def det_laplace(A):
"""Theorem 4.2, recursively. Correct, and never use it beyond n = 4."""
A = np.asarray(A, dtype=float)
n = A.shape[0]
if n == 1:
return A[0, 0]
total = 0.0
for k in range(n): # expand along column 0
minor = np.delete(np.delete(A, k, axis=0), 0, axis=1)
total += (-1) ** k * A[k, 0] * det_laplace(minor)
return total
def det_elimination(A):
"""The practical route: eliminate, then multiply the diagonal."""
M = np.array(A, dtype=float)
n = M.shape[0]
sign = 1.0
for c in range(n):
piv = int(np.argmax(np.abs(M[c:, c]))) + c
if abs(M[piv, c]) < 1e-14:
return 0.0 # a zero column: singular
if piv != c:
M[[c, piv]] = M[[piv, c]]
sign = -sign # row swap flips the sign
for r in range(c + 1, n):
M[r] -= (M[r, c] / M[c, c]) * M[c] # row addition: no change
return sign * float(np.prod(np.diag(M)))
rng = np.random.default_rng(4)
print(f"{'n':>3} {'laplace':>14} {'elimination':>14} {'numpy':>14} {'largest gap':>12}")
for n in (2, 3, 4, 5, 6):
A = rng.integers(-4, 5, size=(n, n)).astype(float)
dl, de, dn = det_laplace(A), det_elimination(A), float(np.linalg.det(A))
print(f"{n:>3} {dl:>14.4f} {de:>14.4f} {dn:>14.4f} "
f"{max(abs(dl - de), abs(de - dn)):>12.2e}")
# The trace identity that actually gets used: Equation 4.20.
x, y = rng.normal(size=400), rng.normal(size=400)
outer_trace = float(np.trace(np.outer(x, y))) # builds a 400x400 matrix
dot = float(y @ x) # builds nothing
print()
print("tr(x y^T) =", round(outer_trace, 10), " y^T x =", round(dot, 10))
print("gap:", f"{abs(outer_trace - dot):.2e}",
" and the second route never allocates the 400x400 outer product")
# Different-sized products, same trace (the fourth trace property).
A46 = rng.normal(size=(4, 6))
B64 = rng.normal(size=(6, 4))
print("tr(AB) on a 4x4:", round(float(np.trace(A46 @ B64)), 10))
print("tr(BA) on a 6x6:", round(float(np.trace(B64 @ A46)), 10)) n laplace elimination numpy largest gap
2 -12.0000 -12.0000 -12.0000 0.00e+00
3 28.0000 28.0000 28.0000 3.55e-15
4 0.0000 0.0000 0.0000 0.00e+00
5 -1008.0000 -1008.0000 -1008.0000 5.68e-13
6 -5634.0000 -5634.0000 -5634.0000 8.19e-12
tr(x y^T) = 32.7253247421 y^T x = 32.7253247421
gap: 7.11e-15 and the second route never allocates the 400x400 outer product
tr(AB) on a 4x4: 2.7234365498
tr(BA) on a 6x6: 2.7234365498Note the last two lines: two matrices of different sizes — and — with the same trace to ten decimals. The three determinant routes agree at every size while the gap grows with , which is accumulating floating-point error rather than a disagreement about the answer. And the row happens to be a random integer matrix with determinant exactly — a singular matrix found by accident, which all three routes report identically.
On real data
Section titled “On real data”Reading the plot
Section titled “Reading the plot”From the volume figure. The left panel is the collapse. One vector is held fixed and the other is swung towards it: the areas measure , , . At exact parallelism the determinant is to machine zero, and the matrix is singular — not “nearly singular”, singular, because the two columns are then linearly dependent and the rank drops.
The right panel is Example 4.2 verified, and it is worth reading the sign. The determinant is , not ; the book quotes because volume is unsigned. The negative sign says is a left-handed frame — swap any two of them and it becomes right-handed with determinant .
From the cost figure. Read the ratio column of the table above, not the curves. The crossover is at : below that the Laplace recursion is genuinely competitive, which is why hand calculation uses it and why it does not feel like a bad idea. The reference lines confirm the asymptotics — the Laplace curve sits just above and the elimination curve just above .
From the basis-change figure. This is the one to take seriously. The left histogram shows a single entry of ranging from to , against an original value of ; somewhere in the sample an entry reached . The matrix is being scrambled beyond recognition.
The right panel is the same matrices plotted by trace and determinant, zoomed until floating-point noise is visible. Every point is on top of the original: the trace moved by at most and the determinant by at most .
So “invariant under a change of basis” is not a mild statement. Under conjugation the matrix retains almost nothing recognisable, and these two numbers retain everything. That is why the chapter opens with them, and it is the same argument that will promote the eigenvalues in §4.2.
Pitfalls
Section titled “Pitfalls”Compare
Section titled “Compare”| determinant | trace | |
|---|---|---|
| definition | signed volume, Equation 4.1 | sum of the diagonal, Equation 4.18 |
| defined for | square matrices only | square matrices only |
| on a sum | no simple rule | additive: |
| on a product | multiplicative: | cyclic: |
| on a scalar multiple | ||
| basis independent? | yes | yes |
| in terms of eigenvalues | (Thm 4.16) | (Thm 4.17) |
| cost | by elimination | |
| tests invertibility? | yes, in principle | no |
| overflows? | routinely, past | no |
-
The book derives the 2x2 determinant from the formula for the inverse. Why is that the right way round?
Theorem 4.1 then generalises it: A is invertible if and only if det A is nonzero, and Theorem 4.3 adds that this is the same as having full rank.
pch.quizShowAnswer
B — Because the determinant is precisely the quantity that has to be nonzero for the inverse to exist — Equation 4.2 has it as a denominator, and Equation 4.3 names the condition — Theorem 4.1 then generalises it: A is invertible if and only if det A is nonzero, and Theorem 4.3 adds that this is the same as having full rank.
-
Which three determinant properties license Gaussian elimination as a determinant algorithm?
Measured on random 5x5 matrices: row addition changed the determinant by 3.6e-15, a row swap gave a ratio of exactly -1, and scaling a row by 4 gave exactly 4.
pch.quizShowAnswer
B — Adding a multiple of a row to another leaves it unchanged, scaling a row scales it, and swapping two rows flips its sign — so you can reach triangular form while tracking exactly what happened — Measured on random 5x5 matrices: row addition changed the determinant by 3.6e-15, a row swap gave a ratio of exactly -1, and scaling a row by 4 gave exactly 4.
-
tr(AB) = tr(BA) for A of shape 4x6 and B of shape 6x4. What is odd about that statement?
It generalises to invariance under cyclic permutation, and specialises for vectors to tr(x y-transpose) = y-transpose x — the identity that lets you avoid building an outer product at all.
pch.quizShowAnswer
B — AB is 4x4 and BA is 6x6 — two matrices of different sizes with the same trace, measured here as -1.5121771480 for both — It generalises to invariance under cyclic permutation, and specialises for vectors to tr(x y-transpose) = y-transpose x — the identity that lets you avoid building an outer product at all.
-
Under two thousand random changes of basis, one entry of the matrix ranged over -31.9 to 62.0 while the trace moved by at most 1.4e-14. What is the significance?
Equation 4.21 proves it in one line from cyclic invariance. It is also the argument that promotes the eigenvalues in section 4.2, which are invariant for the same reason.
pch.quizShowAnswer
B — The matrix retains almost nothing recognisable under conjugation, and the trace and determinant retain everything — so they are properties of the linear mapping rather than of its representation — Equation 4.21 proves it in one line from cyclic invariance. It is also the argument that promotes the eigenvalues in section 4.2, which are invariant for the same reason.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Laplace expansion by hand
Section titled “Exercise 1 – Laplace expansion by hand”Exercise 2 – Verify the seven properties
Section titled “Exercise 2 – Verify the seven properties”Exercise 3 – Laplace against elimination
Section titled “Exercise 3 – Laplace against elimination”Exercise 4 – The trace identity worth memorising
Section titled “Exercise 4 – The trace identity worth memorising”Exercise 5 – Basis independence, measured
Section titled “Exercise 5 – Basis independence, measured”Recall card
Section titled “Recall card”- The determinant is a signed volume: the absolute value is the volume of the parallelepiped spanned by the columns, and the sign records orientation.
- det A nonzero, invertible, and full rank are the same condition — Theorems 4.1 and 4.3.
- For a triangular matrix the determinant is the product of the diagonal, which is why Gaussian elimination is the practical algorithm.
- The Laplace expansion is correct and costs about n factorial. At n = 20 that is 4.2e18 multiplications against 2679 for elimination — the book says numerical methods superseded the explicit determinant, and this is the size of the reason.
- Three row operations have known effects: adding a multiple of a row changes nothing, scaling a row scales the determinant, swapping two rows flips its sign.
- The trace is the sum of the diagonal, is additive rather than multiplicative, and is the only function satisfying its four listed properties.
- The trace is invariant under cyclic permutation, so tr(AB) equals tr(BA) even when the two products have different sizes; for vectors it collapses to tr(x y-transpose) = y-transpose x.
- Both the determinant and the trace survive a change of basis — measured, a single entry roamed over a range of 94 while the trace held to fourteen decimals.
- They are two coefficients of the characteristic polynomial: the constant term is the determinant and the next-to-leading one is plus or minus the trace.
Next: Eigenvalues and Eigenvectors — the roots of that polynomial, and what they mean.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading