AI Glossary / Mathematics and optimization / Intermediate

Trace

Definition

The trace of a square matrix is the sum of its main-diagonal entries, written tr(A). Those entries run from the upper-left corner to the lower-right corner. Trace turns a matrix into one number, and it appears in statistics, linear algebra, and optimization. In this entry, trace means this mathematical operation, rather than an execution log or a record of an AI agent's actions.

What it means

For a 3-by-3 matrix, add the entries in positions (1,1), (2,2), and (3,3). The off-diagonal entries do not directly enter that sum, even though they may be crucial to the matrix's behavior.
Trace is useful because it summarizes certain quantities cleanly. For example, the diagonal of a covariance matrix contains the individual variables' variances, so its trace sums those variances. Whether that sum has a useful interpretation depends on the variables' scales and units.
Trace is also the sum of the matrix's eigenvalues, counted with algebraic multiplicity. This does not mean a trace value reveals all the eigenvalues or proves the matrix is well behaved. Many very different matrices have the same trace.

How it works

Compute tr(A) by summing A[i,i] over the square matrix's row indices. The operation is linear: tr(A+B) = tr(A)+tr(B), and tr(cA) = c tr(A), when dimensions are compatible.
A useful product identity is tr(AB) = tr(BA). This can hold even when AB and BA have different square sizes, provided both products are defined. With longer products, cyclic rotation preserves trace, but arbitrary reordering generally does not.
Trace is unchanged by a similarity transformation S-inverse A S when S is invertible. This concerns representing the same linear transformation in a different basis; it is not permission to rescale a dataset's variables and expect every covariance summary to stay unchanged.

Three practical examples

  • A statistical report can use the trace of a covariance matrix to summarize total coordinate variance after documenting the feature scales.
  • A model-fitting derivation can express a sum of squared residual entries as tr(R-transpose R), making matrix formulas more compact.
  • A numerical analyst can use trace identities to simplify a calculation or check an implementation, while retaining other checks that capture information trace discards.

Think of it this way

Think of a building plan with one inspection value written in each room's matching row-and-column position on a checklist. Trace adds only those matching-position values. It is a selective total, not a review of every relationship in the plan. The analogy helps locate the entries but does not explain deeper properties such as eigenvalue or similarity identities.

Worked example

Let A have rows [2, 1] and [0, 3]. Its trace is 2+3 = 5. Let B have rows [1, 0] and [4, 2]. Multiplication gives AB with rows [6, 2] and [12, 6], so tr(AB) = 12.
Reversing the multiplication gives BA with rows [2, 1] and [8, 10], whose trace is also 12. The products are different matrices, despite their equal traces. This illustrates why the trace identity does not establish AB = BA.
Now take the rectangular matrix X with rows [1, 2] and [3, 4] and [5, 6]. The diagonal of X-transpose X is [35, 56], giving trace 91. Directly summing the squares of X's six entries gives 1+4+9+16+25+36 = 91 as well.
The diagonal of X X-transpose is [5, 25, 61], again summing to 91. Here the two products are 2-by-2 and 3-by-3, respectively. Their equal trace does not make their dimensions, entries, or full eigenvalue lists identical.

Illustrative case study

In a fictional forecasting project, an analyst compares the covariance trace before and after adding a revenue feature measured in a much larger numerical unit. The summary increases sharply, and an early report describes this as evidence that the dataset has become inherently more diverse.
A colleague points out that coordinate scale affects covariance. The team reports the original units, adds an explicitly standardized comparison, and examines individual variances and relationships before drawing conclusions.
The lesson is that an exact matrix calculation can support a misleading comparison when preprocessing changes. The fictional result does not imply that standardization is always the right business choice.

Common misconceptions and mistakes

A zero trace does not imply a zero matrix. The diagonal matrix with entries 1 and −1 has trace zero but nonzero action. A positive trace also does not imply positive definiteness: diagonal entries 3 and −1 sum to 2 while one direction has a negative quadratic value.
Do not confuse trace with determinant. For the diagonal matrix [2, 3], trace is 5 and determinant is 6.
Do not extend cyclic invariance to unrestricted factor swaps. Finally, summing a rectangular array's diagonal may be a supported software operation, but it is not the usual basis-invariant trace of a square linear operator discussed here.

Business use

Trace can make a dashboard or objective compact, but a single number should not conceal the structure needed for a decision. Two covariance matrices may have the same total variance while allocating it very differently across features.
Document what the matrix represents and why its diagonal total matters. If the summary is used to compare datasets or model versions, keep feature ordering, scaling, and preprocessing consistent, and provide the relevant component-level information alongside it.

People also ask

Can trace be negative?

Yes, for a general real square matrix. Covariance matrices, by contrast, have nonnegative diagonal variances.

Does equal trace mean two matrices are equal?

No. Equal sums leave many entries unconstrained.

Must I compute eigenvalues to obtain trace?

No. Adding the main diagonal is enough; the eigenvalue identity provides a related interpretation rather than a required computational procedure.

Check your understanding

A matrix has rows [4, 100] and [−8, −1]. What is its trace, and would changing 100 to 200 change that trace?

Answer: The trace is 4−1 = 3. Changing the off-diagonal entry does not change the trace, although it can change other matrix properties.

Related terms

  • Matrix
  • Eigenvalue
  • Covariance matrix
  • Determinant
  • diagonal
  • similarity transformation
  • Frobenius norm
  • matrix multiplication

Learn next

Verify the products AB and BA in the example and compare their entries before comparing their traces. Next, derive tr(X-transpose X) by expanding its diagonal sums. Study covariance scaling to understand when adding diagonal variances gives a meaningful statistical summary.

More in Mathematics and optimization

  • Absolute error loss - Absolute error loss measures a numerical prediction's error as the absolute difference between its predicted and reference values: L(y,y-hat)=|y-y-hat|.
  • Active-set method - An active-set method is an optimization approach that works with a changing subset of constraints believed to be tight at the solution.
  • Adjoint state method - The adjoint state method computes how a chosen objective changes when the parameters of a mathematical model change.
  • Σ-algebra
  • Algebraic connectivity - Algebraic connectivity is the second-smallest eigenvalue of a graph's Laplacian matrix.
  • Algorithm IMED - Algorithm IMED, short for Indexed Minimum Empirical Divergence, is a method for choosing actions in a stochastic multi-armed bandit problem.

Sources

Aalto Dictionary of Machine Learning, trace entry: definition, cyclic identity, eigenvalue sum, and similarity invariance. https://aaltodictionaryofml.github.io/ADictML_Main.pdf

Previous: Toxicity detection Next: Traceability

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer