Trace (linear algebra)

In linear algebra, the trace of a square matrix $A$, denoted $tr(A)$, is defined to be the sum of elements on the main diagonal (from the upper left to the lower right) of $A$. The trace is only defined for a square matrix ($n × n$).

In mathematical physics texts, if $tr(A)$ = 0 then the matrix is said to be traceless. This is a minomer, but widely used, such as in the Pauli Matrices.

It can be proven that the trace of a matrix is the sum of its eigenvalues (counted with multiplicities). It can also be proven that $tr(AB) = tr(BA)$ for any two matrices $A$ and $B$ of appropriate sizes. This implies that similar matrices have the same trace. As a consequence one can define the trace of a linear operator mapping a finite-dimensional vector space into itself, since all matrices describing such an operator with respect to a basis are similar.

The trace is related to the derivative of the determinant (see Jacobi's formula).

Definition
The trace of an $n × n$ square matrix $A$ is defined as $$\operatorname{tr}(\mathbf{A}) = \sum_{i=1}^n a_{ii} = a_{11} + a_{22} + \dots + a_{nn}$$ where $a_{ii}$ denotes the entry on the $i$&thinsp;th row and $i$&thinsp;th column of $A$. The entries of $A$ can be real numbers, complex numbers, or more generally elements of a field $F$. The trace is not defined for non-square matrices.

Example
Let $A$ be a matrix, with $$\mathbf{A} = \begin{pmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{pmatrix} = \begin{pmatrix} 1 & 0 &  3 \\  11 &  5 &  2 \\   6 & 12 & -5 \end{pmatrix} $$

Then $$\operatorname{tr}(\mathbf{A}) = \sum_{i=1}^{3} a_{ii} = a_{11} + a_{22} + a_{33} = 1 + 5 + (-5) = 1$$

Basic properties
The trace is a linear mapping. That is, $$\begin{align} \operatorname{tr}(\mathbf{A} + \mathbf{B}) &= \operatorname{tr}(\mathbf{A}) + \operatorname{tr}(\mathbf{B}) \\ \operatorname{tr}(c\mathbf{A}) &= c \operatorname{tr}(\mathbf{A}) \end{align}$$ for all square matrices $A$ and $B$, and all scalars $c$.

A matrix and its transpose have the same trace: $$\operatorname{tr}(\mathbf{A}) = \operatorname{tr}\left(\mathbf{A}^\mathsf{T}\right).$$

This follows immediately from the fact that transposing a square matrix does not affect elements along the main diagonal.

Trace of a product
The trace of a square matrix which is the product of two matrices can be rewritten as the sum of entry-wise products of their elements, i.e. as the sum of all elements of their Hadamard product. Phrased directly, if $A$ and $B$ are two $m × n$ matrices, then: $$ \operatorname{tr}\left(\mathbf{A}^\mathsf{T}\mathbf{B}\right) = \operatorname{tr}\left(\mathbf{A}\mathbf{B}^\mathsf{T}\right) = \operatorname{tr}\left(\mathbf{B}^\mathsf{T}\mathbf{A}\right) = \operatorname{tr}\left(\mathbf{B}\mathbf{A}^\mathsf{T}\right) = \sum_{i=1}^m \sum_{j=1}^n a_{ij}b_{ij} \;. $$

If one views any real $m × n$ matrix as a vector of length $mn$ (an operation called vectorization) then the above operation on $A$ and $B$ coincides with the standard dot product. According to the above expression, $tr(A^{⊤}A)$ is a sum of squares and hence is nonnegative, equal to zero if and only if $A$ is zero. Furthermore, as noted in the above formula, $tr(A^{⊤}B) = tr(B^{⊤}A)$. These demonstrate the positive-definiteness and symmetry required of an inner product; it is common to call $tr(A^{⊤}B)$ the Frobenius inner product of $A$ and $B$. This is a natural inner product on the vector space of all real matrices of fixed dimensions. The norm derived from this inner product is called the Frobenius norm, and it satisfies a submultiplicative property, as can be proven with the Cauchy–Schwarz inequality: $$0 \leq \left[\operatorname{tr}(\mathbf{A} \mathbf{B})\right]^2 \leq \operatorname{tr}\left(\mathbf{A}^2\right) \operatorname{tr}\left(\mathbf{B}^2\right) \leq \left[\operatorname{tr}(\mathbf{A})\right]^2 \left[\operatorname{tr}(\mathbf{B})\right]^2 \ ,$$ if $A$ and $B$ are real positive semi-definite matrices of the same size. The Frobenius inner product and norm arise frequently in matrix calculus and statistics.

The Frobenius inner product may be extended to a hermitian inner product on the complex vector space of all complex matrices of a fixed size, by replacing $B$ by its complex conjugate.

The symmetry of the Frobenius inner product may be phrased more directly as follows: the matrices in the trace of a product can be switched without changing the result. If $A$ and $B$ are $m × n$ and $n × m$ real or complex matrices, respectively, then

This is notable both for the fact that $AB$ does not usually equal $BA$, and also since the trace of either does not usually equal $tr(A)tr(B)$. The similarity-invariance of the trace, meaning that $tr(AB) = 1 ≠ 0 ⋅ 0 = tr(A)tr(B)$ for any square matrix $tr(A) = tr(P^{−1}AP)$ and any invertible matrix $A$ of the same dimensions, is a fundamental consequence. This is proved by $$ \operatorname{tr}\left(\mathbf{P}^{-1}(\mathbf{A}\mathbf{P})\right) = \operatorname{tr}\left((\mathbf{A} \mathbf{P})\mathbf{P}^{-1}\right) = \operatorname{tr}(\mathbf{A}). $$ Similarity invariance is the crucial property of the trace in order to discuss traces of linear transformations as below.

Additionally, for real column vectors $$\mathbf{a}\in\mathbb{R}^n$$ and $$\mathbf{b}\in\mathbb{R}^n$$, the trace of the outer product is equivalent to the inner product:

Cyclic property
More generally, the trace is invariant under circular shifts, that is,

This is known as the cyclic property.

Arbitrary permutations are not allowed: in general, $$\operatorname{tr}(\mathbf{A}\mathbf{B}\mathbf{C}) \ne \operatorname{tr}(\mathbf{A}\mathbf{C}\mathbf{B}).$$

However, if products of three symmetric matrices are considered, any permutation is allowed, since: $$\operatorname{tr}(\mathbf{A}\mathbf{B}\mathbf{C}) = \operatorname{tr}\left(\left(\mathbf{A}\mathbf{B}\mathbf{C}\right)^{\mathsf T}\right) = \operatorname{tr}(\mathbf{C}\mathbf{B}\mathbf{A}) = \operatorname{tr}(\mathbf{A}\mathbf{C}\mathbf{B}),$$ where the first equality is because the traces of a matrix and its transpose are equal. Note that this is not true in general for more than three factors.

Trace of a Kronecker product
The trace of the Kronecker product of two matrices is the product of their traces: $$\operatorname{tr}(\mathbf{A} \otimes \mathbf{B}) = \operatorname{tr}(\mathbf{A})\operatorname{tr}(\mathbf{B}).$$

Characterization of the trace
The following three properties: $$\begin{align} \operatorname{tr}(\mathbf{A} + \mathbf{B}) &= \operatorname{tr}(\mathbf{A}) + \operatorname{tr}(\mathbf{B}), \\ \operatorname{tr}(c\mathbf{A}) &= c \operatorname{tr}(\mathbf{A}), \\ \operatorname{tr}(\mathbf{A}\mathbf{B}) &= \operatorname{tr}(\mathbf{B}\mathbf{A}), \end{align}$$ characterize the trace up to a scalar multiple in the following sense: If $$f$$ is a linear functional on the space of square matrices that satisfies $$f(xy) = f(yx),$$ then $$f$$ and $$\operatorname{tr}$$ are proportional.

For $$n\times n$$ matrices, imposing the normalization $$f(\mathbf{I}) = n$$ makes $$f$$ equal to the trace.

Trace as the sum of eigenvalues
Given any $P$ matrix $n × n$, there is

where $A$ are the eigenvalues of $&lambda;_{1}, ..., &lambda;_{n}$ counted with multiplicity. This holds true even if $A$ is a real matrix and some (or all) of the eigenvalues are complex numbers. This may be regarded as a consequence of the existence of the Jordan canonical form, together with the similarity-invariance of the trace discussed above.

Trace of commutator
When both $A$ and $A$ are $B$ matrices, the trace of the (ring-theoretic) commutator of $n × n$ and $A$ vanishes: $B$, because $tr([A, B]) = 0$ and $tr(AB) = tr(BA)$ is linear. One can state this as "the trace is a map of Lie algebras $tr$ from operators to scalars", as the commutator of scalars is trivial (it is an Abelian Lie algebra). In particular, using similarity invariance, it follows that the identity matrix is never similar to the commutator of any pair of matrices.

Conversely, any square matrix with zero trace is a linear combination of the commutators of pairs of matrices. Moreover, any square matrix with zero trace is unitarily equivalent to a square matrix with diagonal consisting of all zeros.

Traces of special kinds of matrices
• The trace of the $gl_{n} → k$ identity matrix is the dimension of the space, namely $n$. $\operatorname{tr}\left(\mathbf{I}_n\right) = n$ This leads to generalizations of dimension using trace.

• The trace of a Hermitian matrix is real, because the elements on the diagonal are real.

• The trace of a permutation matrix is the number of fixed points of the corresponding permutation, because the diagonal term $n × n$ is 1 if the $a_{ii}$th point is fixed and 0 otherwise.

• The trace of a projection matrix is the dimension of the target space. $\begin{align} \mathbf{P}_\mathbf{X} &= \mathbf{X}\left(\mathbf{X}^\mathsf{T} \mathbf{X}\right)^{-1} \mathbf{X}^\mathsf{T} \\[3pt] \Longrightarrow \operatorname{tr}\left(\mathbf{P}_\mathbf{X}\right) &= \operatorname{rank}(\mathbf{X}). \end{align}$ The matrix $i$ is idempotent.

• More generally, the trace of any idempotent matrix, i.e. one with $P_{X}$, equals its own rank.

• The trace of a nilpotent matrix is zero.

When the characteristic of the base field is zero, the converse also holds: if $A^{2} = A$ for all $k$, then $tr(A^{k}) = 0$ is nilpotent.

When the characteristic $A$ is positive, the identity in $n$ dimensions is a counterexample, as $\operatorname{tr}\left(\mathbf{I}_n^k\right) = \operatorname{tr}\left(\mathbf{I}_n\right) = n \equiv 0$, but the identity is not nilpotent.

Relationship to the characteristic polynomial
The trace of an $$n \times n$$ matrix $$A$$ is the coefficient of $$t^{n-1}$$ in the characteristic polynomial, possibly changed of sign, according to the convention in the definition of the characteristic polynomial.

Relationship to eigenvalues
If $n > 0$ is a linear operator represented by a square matrix with real or complex entries and if $A$ are the eigenvalues of $λ_{1}, ..., λ_{n}$ (listed according to their algebraic multiplicities), then

This follows from the fact that $A$ is always similar to its Jordan form, an upper triangular matrix having $A$ on the main diagonal. In contrast, the determinant of $λ_{1}, ..., λ_{n}$ is the product of its eigenvalues; that is, $$\det(\mathbf{A}) = \prod_i \lambda_i.$$

Everything in the present section applies as well to any square matrix with coefficients in an algebraically closed field.

Derivative relationships
If $A$ is a square matrix with small entries and $ΔA$ denotes the identity matrix, then we have approximately

$$\det(\mathbf{I}+\mathbf{\Delta A})\approx 1 + \operatorname{tr}(\mathbf{\Delta A}).$$

Precisely this means that the trace is the derivative of the determinant function at the identity matrix. Jacobi's formula

$$d\det(\mathbf{A}) = \operatorname{tr} \big(\operatorname{adj}(\mathbf{A})\cdot d\mathbf{A}\big)$$

is more general and describes the differential of the determinant at an arbitrary square matrix, in terms of the trace and the adjugate of the matrix.

From this (or from the connection between the trace and the eigenvalues), one can derive a relation between the trace function, the matrix exponential function, and the determinant:$$\det(\exp(\mathbf{A})) = \exp(\operatorname{tr}(\mathbf{A})).$$

A related characterization of the trace applies to linear vector fields. Given a matrix $I$, define a vector field $A$ on $F$ by $R^{n}$. The components of this vector field are linear functions (given by the rows of $F(x) = Ax$). Its divergence $A$ is a constant function, whose value is equal to $div F$.

By the divergence theorem, one can interpret this in terms of flows: if $tr(A)$ represents the velocity of a fluid at location $F(x)$ and $U$ is a region in $x$, the net flow of the fluid out of $U$ is given by $R^{n}$, where $tr(A) · vol(U)$ is the volume of $U$.

The trace is a linear operator, hence it commutes with the derivative: $$d \operatorname{tr} (\mathbf{X}) = \operatorname{tr}(d\mathbf{X}) .$$

Trace of a linear operator
In general, given some linear map $vol(U)$ (where $V$ is a finite-dimensional vector space), we can define the trace of this map by considering the trace of a matrix representation of $f$, that is, choosing a basis for $V$ and describing $f$ as a matrix relative to this basis, and taking the trace of this square matrix. The result will not depend on the basis chosen, since different bases will give rise to similar matrices, allowing for the possibility of a basis-independent definition for the trace of a linear map.

Such a definition can be given using the canonical isomorphism between the space $f : V → V$ of linear maps on $V$ and $End(V)$, where $V ⊗ V*$ is the dual space of $V$. Let $v$ be in $V$ and let $g$ be in $V*$. Then the trace of the indecomposable element $V*$ is defined to be $v ⊗ g$; the trace of a general element is defined by linearity. The trace of a linear map $g(v)$ can then be defined as the trace, in the above sense, of the element of $f : V → V$ corresponding to f under the above mentioned canonical isomorphism. Using an explicit basis for $V$ and the corresponding dual basis for $V ⊗ V*$, one can show that this gives the same definition of the trace as given above.

Stochastic estimator
The trace can be estimated unbiasedly by "Hutchinson's trick": "Given any matrix $W\in \R^{n\times n}$, and any random $u\in \R^n$ with $E[uu^T] = I$, we have $E[u^T W u] = tr(W)$. (Proof: expand the expectation directly.)"Usually, the random vector is sampled from $$N(0, I)$$ (normal distribution) or $$\{\pm n^{-1/2}\}^n$$ (Rademacher distribution).

More sophisticated stochastic estimators of trace have been developed.

Applications
If a 2 x 2 real matrix has zero trace, its square is a diagonal matrix.

The trace of a 2 × 2 complex matrix is used to classify Möbius transformations. First, the matrix is normalized to make its determinant equal to one. Then, if the square of the trace is 4, the corresponding transformation is parabolic. If the square is in the interval [0,4), it is elliptic. Finally, if the square is greater than 4, the transformation is loxodromic. See classification of Möbius transformations.

The trace is used to define characters of group representations. Two representations $V*$ of a group $G$ are equivalent (up to change of basis on $V$) if $A, B : G → GL(V)$ for all $tr(A(g)) = tr(B(g))$.

The trace also plays a central role in the distribution of quadratic forms.

Lie algebra
The trace is a map of Lie algebras $$\operatorname{tr}:\mathfrak{gl}_n\to K$$ from the Lie algebra $$\mathfrak{gl}_n$$ of linear operators on an $n$-dimensional space ($g ∈ G$ matrices with entries in $$K$$) to the Lie algebra $K$ of scalars; as $K$ is Abelian (the Lie bracket vanishes), the fact that this is a map of Lie algebras is exactly the statement that the trace of a bracket vanishes: $$\operatorname{tr}([\mathbf{A}, \mathbf{B}]) = 0 \text{ for each }\mathbf A,\mathbf B\in\mathfrak{gl}_n.$$

The kernel of this map, a matrix whose trace is zero, is often said to be ' or ', and these matrices form the simple Lie algebra $$\mathfrak{sl}_n$$, which is the Lie algebra of the special linear group of matrices with determinant 1. The special linear group consists of the matrices which do not change volume, while the special linear Lie algebra is the matrices which do not alter volume of infinitesimal sets.

In fact, there is an internal direct sum decomposition $$\mathfrak{gl}_n = \mathfrak{sl}_n \oplus K$$ of operators/matrices into traceless operators/matrices and scalars operators/matrices. The projection map onto scalar operators can be expressed in terms of the trace, concretely as: $$\mathbf{A} \mapsto \frac{1}{n}\operatorname{tr}(\mathbf{A})\mathbf{I}.$$

Formally, one can compose the trace (the counit map) with the unit map $$K\to\mathfrak{gl}_n$$ of "inclusion of scalars" to obtain a map $$\mathfrak{gl}_n\to\mathfrak{gl}_n$$ mapping onto scalars, and multiplying by $n$. Dividing by $n$ makes this a projection, yielding the formula above.

In terms of short exact sequences, one has $$0 \to \mathfrak{sl}_n \to \mathfrak{gl}_n \overset{\operatorname{tr}}{\to} K \to 0$$ which is analogous to $$1 \to \operatorname{SL}_n \to \operatorname{GL}_n \overset{\det}{\to} K^* \to 1$$ (where $$K^*=K\setminus\{0\}$$) for Lie groups. However, the trace splits naturally (via $$1/n$$ times scalars) so $$\mathfrak{gl}_n=\mathfrak{sl}_n\oplus K$$, but the splitting of the determinant would be as the $n$th root times scalars, and this does not in general define a function, so the determinant does not split and the general linear group does not decompose: $$\operatorname{GL}_n \neq \operatorname{SL}_n \times K^*.$$

Bilinear forms
The bilinear form (where $n × n$, $X$ are square matrices) $$B(\mathbf{X}, \mathbf{Y}) = \operatorname{tr}(\operatorname{ad}(\mathbf{X})\operatorname{ad}(\mathbf{Y}))\quad \text{where } \operatorname{ad}(\mathbf{X})\mathbf{Y} = [\mathbf{X}, \mathbf{Y}] = \mathbf{X}\mathbf{Y} - \mathbf{Y}\mathbf{X}$$ is called the Killing form, which is used for the classification of Lie algebras.

The trace defines a bilinear form: $$(\mathbf{X}, \mathbf{Y}) \mapsto \operatorname{tr}(\mathbf{X}\mathbf{Y}).$$

The form is symmetric, non-degenerate and associative in the sense that: $$\operatorname{tr}(\mathbf{X}[\mathbf{Y}, \mathbf{Z}]) = \operatorname{tr}([\mathbf{X}, \mathbf{Y}]\mathbf{Z}).$$

For a complex simple Lie algebra (such as $Y$), every such bilinear form is proportional to each other; in particular, to the Killing form.

Two matrices $tr(A*A) = 0$ and $A = 0$ are said to be trace orthogonal if $$\operatorname{tr}(\mathbf{X}\mathbf{Y}) = 0.$$

There is a generalization to a general representation $$(\rho,\mathfrak{g},V)$$ of a Lie algebra $$\mathfrak{g}$$, such that $$\rho$$ is a homomorphism of Lie algebras $$\rho: \mathfrak{g} \rightarrow \text{End}(V).$$ The trace form $$\text{tr}_V$$ on $$\text{End}(V)$$ is defined as above. The bilinear form $$\phi(\mathbf{X},\mathbf{Y}) = \text{tr}_V(\rho(\mathbf{X})\rho(\mathbf{Y}))$$ is symmetric and invariant due to cyclicity.

Generalizations
The concept of trace of a matrix is generalized to the trace class of compact operators on Hilbert spaces, and the analog of the Frobenius norm is called the Hilbert–Schmidt norm.

If $K$ is a trace-class operator, then for any orthonormal basis $$(e_n)_n$$, the trace is given by $$\operatorname{tr}(K) = \sum_n \left\langle e_n, Ke_n \right\rangle,$$ and is finite and independent of the orthonormal basis.

The partial trace is another generalization of the trace that is operator-valued. The trace of a linear operator $Z$ which lives on a product space $$\mathfrak{sl}$_{n}$ is equal to the partial traces over $A$ and $B$: $$\operatorname{tr}(Z) = \operatorname{tr}_A \left(\operatorname{tr}_B(Z)\right) = \operatorname{tr}_B \left(\operatorname{tr}_A(Z)\right).$$

For more properties and a generalization of the partial trace, see traced monoidal categories.

If $A$ is a general associative algebra over a field $k$, then a trace on $A$ is often defined to be any map $X$ which vanishes on commutators; $Y$ for all $A ⊗ B$. Such a trace is not uniquely defined; it can always at least be modified by multiplication by a nonzero scalar.

A supertrace is the generalization of a trace to the setting of superalgebras.

The operation of tensor contraction generalizes the trace to arbitrary tensors.

Traces in the language of tensor products
Given a vector space $V$, there is a natural bilinear map $tr : A ↦ k$ given by sending $tr([a,b]) = 0$ to the scalar $a, b ∈ A$. The universal property of the tensor product $V × V^{∗} → F$ automatically implies that this bilinear map is induced by a linear functional on $(v, &phi;)$.

Similarly, there is a natural bilinear map $&phi;(v)$ given by sending $V ⊗ V^{∗}$ to the linear map $V ⊗ V^{∗}$. The universal property of the tensor product, just as used previously, says that this bilinear map is induced by a linear map $V × V^{∗} → Hom(V, V)$. If $V$ is finite-dimensional, then this linear map is a linear isomorphism. This fundamental fact is a straightforward consequence of the existence of a (finite) basis of $V$, and can also be phrased as saying that any linear map $(v, &phi;)$ can be written as the sum of (finitely many) rank-one linear maps. Composing the inverse of the isomorphism with the linear functional obtained above results in a linear functional on $w ↦ &phi;(w)v$. This linear functional is exactly the same as the trace.

Using the definition of trace as the sum of diagonal elements, the matrix formula $V ⊗ V^{∗} → Hom(V, V)$ is straightforward to prove, and was given above. In the present perspective, one is considering linear maps $S$ and $T$, and viewing them as sums of rank-one maps, so that there are linear functionals $V → V$ and $Hom(V, V)$ and nonzero vectors $tr(AB) = tr(BA)$ and $&phi;_{i}$ such that $&psi;_{j}$ and $v_{i}$ for any $u$ in $V$. Then
 * $$(S\circ T)(u)=\sum_i\varphi_i\left(\sum_j\psi_j(u)w_j\right)v_i=\sum_i\sum_j\psi_j(u)\varphi_i(w_j)v_i $$

for any $u$ in $V$. The rank-one linear map $w_{j}$ has trace $S(u) = Σ&phi;_{i}(u)v_{i}$ and so
 * $$\operatorname{tr}(S\circ T)=\sum_i\sum_j\psi_j(v_i)\varphi_i(w_j)=\sum_j\sum_i\varphi_i(w_j)\psi_j(v_i).$$

Following the same procedure with $S$ and $T$ reversed, one finds exactly the same formula, proving that $T(u) = Σ&psi;_{j}(u)w_{j}$ equals $u ↦ &psi;_{j}(u)&phi;_{i}(w_{j})v_{i}$.

The above proof can be regarded as being based upon tensor products, given that the fundamental identity of $&psi;_{j}(v_{i})&phi;_{i}(w_{j})$ with $tr(S ∘ T)$ is equivalent to the expressibility of any linear map as the sum of rank-one linear maps. As such, the proof may be written in the notation of tensor products. Then one may consider the multilinear map $tr(T ∘ S)$ given by sending $End(V)$ to $V ⊗ V^{∗}$. Further composition with the trace map then results in $V × V^{∗} × V × V^{∗} → V ⊗ V^{∗}$, and this is unchanged if one were to have started with $(v, &phi;, w, &psi;)$ instead. One may also consider the bilinear map $&phi;(w)v ⊗ &psi;$ given by sending $&phi;(w)&psi;(v)$ to the composition $(w, &psi;, v, &phi;)$, which is then induced by a linear map $End(V) × End(V) → End(V)$. It can be seen that this coincides with the linear map $(f, g)$. The established symmetry upon composition with the trace map then establishes the equality of the two traces.

For any finite dimensional vector space $V$, there is a natural linear map $f ∘ g$; in the language of linear maps, it assigns to a scalar $c$ the linear map $End(V) ⊗ End(V) → End(V)$. Sometimes this is called coevaluation map, and the trace $V ⊗ V^{∗} ⊗ V ⊗ V^{∗} → V ⊗ V^{∗}$ is called evaluation map. These structures can be axiomatized to define categorical traces in the abstract setting of category theory.