Trace Cyclic Property

lean4-prooflinear-algebravisualization
tr(AB)=tr(BA)\operatorname{tr}(AB) = \operatorname{tr}(BA)

Statement

For any m×nm \times n matrix AA and n×mn \times m matrix BB over a commutative ring RR:

tr(AB)=tr(BA).\operatorname{tr}(AB) = \operatorname{tr}(BA).

More generally, the trace is invariant under cyclic permutations of a product:

tr(A1A2Ak)=tr(A2AkA1)==tr(AkA1Ak1).\operatorname{tr}(A_1 A_2 \cdots A_k) = \operatorname{tr}(A_2 \cdots A_k A_1) = \cdots = \operatorname{tr}(A_k A_1 \cdots A_{k-1}).

Note that tr(ABC)=tr(BCA)=tr(CAB)\operatorname{tr}(ABC) = \operatorname{tr}(BCA) = \operatorname{tr}(CAB) in general, but tr(ABC)tr(BAC)\operatorname{tr}(ABC) \ne \operatorname{tr}(BAC) — only cyclic permutations preserve the trace, not arbitrary ones.

Visualization

Take A=(1234)A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} and B=(5678)B = \begin{pmatrix} 5 & 6 \\ 7 & 8 \end{pmatrix}.

Compute ABAB:

AB=(15+2716+2835+4736+48)=(19224350).AB = \begin{pmatrix} 1 \cdot 5 + 2 \cdot 7 & 1 \cdot 6 + 2 \cdot 8 \\ 3 \cdot 5 + 4 \cdot 7 & 3 \cdot 6 + 4 \cdot 8 \end{pmatrix} = \begin{pmatrix} 19 & 22 \\ 43 & 50 \end{pmatrix}.

Compute BABA:

BA=(51+6352+6471+8372+84)=(23343146).BA = \begin{pmatrix} 5 \cdot 1 + 6 \cdot 3 & 5 \cdot 2 + 6 \cdot 4 \\ 7 \cdot 1 + 8 \cdot 3 & 7 \cdot 2 + 8 \cdot 4 \end{pmatrix} = \begin{pmatrix} 23 & 34 \\ 31 & 46 \end{pmatrix}.
ProductMatrixTrace
ABAB(19224350)\begin{pmatrix} 19 & 22 \\ 43 & 50 \end{pmatrix}19+50=6919 + 50 = 69
BABA(23343146)\begin{pmatrix} 23 & 34 \\ 31 & 46 \end{pmatrix}23+46=6923 + 46 = 69

Both traces equal 6969, confirming tr(AB)=tr(BA)\operatorname{tr}(AB) = \operatorname{tr}(BA).

Proof Sketch

  1. Expand by definition. tr(AB)=i(AB)ii=ijAijBji\operatorname{tr}(AB) = \sum_i (AB)_{ii} = \sum_i \sum_j A_{ij} B_{ji}.
  2. Swap summation. ijAijBji=jiBjiAij=j(BA)jj=tr(BA)\sum_i \sum_j A_{ij} B_{ji} = \sum_j \sum_i B_{ji} A_{ij} = \sum_j (BA)_{jj} = \operatorname{tr}(BA).
  3. Commutativity of RR. The interchange AijBji=BjiAijA_{ij} B_{ji} = B_{ji} A_{ij} uses that RR is commutative.
  4. Cyclic extension. tr(ABC)=tr((AB)C)=tr(C(AB))=tr(CAB)\operatorname{tr}(ABC) = \operatorname{tr}((AB)C) = \operatorname{tr}(C(AB)) = \operatorname{tr}(CAB); apply inductively.

Connections

  • Cayley–Hamilton TheoremCayley–Hamilton TheorempA(A)=0  where  pA(λ)=det(λIA)p_A(A) = 0 \;\text{where}\; p_A(\lambda) = \det(\lambda I - A)Every square matrix satisfies its own characteristic polynomialRead more → — the proof of Cayley–Hamilton for matrices over a commutative ring uses the trace (and more generally the symmetric functions of eigenvalues) to identify coefficients of the characteristic polynomial
  • Determinant MultiplicativityDeterminant Multiplicativitydet(AB)=det(A)det(B)\det(AB) = \det(A)\,\det(B)The determinant of a product equals the product of determinantsRead more → — both trace and determinant are similarity invariants; tr(PAP1)=tr(A)\operatorname{tr}(PAP^{-1}) = \operatorname{tr}(A) follows from the cyclic property applied to PP, AA, P1P^{-1}
  • Sylvester Determinant TheoremSylvester Determinant Theoremdet(Im+AB)=det(In+BA)\det(I_m + AB) = \det(I_n + BA)det(Im + AB) = det(In + BA) for any m×n matrix A and n×m matrix B.Read more → — a determinant analogue: just as det(I+AB)=det(I+BA)\det(I+AB) = \det(I+BA), the cyclic property gives tr(AB)=tr(BA)\operatorname{tr}(AB) = \operatorname{tr}(BA) as a first-order version of the same symmetry

Lean4 Proof

import Mathlib.LinearAlgebra.Matrix.Trace

/-- Trace cyclic property: tr(A * B) = tr(B * A).
    Mathlib's direct alias is `Matrix.trace_mul_comm`. -/
theorem trace_cyclic
    {R : Type*} [AddCommMonoid R] [CommMagma R]
    {m n : Type*} [Fintype m] [Fintype n]
    (A : Matrix m n R) (B : Matrix n m R) :
    Matrix.trace (A * B) = Matrix.trace (B * A) :=
  Matrix.trace_mul_comm A B