Search

Search papers and lessons

Trace of a Matrix

Level 2 · 8 min read

The trace of a square matrix is the sum of its diagonal entries, a single number that is unchanged by reordering a product and equals the sum of the eigenvalues.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • Sign in to see your progress

TL;DR

The trace of a square matrix is the sum of the numbers on its main diagonal, the line running from top left to bottom right. It squeezes a whole grid of numbers into one number. Despite its simplicity, it has two properties that make it useful: swapping the order of two matrices in a product leaves the trace unchanged, and the trace equals the sum of the matrix's eigenvalues.

Where you'll see it: in the total variance of a Covariance Matrix, in checks on matrix identities, and in Principal Component Analysis, where total variance is tracked.

Intuition

Start from a grid you can read

A square matrix is a grid with the same number of rows and columns. Take a 3×33 \times 3 example:

A=(410125071).A = \begin{pmatrix} 4 & 1 & 0 \\ 1 & 2 & 5 \\ 0 & 7 & 1 \end{pmatrix}.

The diagonal entries are 44, 22 and 11, the ones whose row and column numbers match. The trace is their sum, 4+2+1=74 + 2 + 1 = 7. The off-diagonal numbers describe how the entries interact, but the trace ignores them entirely.

flowchart LR
  A["Square matrix"] --> B["Keep the diagonal entries a_11, a_22, ..., a_nn"]
  B --> C["Add them up"]
  C --> D["One number: the trace"]

Why the idea exists

Many questions about a matrix come down to a single summary. How much does a transformation stretch things overall? What is the total spread of a set of measurements? The trace gives one such summary cheaply, without having to compute the eigenvalues first. It is also the building block for more advanced tools, because it is linear, easy to handle algebraically, and unaffected by re-describing the same Linear Transformation in a different coordinate system.

The math

Definition

Definition: Trace

For a square matrix AA of size n×nn \times n with entries aija_{ij}, the trace is

tr⁡(A)=∑i=1naii=a11+a22+⋯+ann.\operatorname{tr}(A) = \sum_{i=1}^{n} a_{ii} = a_{11} + a_{22} + \cdots + a_{nn}.

Here aiia_{ii} is the entry in row ii and column ii, and ∑\sum is the summation sign over the index ii. The trace is only defined for square matrices, because a non-square matrix has no complete main diagonal.

Key results

Linearity

For matrices AA and BB of the same size and a number cc,

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

The proof is one line: add the diagonal entries of AA and BB separately, which is the same as adding the diagonals of A+BA + B.

The cyclic property

For matrices AA (of size m×nm \times n) and BB (of size n×mn \times m),

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

This holds even though ABAB and BABA are usually different matrices, and even though they may have different sizes. The trace of a product does not care about the order. As a consequence, tr⁡(ABC)=tr⁡(BCA)=tr⁡(CAB)\operatorname{tr}(ABC) = \operatorname{tr}(BCA) = \operatorname{tr}(CAB): you may rotate the factors cyclically, but you cannot reorder them arbitrarily.

Trace and eigenvalues

If a square matrix AA has eigenvalues λ1,…,λn\lambda_1, \dots, \lambda_n counted with multiplicity (see Eigenvalues and Eigenvectors), then

tr⁡(A)=λ1+λ2+⋯+λn.\operatorname{tr}(A) = \lambda_1 + \lambda_2 + \cdots + \lambda_n.

The determinant is the product of the same eigenvalues. So the trace is their sum and the Determinant is their product, and both can be read off without solving for the eigenvalues.

The trace of an outer product

For a column vector v\mathbf{v}, the matrix vv⊤\mathbf{v}\mathbf{v}^\top (the column times its transpose, a row) has diagonal entries vi2v_i^2. Therefore

tr⁡(vv⊤)=∑ivi2=∥v∥2,\operatorname{tr}(\mathbf{v}\mathbf{v}^\top) = \sum_i v_i^2 = \|\mathbf{v}\|^2,

the squared length of v\mathbf{v}. This is the same as the dot product of v\mathbf{v} with itself.

Worked examples

Example
1. A 2 by 2 matrix (tiny)

Let A=(1234)A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}.

  1. Identify the diagonal entries: a11=1a_{11} = 1 (row 1, column 1) and a22=4a_{22} = 4 (row 2, column 2).
  2. Add them: tr⁡(A)=1+4=5\operatorname{tr}(A) = 1 + 4 = 5.
  3. Note that the off-diagonal entries 22 and 33 do not appear.

Interpretation: the trace is a single number summarising the matrix. Two matrices with the same diagonal have the same trace, even if their off-diagonal entries are very different.

Example
2. Checking the cyclic property (the standard case)

