1.1.16Linear Algebra Essentials

Trace operator and properties

1,379 words6 min readdifficulty · medium

WHAT is the trace?

WHY does such a simple sum matter? Because it turns out to equal the sum of the eigenvalues and it behaves beautifully under products (cyclic property). In ML it appears everywhere: the trace of a covariance matrix is total variance, tr(AA)\operatorname{tr}(A^\top A) is the squared Frobenius norm, and traces let us rewrite ugly quadratic forms into differentiable expressions.


Deriving the key properties FROM SCRATCH

We use only the definition tr(A)=iAii\operatorname{tr}(A)=\sum_i A_{ii}.

1. Linearity

tr(A+B)=i(A+B)ii=i(Aii+Bii)=tr(A)+tr(B)\operatorname{tr}(A+B)=\sum_i (A+B)_{ii}=\sum_i (A_{ii}+B_{ii}) = \operatorname{tr}(A)+\operatorname{tr}(B) tr(cA)=icAii=ciAii=ctr(A)\operatorname{tr}(cA)=\sum_i cA_{ii}=c\sum_i A_{ii}=c\,\operatorname{tr}(A) Why this step? We just split a sum of sums — pure definition, no magic.

2. Transpose invariance

tr(A)=i(A)ii=iAii=tr(A)\operatorname{tr}(A^\top)=\sum_i (A^\top)_{ii}=\sum_i A_{ii}=\operatorname{tr}(A) Why? The diagonal entries AiiA_{ii} are exactly the ones unchanged by transposing ((A)ii=Aii(A^\top)_{ii}=A_{ii}).

3. Cyclic property (the star result)

Start with two matrices where AA is m×nm\times n and BB is n×mn\times m (so both ABAB and BABA are square): tr(AB)=i=1m(AB)ii=i=1mj=1nAijBji\operatorname{tr}(AB)=\sum_{i=1}^{m}(AB)_{ii}=\sum_{i=1}^{m}\sum_{j=1}^{n}A_{ij}B_{ji} Why this step? Definition of trace, then definition of matrix product (AB)ii=jAijBji(AB)_{ii}=\sum_j A_{ij}B_{ji}.

Now swap the order of summation (finite sums, always legal) and relabel: =j=1ni=1mBjiAij=j=1n(BA)jj=tr(BA)=\sum_{j=1}^{n}\sum_{i=1}^{m}B_{ji}A_{ij}=\sum_{j=1}^{n}(BA)_{jj}=\operatorname{tr}(BA) Why this step? iBjiAij=(BA)jj\sum_i B_{ji}A_{ij}=(BA)_{jj} by the product definition. So

4. Trace = sum of eigenvalues

The characteristic polynomial is p(λ)=det(AλI)=i(λiλ)p(\lambda)=\det(A-\lambda I)=\prod_i(\lambda_i-\lambda). Expanding det(AλI)\det(A-\lambda I), the coefficient of (λ)n1(-\lambda)^{n-1} is iAii=tr(A)\sum_i A_{ii}=\operatorname{tr}(A). From the factored form that same coefficient is iλi\sum_i \lambda_i. Matching:

WHY invariant under similarity? If A=P1APA'=P^{-1}AP (a change of basis), then by cyclic property tr(P1AP)=tr(APP1)=tr(A).\operatorname{tr}(P^{-1}AP)=\operatorname{tr}(APP^{-1})=\operatorname{tr}(A). So trace depends only on the map, not the chosen coordinates. That's the deep reason it equals a sum of eigenvalues.

AF2=i,jAij2=tr(AA)\|A\|_F^2=\sum_{i,j}A_{ij}^2=\operatorname{tr}(A^\top A) Why? (AA)jj=iAijAij=iAij2(A^\top A)_{jj}=\sum_i A_{ij}A_{ij}=\sum_i A_{ij}^2; summing over jj gives all entries squared.

Figure — Trace operator and properties

Worked examples


Steel-man the mistakes


Forecast-then-Verify

Recall Predict before revealing

Let A=(0100)A=\begin{pmatrix}0&1\\0&0\end{pmatrix}. Forecast tr(A)\operatorname{tr}(A) and its eigenvalues. Answer: tr(A)=0+0=0\operatorname{tr}(A)=0+0=0. Eigenvalues both 00, sum =0=0. ✓ Note: a nonzero matrix can have zero trace.


Recall Feynman: explain to a 12-year-old

Imagine a square grid of numbers. Walk from the top-left corner straight down to the bottom-right corner — that diagonal line. Add up only the numbers you step on. That total is the "trace." The cool magic trick: if you tilt your head and look at the same shape from a different angle (change coordinates), that diagonal total stays exactly the same. It's like the weight of an object: doesn't change if you turn the object around.


