Foundations to Frontier
Tests
A. Mathematical Foundations › A2 Linear Algebra › Matrices

The Trace and Its Properties

Add up the diagonal. This tiny operation equals the sum of the eigenvalues, turns matrix losses into neat formulas, measures rotation angles, and can be estimated without ever seeing the matrix.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Adding up the diagonal

Number a 3 × 3 board 1 to 9, row by row. Walk diagonally from the top-left corner to the bottom-right and add what you step on: 1 + 5 + 9 = 15. That sum is called the trace.

A sum that behaves nicely

If you add two boards square by square, their traces add too. Double every number, and the trace doubles. And here’s the surprise: even when two matrices give different answers in different orders (AB≠BAAB \ne BA), their traces agree, tr⁡(AB)=tr⁡(BA)\operatorname{tr}(AB) = \operatorname{tr}(BA). Small facts like this turn out to be very useful.

What the trace measures
A =
2 1
1 1
· tr A = 3 · det A = 1
eigenvalues: 2.62 and 0.38 · their sum = 3
with B =
1 2
0 -1
: AB =
2 3
1 1
BA =
4 3
-1 -1
tr(AB) = 3, tr(BA) = 3 (equal, although AB ≠ BA)
area of the square after I + 0.1A: 1.310 ≈ 1 + 0.1·tr A = 1.300
Only the diagonal enters the trace, yet it equals the sum of the eigenvalues, it is unchanged by swapping the order of a product, and it measures how fast a small step x ← x + 0.1·Ax expands (positive trace) or shrinks (negative trace) areas. That last fact is why the trace appears in continuous normalizing flows and in the divergence of a vector field.
📝 Quick check (Level 0)
0/3 answered
  1. 1. A 3 × 3 board numbered 1–9 row by row. Sum of the top-left to bottom-right diagonal?
  2. 2. Board A has diagonal sum 4 and board B has 6. The diagonal sum of A + B?
  3. 3. Does the trace use the numbers off the diagonal?

Level 1: Beginner

★★☆☆☆ Basic⏱ 10 minSecondary-school mathematics: arithmetic and elementary algebra.

Definition

For a square matrix, tr⁡A=∑i=1naii\operatorname{tr}A = \sum_{i=1}^n a_{ii}. For example, tr⁡(1234)=5\operatorname{tr}\begin{pmatrix}1&2\\3&4\end{pmatrix} = 5, and tr⁡In=n\operatorname{tr}I_n = n.

Basic rules

tr⁡(A+B)=tr⁡A+tr⁡B,tr⁡(cA)=ctr⁡A,tr⁡(A⊤)=tr⁡A\operatorname{tr}(A + B) = \operatorname{tr}A + \operatorname{tr}B, \qquad \operatorname{tr}(cA) = c\operatorname{tr}A, \qquad \operatorname{tr}(A^\top) = \operatorname{tr}A

So the trace is a linear function of a matrix.

📝 Quick check (Level 1)
0/3 answered
  1. 1. tr I₇ = ?
  2. 2. tr A = 5. tr(3A) = ?
  3. 3. Trace is defined for…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

The cyclic property

tr⁡(AB)=tr⁡(BA),tr⁡(ABC)=tr⁡(BCA)=tr⁡(CAB)\operatorname{tr}(AB) = \operatorname{tr}(BA), \qquad \operatorname{tr}(ABC) = \operatorname{tr}(BCA) = \operatorname{tr}(CAB)

This holds even when AA and BB aren’t square, as long as both products are square. You can rotate a product cyclically, but you can’t reorder it arbitrarily: tr⁡(ABC)≠tr⁡(ACB)\operatorname{tr}(ABC) \ne \operatorname{tr}(ACB) in general. With A=(1234)A = \begin{pmatrix}1&2\\3&4\end{pmatrix} and B=(5678)B = \begin{pmatrix}5&6\\7&8\end{pmatrix}, AB≠BAAB\ne BA, but both traces are 69.

Useful identities

