Math - Trace

Trace Properties and Proofs, Cyclic Shifting, Trace Equals the Sum of Eigenvalues

Posted by Rico's Nerd Cluster on February 28, 2017

Trace Properties

The trace of a square matrix is just the sum of its diagonal entries: \(\operatorname{tr}(A) = \sum_i A_{ii}\). It looks simple, but a handful of its properties show up all over robotics, so let’s prove them one by one.

Linearity and Transpose

These all follow directly from the definition, because the trace only ever looks at the diagonal:

\[\begin{aligned} \operatorname{tr}(A + B) &= \sum_i (A_{ii} + B_{ii}) = \operatorname{tr}(A) + \operatorname{tr}(B), \\ \operatorname{tr}(cA) &= \sum_i c A_{ii} = c\,\operatorname{tr}(A), \\ \operatorname{tr}(A^\top) &= \operatorname{tr}(A) \quad (\text{transposing does not move diagonal entries}). \end{aligned}\]

Cyclic Shifting

\(\operatorname{tr}(AB) = \operatorname{tr}(BA)\). This holds even when \(AB\) and \(BA\) have different sizes, say \(A\) is \(m \times n\) and \(B\) is \(n \times m\).

By matrix multiplication:

\[(AB)_{ii}=\sum_{j=1}^{n}a_{ij}b_{ji}\]

Therefore:

\[\operatorname{tr}(AB) =\sum_{i=1}^{m}\sum_{j=1}^{n}a_{ij}b_{ji}\]

Since scalar multiplication is commutative:

\[=\sum_{i=1}^{m}\sum_{j=1}^{n}b_{ji}a_{ij}\]

Exchange the summation order:

\[=\sum_{j=1}^{n}\sum_{i=1}^{m}b_{ji}a_{ij}\]

Rename the dummy indices \(i\leftrightarrow j\):

\[=\sum_{i=1}^{n}\sum_{j=1}^{m}b_{ij}a_{ji}\]

This is precisely \(\sum_{i=1}^{n}(BA)_{ii}\):

\[\boxed{\operatorname{tr}(AB)=\operatorname{tr}(BA)}\]

Extension: applying this repeatedly, we can rotate a longer product around:

\[\operatorname{tr}(ABC) = \operatorname{tr}(CAB) = \operatorname{tr}(BCA)\]

This is allowed because it is a cyclic permutation. But \(\operatorname{tr}(ACB)\) is not allowed: in general, \(\operatorname{tr}(ABC) \ne \operatorname{tr}(ACB)\).

Invariance Under Change of Basis

For any invertible \(P\), cyclic shifting gives:

\[\operatorname{tr}(P^{-1}AP) = \operatorname{tr}(A P P^{-1}) = \operatorname{tr}(A)\]

So the trace doesn’t care which coordinate system we use. That’s what allows it to equal something basis-independent, like the sum of eigenvalues below.

Trace Equals the Sum of Eigenvalues

\[\operatorname{tr}(A) = \sum_i \lambda_i\]

For a diagonal matrix this is obvious, since the eigenvalues are the diagonal entries:

\[D = \begin{bmatrix} d_1 & 0 & 0 \\ 0 & d_2 & 0 \\ 0 & 0 & d_3 \end{bmatrix} \quad\Rightarrow\quad \operatorname{tr}(D) = d_1 + d_2 + d_3\]

Quick proof for a symmetric matrix. A symmetric \(H\) has an eigendecomposition \(H = V\Lambda V^\top\), where \(V\) is orthonormal and \(\Lambda = \operatorname{diag}(\lambda_1, \dots, \lambda_n)\). Using cyclic shifting to move \(V\) around, and \(V^\top V = I\):

\[\operatorname{tr}(H) = \operatorname{tr}(V\Lambda V^\top) = \operatorname{tr}(\Lambda V^\top V) = \operatorname{tr}(\Lambda) = \sum_i \lambda_i\]

The same argument works for any diagonalizable matrix \(A = V\Lambda V^{-1}\), by the change-of-basis invariance above.

General proof, using the characteristic polynomial. The eigenvalues are its roots, so we can factor it as

\[\det(\lambda I - A) = \prod_{i=1}^n (\lambda - \lambda_i) = \lambda^n - \Big(\sum_i \lambda_i\Big)\lambda^{n-1} + \cdots\]

Now let’s expand the same determinant directly. A determinant is a sum of products, where each product picks one entry from every row and every column. Only the product of the diagonal entries, \(\prod_i (\lambda - A_{ii})\), can reach \(\lambda^{n-1}\). Any other product has to skip at least two diagonal entries, so it has at most \(n-2\) factors of \(\lambda\). Expanding the diagonal product:

