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 example:
The diagonal entries are , and , the ones whose row and column numbers match. The trace is their sum, . 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
For a square matrix of size with entries , the trace is
Here is the entry in row and column , and is the summation sign over the index . The trace is only defined for square matrices, because a non-square matrix has no complete main diagonal.
Key results
Linearity
For matrices and of the same size and a number ,
The proof is one line: add the diagonal entries of and separately, which is the same as adding the diagonals of .
The cyclic property
For matrices (of size ) and (of size ),
This holds even though and 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, : you may rotate the factors cyclically, but you cannot reorder them arbitrarily.
Trace and eigenvalues
If a square matrix has eigenvalues counted with multiplicity (see Eigenvalues and Eigenvectors), then
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 , the matrix (the column times its transpose, a row) has diagonal entries . Therefore
the squared length of . This is the same as the dot product of with itself.
Worked examples
Let .
- Identify the diagonal entries: (row 1, column 1) and (row 2, column 2).
- Add them: .
- Note that the off-diagonal entries and 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.
Take and .
- Compute , row by column: the entry in row 1, column 1 is . Filling in the rest gives .
- Trace of : .
- Compute , row by column: the entry in row 1, column 1 is . Filling in the rest gives .
- Trace of : .
- Compare: (their entries differ), but .
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.
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
- The variances are on the diagonal: , and .
- The trace is .
- The off-diagonal covariances , and 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 , 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
| Situation | What goes wrong | Better choice |
|---|---|---|
| You need the size of each eigenvalue | The trace gives only their sum | Compute the [[eigenvalues-and-eigenvectors |
| You need whether a matrix is invertible | Trace can be non-zero for a singular matrix | Use the Determinant |
| You want the trace of a product of three or more matrices | Assuming it splits into the product of traces is a common error | Expand with the cyclic property; in general |
| The matrix is not square | The trace is undefined | Use 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 , it returns the squared length of , 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 (row 1, column 1), (row 2, column 2) and (row 3, column 3). Their sum is . The entries , , , , and 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 is and is , so their traces are sums over different numbers of diagonal entries, yet the identity still holds: . The sums agree because both equal the sum over all index pairs of .
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: . The determinant is their product: . 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.