Flashcards

What is the trace of a square matrix?
The sum of its main-diagonal entries, tr(A)=iAii\operatorname{tr}(A)=\sum_i A_{ii}.
For which matrices is the trace defined?
Only square matrices.
State the cyclic property of trace.
tr(AB)=tr(BA)\operatorname{tr}(AB)=\operatorname{tr}(BA), and tr(ABC)=tr(BCA)=tr(CAB)\operatorname{tr}(ABC)=\operatorname{tr}(BCA)=\operatorname{tr}(CAB).
Why is tr(A)=tr(A)\operatorname{tr}(A^\top)=\operatorname{tr}(A)?
Transpose leaves diagonal entries unchanged: (A)ii=Aii(A^\top)_{ii}=A_{ii}.
Trace equals the sum of what spectral quantity?
The eigenvalues, tr(A)=iλi\operatorname{tr}(A)=\sum_i\lambda_i.
Why is trace invariant under similarity P1APP^{-1}AP?
By cyclic property tr(P1AP)=tr(APP1)=tr(A)\operatorname{tr}(P^{-1}AP)=\operatorname{tr}(APP^{-1})=\operatorname{tr}(A).
Express the Frobenius norm using trace.
AF2=tr(AA)\|A\|_F^2=\operatorname{tr}(A^\top A).
What does tr(xy)\operatorname{tr}(xy^\top) equal for column vectors?
The inner product yx=xyy^\top x = x^\top y.
Is tr(AB)=tr(A)tr(B)\operatorname{tr}(AB)=\operatorname{tr}(A)\operatorname{tr}(B)?
No — trace is not multiplicative (counterexample A=B=I2A=B=I_2).
Meaning of tr(Σ)\operatorname{tr}(\Sigma) for a covariance matrix?
Total variance = sum of variances of all features.

Connections

  • Eigenvalues and Eigenvectors — trace = sum of eigenvalues.
  • Determinant — determinant = product of eigenvalues (the multiplicative sibling).
  • Frobenius NormAF2=tr(AA)\|A\|_F^2=\operatorname{tr}(A^\top A).
  • Covariance Matrix and PCA — trace as total variance.
  • Matrix Calculus — trace trick for derivatives of quadratic forms.
  • Change of Basis and Similarity — trace as an invariant.

Concept Map

requires

split sums

diagonal unchanged

swap summation

char polynomial coeff

proves

confirms

enables

total variance

used in

used in

tr A = sum of diagonal entries

Square matrices only

Linearity

Transpose invariance

Cyclic property

Sum of eigenvalues

Similarity invariant

ML applications

Frobenius norm sq

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Trace ka matlab bahut simple hai: kisi bhi square matrix ke main diagonal (top-left se bottom-right) ke numbers ko add kar do — bas wahi trace hai, ek single number. Yaad rakho, sirf square matrices ka trace hota hai, rectangular ka nahi. Off-diagonal numbers ko ignore kar dete hain.

Ab isme khaas baat kya hai? Trace ka number coordinates badalne par nahi badalta. Matlab agar aap basis rotate kar do (similarity transform P1APP^{-1}AP), trace same rehta hai. Iska deep reason yeh hai ki trace actually eigenvalues ka sum ke barabar hai — aur eigenvalues to map ki apni property hain, aapke likhe numbers ki nahi. Isliye trace ko "invariant" kehte hain, jaise kisi cheez ka weight ghumane se nahi badalta.

Sabse useful trick hai cyclic property: tr(AB)=tr(BA)\operatorname{tr}(AB)=\operatorname{tr}(BA), aur teen matrices ho to ABCBCACABABC\to BCA\to CAB — cycle (loop) ghuma sakte ho, lekin randomly reorder nahi. Ek dum common galti: log sochte hain ABCABC aur ACBACB same honge — nahi! Sirf cyclic rotation allowed hai.

ML me trace har jagah aata hai: covariance matrix ka trace = total variance (PCA ka base yahi hai), aur tr(AA)=AF2\operatorname{tr}(A^\top A)=\|A\|_F^2 yaani Frobenius norm squared. Ek aur pyaari cheez: do vectors ka outer product xyxy^\top ka trace bas inner product yxy^\top x ban jaata hai — isse loss functions ke derivatives nikalne me bahut aasaani hoti hai. Toh trace ko halke me mat lo, chhota dikhta hai par power-packed hai.

Test yourself — Linear Algebra Essentials

Connections