skip to content

Why does the trace satisfy tr(AB) = tr(BA) even when AB and BA differ?

level: middleimportance: should knowfreq 38%

answer

  1. the trace only sees the diagonal
  2. write the diagonal entry as a sum
  3. each product term appears in both
  4. you are swapping the order of summation
  5. rotations of the factors, not swaps

basics

~20 s

The trace is the sum of a square matrix's diagonal entries, and both tr(AB) and tr(BA) expand to the same double sum over every pair of entries. The order of summation changes, so the totals match.

solid answer

~50 s

The trace of a square matrix is the sum of its diagonal entries. Write out the diagonal of a product: `tr(AB) = sum over i of sum over j of A_ij * B_ji`, and `tr(BA) = sum over j of sum over i of B_ji * A_ij`. These are the same terms summed in a different order, so the totals are equal. Strikingly, the identity holds even when `A` is `m x n` and `B` is `n x m`, so `AB` is `m x m` while `BA` is `n x n` — two products of different sizes with the same trace. The property extends cyclically: `tr(ABC) = tr(BCA) = tr(CAB)`. It is a **cyclic** rotation only, not free reordering — `tr(ACB)` is generally different. The trace is also linear: `tr(A + B) = tr(A) + tr(B)` and `tr(cA) = c * tr(A)`, unlike the determinant.

go deeper

for a junior

Be able to state that the trace is the sum of the diagonal entries and compute it instantly for a small matrix, and remember that the n by n identity has trace n.

for a middle

Reproduce the index argument: write the diagonal entry of the product as a sum, swap the order of summation, and note that AB and BA can even have different sizes.

for a senior

Demonstrate the boundary of the identity, that only cyclic rotations are allowed, and use the trace confidently as a scalar summary such as total variance while naming what it discards.

for a principal

Be ready to argue when a one-number summary of a matrix is the right thing to report to a team and when collapsing the off-diagonal structure hides the decision that actually matters.

## What the trace is The trace of a square matrix `A`, written `tr(A)`, is the sum of the entries on its main diagonal: `tr(A) = A_11 + A_22 + ... + A_nn`. Nothing off the diagonal is used. For `[[4, 9], [2, -1]]` the trace is `4 + (-1) = 3`. Like the determinant, it collapses a whole matrix into one number, but it does so by adding rather than by measuring volume, and it is defined only for square matrices. ## The elementary properties The trace is **linear**, which the determinant is not: - `tr(A + B) = tr(A) + tr(B)` - `tr(cA) = c * tr(A)` for a scalar `c` - `tr(A^T) = tr(A)`, since transposing leaves the diagonal in place - `tr(I_n) = n`, since the identity has n ones on its diagonal That last one is a favourite trap: the identity's determinant is 1, but its trace is n. ## Why tr(AB) = tr(BA) Let `A` be `m x n` and `B` be `n x m`, so both products are defined: `AB` is `m x m` and `BA` is `n x n`. The `i`-th diagonal entry of `AB` is the dot product of row `i` of `A` with column `i` of `B`: ``` (AB)_ii = sum over j of A_ij * B_ji ``` Summing over `i` from 1 to m, ``` tr(AB) = sum over i, sum over j, of A_ij * B_ji ``` Doing the same for `BA`, whose `j`-th diagonal entry is `sum over i of B_ji * A_ij`, and summing over `j` from 1 to n, ``` tr(BA) = sum over j, sum over i, of B_ji * A_ij ``` The two expressions contain exactly the same `m * n` products `A_ij * B_ji`; only the order of summation differs, and finite sums of scalars can be reordered freely. Hence the traces are equal. Notice how much the identity is *not* claiming: `AB` and `BA` are generally different matrices, and here they need not even be the same size. If `A` is `2 x 5` and `B` is `5 x 2`, then `AB` is `2 x 2` and `BA` is `5 x 5`, and yet both have the same trace. ## The cyclic property, stated carefully Applying the two-factor result with `A` playing the role of the first matrix and `BC` the second gives ``` tr(ABC) = tr((A)(BC)) = tr((BC)(A)) = tr(BCA) ``` and repeating gives `tr(CAB)`. So a product's trace is invariant under **cyclic rotation** of the factors: slide the leftmost factor to the right end as many times as you like. What it does *not* allow is arbitrary permutation. `tr(ACB)` is not a cyclic rotation of `tr(ABC)` — it swaps two factors — and it generally differs. With three factors the cyclic class `{ABC, BCA, CAB}` and the other class `{ACB, CBA, BAC}` are two separate groups with, in general, two separate traces. Claiming you may reorder freely is one of the most common errors on this identity. ## What it is not The trace is not multiplicative: `tr(AB)` has no general relationship to `tr(A) tr(B)`. Take `A = B = I_2`. Then `tr(A) tr(B) = 2 * 2 = 4`, but `AB = I_2` has trace 2. Multiplicativity belongs to the determinant; linearity belongs to the trace. Mixing the two up is another common slip. ## Where the trace shows up as a summary Because the trace adds the diagonal, it is a natural "total" whenever the diagonal entries are individually meaningful quantities. The clearest example is a covariance matrix, whose diagonal entries are the variances of the individual variables: its trace is therefore the **total variance**, a single scalar summarising overall spread. It is deliberately coarse — it discards every off-diagonal entry, so two very differently structured matrices can share a trace — but as a one-number "how much spread is there in total" it is convenient and cheap. The cyclic property is what makes traces pleasant to manipulate on paper. When an expression is a scalar written as a chain of matrix products, wrapping it in a trace and rotating the factors often turns an awkward arrangement into a recognisable one without changing the value. ## Answering in the room Give the definition (sum of the diagonal), then do the index proof in two lines — write the diagonal entry of `AB` as a sum over `j`, sum over `i`, and observe that swapping the order of summation gives `tr(BA)`. Add the size observation (`m x m` versus `n x n` with equal traces) because it shows you understand the statement rather than the slogan, and close by naming the boundary: cyclic rotations only, `tr(ACB)` is not covered.

  • What does the trace of a covariance matrix summarise?
    Total variance. The diagonal entries of a covariance matrix are the variances of the individual variables, so summing them gives one scalar for the overall spread. It is a deliberately coarse summary: every off-diagonal entry is discarded, so two matrices with very different structure can share a trace.
  • Is tr(AB) equal to tr(A) times tr(B)?
    No. The trace is linear, not multiplicative. A one-line counterexample: with `A = B = I_2`, `tr(A) tr(B) = 2 * 2 = 4` while `AB = I_2` has trace 2. Multiplicativity is the determinant's property; the trace instead satisfies `tr(A + B) = tr(A) + tr(B)`.
  • Does the cyclic property let you reorder three matrices any way you like inside a trace?
    No — only cyclic rotations are permitted. `tr(ABC) = tr(BCA) = tr(CAB)`, because each step moves the leftmost factor to the right end. Swapping two factors is a different operation, and `tr(ACB)` generally differs from `tr(ABC)`. Treating the identity as free permutation is the classic error.

saying these in an interview costs you the question

  • Says tr(AB) equals tr(A) times tr(B)
  • Claims tr(ABC) equals tr(ACB) in general
  • Sums every entry of the matrix, not just the diagonal
  • Says tr(AB) needs AB and BA to be the same size
  • Reports the identity matrix's trace as 1 rather than n

context