\[\prod_i (\lambda - A_{ii}) = \lambda^n - \Big(\sum_i A_{ii}\Big)\lambda^{n-1} + \cdots\]

Matching the \(\lambda^{n-1}\) coefficients of the two expansions gives \(\sum_i A_{ii} = \sum_i \lambda_i\). This works for every square matrix, as long as we count eigenvalues with multiplicity (and allow them to be complex). As a bonus, comparing the constant terms the same way gives \(\det(A) = \prod_i \lambda_i\).

Trace of an Outer Product

\[\operatorname{tr}(\mathbf{x}\mathbf{y}^\top) = \sum_i x_i y_i = \mathbf{x}^\top\mathbf{y}, \qquad \operatorname{tr}(\mathbf{x}\mathbf{x}^\top) = \|\mathbf{x}\|^2\]

The diagonal entries of \(\mathbf{x}\mathbf{y}^\top\) are \(x_i y_i\), and summing them gives the dot product. Another way to see it: by cyclic shifting, \(\operatorname{tr}(\mathbf{x}\mathbf{y}^\top) = \operatorname{tr}(\mathbf{y}^\top\mathbf{x})\), and \(\mathbf{y}^\top\mathbf{x}\) is a \(1 \times 1\) matrix, which is its own trace.

Trace of a Squared Skew-Symmetric Matrix

Let \(\mathbf{v} \in \mathbb{R}^3\), and let \([\mathbf{v}]_\times\) (also written \(\mathbf{v}^\wedge\)) be its skew-symmetric matrix, so that \([\mathbf{v}]_\times \mathbf{w} = \mathbf{v} \times \mathbf{w}\). Then

\[\operatorname{tr}\left([\mathbf{v}]_\times^\top [\mathbf{v}]_\times\right) = 2\|\mathbf{v}\|^2, \qquad \operatorname{tr}\left([\mathbf{v}]_\times^2\right) = -2\|\mathbf{v}\|^2\]

First, we find what \([\mathbf{v}]_\times^\top [\mathbf{v}]_\times\) actually is. Since \([\mathbf{v}]_\times^\top = -[\mathbf{v}]_\times\), applying it to any vector \(\mathbf{w}\) and using the triple product expansion \(\mathbf{a} \times (\mathbf{b} \times \mathbf{c}) = \mathbf{b}(\mathbf{a}\cdot\mathbf{c}) - \mathbf{c}(\mathbf{a}\cdot\mathbf{b})\) gives

\[[\mathbf{v}]_\times^\top [\mathbf{v}]_\times \mathbf{w} = -\mathbf{v} \times (\mathbf{v} \times \mathbf{w}) = -\mathbf{v}(\mathbf{v}\cdot\mathbf{w}) + \mathbf{w}\|\mathbf{v}\|^2 \quad\Rightarrow\quad [\mathbf{v}]_\times^\top [\mathbf{v}]_\times = \|\mathbf{v}\|^2 I - \mathbf{v}\mathbf{v}^\top\]

Then, taking the trace with linearity and the outer-product result above:

\[\operatorname{tr}\left(\|\mathbf{v}\|^2 I - \mathbf{v}\mathbf{v}^\top\right) = 3\|\mathbf{v}\|^2 - \|\mathbf{v}\|^2 = 2\|\mathbf{v}\|^2\]

Since \([\mathbf{v}]_\times^2 = -[\mathbf{v}]_\times^\top[\mathbf{v}]_\times\), the second form just flips the sign. This identity shows up in the quaternion-to-angle derivation and in the rotation block of an ICP Hessian.

Trace of a Rotation Matrix

A rotation by angle \(\theta\) about a unit axis \(\mathbf{a}\) has trace

\[\operatorname{tr}(R) = 1 + 2\cos\theta\]

To see this, start from Rodrigues’ formula, \(R = \cos\theta\, I + (1 - \cos\theta)\,\mathbf{a}\mathbf{a}^\top + \sin\theta\,[\mathbf{a}]_\times\), and take the trace term by term. We have \(\operatorname{tr}(I) = 3\) and \(\operatorname{tr}(\mathbf{a}\mathbf{a}^\top) = \|\mathbf{a}\|^2 = 1\). The last term vanishes, because a skew-symmetric matrix has zeros on its diagonal, so \(\operatorname{tr}([\mathbf{a}]_\times) = 0\). Putting it together:

\[\operatorname{tr}(R) = 3\cos\theta + (1 - \cos\theta) = 1 + 2\cos\theta \quad\Rightarrow\quad \theta = \cos^{-1}\left(\frac{\operatorname{tr}(R) - 1}{2}\right)\]

This is exactly how we recover the rotation angle from a rotation matrix.