Identity Why it matters
tr⁡(xx⊤)=x⊤x=∥x∥2\operatorname{tr}(\mathbf x\mathbf x^\top) = \mathbf x^\top\mathbf x = \lVert\mathbf x\rVert^2 a scalar is its own trace, so the “trace trick” applies
∥A∥F2=∑ijaij2=tr⁡(A⊤A)\lVert A\rVert_F^2 = \sum_{ij}a_{ij}^2 = \operatorname{tr}(A^\top A) Frobenius norm: multi-output squared losses, weight decay
tr⁡A=∑iλi\operatorname{tr}A = \sum_i\lambda_i the sum of the eigenvalues (A2.3)
tr⁡(A⊤B)=∑ijaijbij\operatorname{tr}(A^\top B) = \sum_{ij}a_{ij}b_{ij} the inner product of matrices
🐍 Exercise 1: trace, Frobenius norm and the cyclic property
Implement trace and the squared Frobenius norm via tr(AᵀA). The tests check cyclic invariance, and show that arbitrary reordering fails.

The trace trick in ML and statistics

A scalar like x⊤Ax\mathbf x^\top A\mathbf x is a 1×11\times1 matrix, so it equals its own trace. Then x⊤Ax=tr⁡(Axx⊤)\mathbf x^\top A\mathbf x = \operatorname{tr}(A\mathbf x\mathbf x^\top). Taking an expectation gives E[x⊤Ax]=tr⁡(A E[xx⊤])\mathbb{E}[\mathbf x^\top A\mathbf x] = \operatorname{tr}(A\,\mathbb{E}[\mathbf x\mathbf x^\top]). This one-line move appears in derivations of the multivariate Gaussian MLE, PCA, the bias–variance decomposition, and Kalman filters.

Kalman filters minimize a trace

The Kalman gain is chosen to minimize tr⁡P\operatorname{tr}P, the trace of the state covariance, which is the total expected squared estimation error (T1). Satellites, drones and rovers all estimate their state this way.

📝 Quick check (Level 2)
0/3 answered
  1. 1. tr(ABC) always equals…
  2. 2. tr(x xᵀ) for x = (1, 2, 3)?
  3. 3. ‖A‖²_F for A = [[1, 2], [3, 4]]?

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Proof of the cyclic property

Theorem

For A∈Rm×nA\in\mathbb{R}^{m\times n} and B∈Rn×mB\in\mathbb{R}^{n\times m}: tr⁡(AB)=tr⁡(BA)\operatorname{tr}(AB) = \operatorname{tr}(BA).

Proof. tr⁡(AB)=∑i=1m∑k=1naikbki=∑k=1n∑i=1mbkiaik=tr⁡(BA)\operatorname{tr}(AB) = \sum_{i=1}^m\sum_{k=1}^n a_{ik}b_{ki} = \sum_{k=1}^n\sum_{i=1}^m b_{ki}a_{ik} = \operatorname{tr}(BA). Swapping finite sums is allowed (A1-3). ∎ Applying it to AA and (BC)(BC) gives tr⁡(ABC)=tr⁡(BCA)\operatorname{tr}(ABC) = \operatorname{tr}(BCA).

Trace is basis-independent

Theorem

Similar matrices have the same trace: tr⁡(P−1AP)=tr⁡A\operatorname{tr}(P^{-1}AP) = \operatorname{tr}A.

Proof. tr⁡(P−1(AP))=tr⁡((AP)P−1)=tr⁡A\operatorname{tr}(P^{-1}(AP)) = \operatorname{tr}((AP)P^{-1}) = \operatorname{tr}A. ∎ So the trace belongs to the linear map, not to the coordinates. That’s why it equals the sum of the eigenvalues (see A2.3), and why physical quantities built from it don’t depend on the frame.

