Tr
Details
- Tr[list] sums the diagonal elements list[[i,i,…]].
- Tr works for rectangular as well as square matrices and tensors.
- Tr can be used on SparseArray objects. »
Examples
open all close allBasic Examples (1)
Scope (2)
Generalizations & Extensions (6)
For a vector Tr gives the sum of the elements:
Tr[{1, 2, 3}]For a higher‐rank tensor, Tr gives the sum of elements with equal indices:
Tr[Array[a, {4, 3, 2}]]Apply a function to the diagonal elements of a matrix:
Tr[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, f]Extract the diagonal of a matrix as a list:
Tr[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, List]Only consider down to level 1; this adds the rows of the matrix:
Tr[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, Plus, 1]Only consider down to level 2:
Tr[Array[a, {4, 3, 2}], f, 2]Applications (2)
Find the determinant of a triangular matrix:
Tr[{{1, 2, 3}, {0, 4, 5}, {0, 0, 6}}, Times]Det[{{1, 2, 3}, {0, 4, 5}, {0, 0, 6}}]Define an inner product for the cone of positive definite matrices using
:
traceProduct[a_, b_] := Tr[a.b]a = RandomReal[1, {3, 3}]; a = a.a;
b = RandomReal[1, {3, 3}];b = b.b;traceProduct[a, b]Project the matrix
onto the space spanned by the matrix
:
Projection[a, b, traceProduct]Properties & Relations (3)
The trace of a matrix is invariant under similarity transformations:
m = RandomReal[1, {10, 10}];
Tr[m]a = RandomReal[1, {10, 10}];
Tr[a.m.Inverse[a]]The invariance means that the sum of the eigenvalues must equal the trace:
Total[Eigenvalues[m]]The Frobenius norm is defined as
:
A = RandomReal[1, {3, 3}];Sqrt@Tr[A.Transpose[A]]Norm[A, "Frobenius"]Tr[m,List] is equivalent to Diagonal[m] for a matrix m:
m = (| | | |
| - | - | - |
| 1 | 2 | 3 |
| 4 | 5 | 6 |);Tr[m, List]Diagonal[m]See Also
Total Diagonal Transpose Det DiagonalMatrix Eigenvalues
Function Repository: MatrixPartialTrace FromTensor
Tech Notes
Related Guides
History
Introduced in 1999 (4.0) | Updated in 2003 (5.0)
Text
Wolfram Research (1999), Tr, Wolfram Language function, https://reference.wolfram.com/language/ref/Tr.html (updated 2003).
CMS
Wolfram Language. 1999. "Tr." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2003. https://reference.wolfram.com/language/ref/Tr.html.
APA
Wolfram Language. (1999). Tr. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Tr.html
BibTeX
@misc{reference.wolfram_2026_tr, author="Wolfram Research", title="{Tr}", year="2003", howpublished="\url{https://reference.wolfram.com/language/ref/Tr.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_tr, organization={Wolfram Research}, title={Tr}, year={2003}, url={https://reference.wolfram.com/language/ref/Tr.html}, note=[Accessed: 15-June-2026]}