Take A=(1234)A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} and B=(0110)B = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}.

  1. Compute ABAB, row by column: the entry in row 1, column 1 is 1⋅0+2⋅1=21 \cdot 0 + 2 \cdot 1 = 2. Filling in the rest gives AB=(2143)AB = \begin{pmatrix} 2 & 1 \\ 4 & 3 \end{pmatrix}.
  2. Trace of ABAB: 2+3=52 + 3 = 5.
  3. Compute BABA, row by column: the entry in row 1, column 1 is 0⋅1+1⋅3=30 \cdot 1 + 1 \cdot 3 = 3. Filling in the rest gives BA=(3412)BA = \begin{pmatrix} 3 & 4 \\ 1 & 2 \end{pmatrix}.
  4. Trace of BABA: 3+2=53 + 2 = 5.
  5. Compare: AB≠BAAB \neq BA (their entries differ), but tr⁡(AB)=tr⁡(BA)=5\operatorname{tr}(AB) = \operatorname{tr}(BA) = 5.

Interpretation: the matrices differ, yet the trace of the product agrees. This is the cyclic property in action, and it is the reason trace is handy in algebraic proofs.

Example
3. Total variance of a covariance matrix (realistic)

A covariance matrix records the variance of each measured feature on the diagonal and the covariances between features off the diagonal. Suppose three features have covariance matrix

Σ=(410120.500.51).\Sigma = \begin{pmatrix} 4 & 1 & 0 \\ 1 & 2 & 0.5 \\ 0 & 0.5 & 1 \end{pmatrix}.
  1. The variances are on the diagonal: 44, 22 and 11.
  2. The trace is tr⁡(Σ)=4+2+1=7\operatorname{tr}(\Sigma) = 4 + 2 + 1 = 7.
  3. The off-diagonal covariances 11, 00 and 0.50.5 do not change the total spread. They do change how the features move together, which the trace cannot tell you.

Interpretation: the total variance across the three features is 7. Because the trace equals the sum of the eigenvalues of Σ\Sigma, the same 7 is the total variance along the principal directions of the data, which is why principal component analysis keeps track of it.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
You need the size of each eigenvalueThe trace gives only their sumCompute the [[eigenvalues-and-eigenvectors
You need whether a matrix is invertibleTrace can be non-zero for a singular matrixUse the Determinant
You want the trace of a product of three or more matricesAssuming it splits into the product of traces is a common errorExpand with the cyclic property; tr⁡(AB)≠tr⁡(A)tr⁡(B)\operatorname{tr}(AB) \neq \operatorname{tr}(A)\operatorname{tr}(B) in general
The matrix is not squareThe trace is undefinedUse a different summary, such as the square root of the sum of all squared entries (the Frobenius norm)

The trace is a coarse summary. Two very different matrices can share a trace, so it is best used alongside other measures when you need to distinguish them.

History

The matrix algebra in which the trace lives was set out by Arthur Cayley in his 1858 memoir on the theory of matrices. The trace itself is a simple sum, so it appeared early as a convenient quantity. The English word "trace" follows the German term "Spur", meaning track or trail, used for the diagonal sum. Its importance grew with the study of linear transformations, where the trace does not change when the coordinate system changes, and it later became standard in statistics, physics and machine learning.

Recap

The trace of a square matrix is the sum of its diagonal entries. It is linear, invariant under cyclic reordering of products, and equal to the sum of the eigenvalues. Applied to an outer product vv⊤\mathbf{v}\mathbf{v}^\top, it returns the squared length of v\mathbf{v}, which connects it to the dot product. For data, the trace of a covariance matrix is the total variance, and the eigenvalue view explains why that total is shared across directions.

Check yourself

QuestionFind the trace of the matrix with rows (2, 9, 4), (1, 5, 0) and (7, 3, 8).Show answer

The diagonal entries are 22 (row 1, column 1), 55 (row 2, column 2) and 88 (row 3, column 3). Their sum is 2+5+8=152 + 5 + 8 = 15. The entries 99, 44, 11, 00, 77 and 33 do not contribute.

QuestionLet A be a 2 by 3 matrix and B a 3 by 2 matrix. Explain why tr(AB) = tr(BA) even though AB is 2 by 2 and BA is 3 by 3.Show answer

The cyclic property of the trace holds for any pair of matrices whose product makes sense in both orders. Here ABAB is 2×22 \times 2 and BABA is 3×33 \times 3, so their traces are sums over different numbers of diagonal entries, yet the identity still holds: tr⁡(AB)=tr⁡(BA)\operatorname{tr}(AB) = \operatorname{tr}(BA). The sums agree because both equal the sum over all index pairs of aijbjia_{ij} b_{ji}.

QuestionA [[symmetric-matrix|symmetric]] 2 by 2 matrix has eigenvalues 5 and 1. What is its trace, and what is its determinant?Show answer

The trace is the sum of the eigenvalues: 5+1=65 + 1 = 6. The determinant is their product: 5×1=55 \times 1 = 5. Both follow without computing the matrix entries.

QuestionA researcher reports that a 4 by 4 covariance matrix has trace 12. Is this enough to say the features are uncorrelated?Show answer

No. The trace is the sum of the variances on the diagonal and ignores all the covariances. Two matrices with the same trace can have very different correlations. Checking the off-diagonal entries, or the eigenvalues, is needed to judge correlation.

Sources