Consequences

  • No matrices satisfy AB−BA=IAB - BA = I: the left side has trace 0 and the right side has trace nn. (In quantum mechanics, this is why position and momentum need infinite-dimensional spaces.)
  • Projections: if P2=PP^2 = P, then tr⁡P=rank⁡P\operatorname{tr}P = \operatorname{rank}P. Projecting onto a 3-dimensional subspace has trace 3.
  • Rotations: a 3D rotation by angle θ\theta has tr⁡R=1+2cos⁡θ\operatorname{tr}R = 1 + 2\cos\theta, whatever its axis. So θ=arccos⁡tr⁡R−12\theta = \arccos\frac{\operatorname{tr}R - 1}{2}. This is the standard way to measure the attitude error between two orientations in robotics and spacecraft (R2).
Exercise 3.1: A 3D rotation has trace 0. What is its angle?

1+2cos⁡θ=01 + 2\cos\theta = 0, so cos⁡θ=−12\cos\theta = -\tfrac12 and θ=120∘\theta = 120^\circ.

📝 Quick check (Level 3)
0/3 answered
  1. 1. tr(AB − BA) = ?
  2. 2. A projection onto a 4-dimensional subspace of ℝ¹⁰ has trace…
  3. 3. A 2D rotation by 60° has trace…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Estimating a trace without the matrix

In large-scale ML you often can’t form a matrix, such as the Hessian of a network with a billion parameters, but you can multiply it by a vector. Hutchinson’s estimator (1989) uses random vectors z\mathbf z with independent ±1 entries:

E[z⊤Az]=tr⁡(A E[zz⊤])=tr⁡(A).\mathbb{E}[\mathbf z^\top A\mathbf z] = \operatorname{tr}(A\,\mathbb{E}[\mathbf z\mathbf z^\top]) = \operatorname{tr}(A).

That’s the trace trick again. Averaging over mm probes gives an error that shrinks like 1/m1/\sqrt m. Improvements such as Hutch++ (Meyer et al., 2021) do better. Uses:

  • Hessian traces to measure sharpness and curvature of the loss landscape (B4), and in second-order optimizers.
  • Log-determinants, log⁡det⁡A=tr⁡log⁡A\log\det A = \operatorname{tr}\log A, for Gaussian processes (I2) and normalizing flows (H5), via stochastic Lanczos quadrature.
  • FFJORD / continuous normalizing flows, which need tr⁡(∂f/∂z)\operatorname{tr}(\partial f/\partial\mathbf z) at every step. Grathwohl et al. (2019) use exactly the Hutchinson estimator.
🐍 Exercise 2 (research): Hutchinson's trace estimator
Estimate tr(A) using only products A·z with random ±1 vectors. Check the error shrinks as the number of probes grows.

Traces in matrix calculus

Many gradients are easiest to derive by writing the loss as a trace and using ∂∂Xtr⁡(AX)=A⊤\frac{\partial}{\partial X}\operatorname{tr}(AX) = A^\top together with the cyclic property. For example, the gradient of ∥Y−XW∥F2\lVert Y - XW\rVert_F^2 with respect to WW is −2X⊤(Y−XW)-2X^\top(Y - XW), which is the backbone of linear regression and of backprop through a linear layer (A3.3, Practice Code 5).

References

  1. M. Hutchinson (1989). “A stochastic estimator of the trace of the influence matrix for Laplacian smoothing splines.” Communications in Statistics: Simulation and Computation, 18(3).
  2. K. B. Petersen and M. S. Pedersen (2012). The Matrix Cookbook. Technical University of Denmark. (Trace identities and derivatives.)
  3. W. Grathwohl, R. Chen, J. Bettencourt, I. Sutskever, D. Duvenaud (2019). “FFJORD: Free-form continuous dynamics for scalable reversible generative models.” ICLR.
  4. R. Meyer, C. Musco, C. Musco, D. Woodruff (2021). “Hutch++: Optimal stochastic trace estimation.” SIAM Symposium on Simplicity in Algorithms (SOSA).
  5. R. Kalman (1960). “A new approach to linear filtering and prediction problems.” Journal of Basic Engineering, 82(1).