Scientia sine finibus

Linear Algebra / Unit 3, Matrices / Topic 3.5

Units and Topics

Unit 3. Matrices

3.5 The Trace

This topic defines the trace of a square matrix, which is the sum of its diagonal entries. The trace is simple to calculate, and it follows rules that make it useful in the later units and in quantum mechanics.

You need Topics 3.2, 3.3, and 3.4 for this topic.

1. The Definition

The trace of a square matrix is the sum of the entries on its main diagonal. It is written tr A.

tr abcd = a + d tr 211433879 = 2 + 3 + 9add the entries on the main diagonal= 14

Only a square matrix has a trace. The trace of the n by n identity matrix is n.

2. Rules for the Trace

The trace works with addition and with multiplication by a number in the expected way, and it does not change under the transpose.

tr(A + B) = tr A + tr B tr(cA) = c tr A tr(AT) = tr A

The first two rules say that the trace is linear. The determinant does not follow these two rules, as Topic 3.4 showed.

3. The Trace of a Product

The most important rule is about products. The matrices AB and BA are usually different, but they always have the same trace.

tr(AB) = tr(BA)

For example,

A = 1234 B = 0152 AB = 1052011 BA = 341118

The two products are different matrices, but their traces are equal.

tr(AB) = 10 + 11 = 21tr(BA) = 3 + 18 = 21

The reason is in the entry rule for a product. The diagonal entry of AB in row i is the sum of the products aijbji over j. So tr(AB) is the sum of aijbji over all i and all j. The trace of BA is the sum of bjiaij over all j and all i, which contains the same products.

Try it: two products, one trace

Press New matrices to choose two matrices at random. The products AB and BA are usually different, but their traces are equal.

Three or more matrices

For a product of three matrices, the rule allows the first matrix to move to the end, or the last matrix to move to the front.

tr(ABC) = tr(BCA) = tr(CAB)

This is called the cyclic property of the trace. It does not allow any other change of order. In general, tr(ABC) is not equal to tr(ACB). With the matrices A and B above and C = 1102, tr(ABC) = 52 and tr(ACB) = 50.

4. The Trace Does Not Depend on the Coordinates

Let P be a matrix that has an inverse. The matrices A and P⁻¹AP are called similar. Topic 4.6 shows that similar matrices describe the same map in two different sets of coordinates. The cyclic property shows that similar matrices have the same trace.

tr(P⁻¹AP) = tr(APP⁻¹) = tr A

The product rule for determinants shows that they also have the same determinant, because det(P⁻¹) det(P) = 1.

For example, take

A = 2112 P = 111−1 P⁻¹AP = 3001

Both matrices have the trace 4 and the determinant 3.

The trace and the determinant belong to the map, not to the particular table of numbers that describes it. Unit 5 shows what they are in terms of the map: the trace is the sum of the eigenvalues, and the determinant is their product.

5. Commutators

The matrix AB − BA is called the commutator of A and B. It measures how far the two matrices are from having the same product in both orders. From Part 3, the trace of a commutator is always 0.

tr(AB − BA) = tr(AB) − tr(BA)the trace is lineartr(AB − BA) = 0the two traces are equal

For the matrices of Part 3, AB − BA = 719−7, and the trace is 7 − 7 = 0.

This fact has a consequence for quantum mechanics. Position and momentum are represented by operators X and P whose commutator is a number times the identity. For n by n matrices this is impossible, because the trace of a commutator is 0 and the trace of the identity is n. So position and momentum cannot be matrices of any finite size. Unit 9 introduces the vector spaces of infinite dimension that they need.

6. The Size of a Matrix

For any matrix A, the trace of AT A is the sum of the squares of all of the entries of A.

A = 1234 AT A = 10141420 tr(AT A) = 30 = 1 + 4 + 9 + 16

The square root of this number measures the size of a matrix, in the same way that √v · v measures the length of a vector. For two matrices of the same size, tr(AT B) is the sum of the products of matching entries. It is the dot product of the two matrices, treated as long lists of numbers.

7. The Trace of a Rotation

The trace of a rotation matrix gives the angle of the rotation. In two dimensions, the rotation by θ from Topic 3.1 has the trace 2 cos θ. In three dimensions, a rotation by θ about any axis has the trace 1 + 2 cos θ. For a rotation about the z-axis, this can be read from the matrix.

R = cos θ−sin θ0sin θcos θ0001 tr R = 1 + 2 cos θ

A rotation about a different axis is similar to this matrix, so it has the same trace. To find the angle of any rotation in three dimensions, calculate cos θ = (tr R − 1) / 2.

Common Mistakes

  • Adding the entries of the other diagonal. The trace uses the main diagonal, from the upper left to the lower right.
  • Calculating tr(AB) as (tr A)(tr B). The trace of a product is not the product of the traces.
  • Changing the order in a product of three matrices freely. Only cyclic changes keep the trace.
  • Concluding that AB = BA because tr(AB) = tr(BA). Equal traces do not mean equal matrices.
  • Taking the trace of a matrix that is not square.

Summary

FactStatement
DefinitionThe sum of the entries on the main diagonal
Lineartr(A + B) = tr A + tr B and tr(cA) = c tr A
Producttr(AB) = tr(BA)
Cyclic propertytr(ABC) = tr(BCA) = tr(CAB)
Similar matricestr(P⁻¹AP) = tr A
Commutatortr(AB − BA) = 0
Sum of squarestr(AT A) is the sum of the squares of the entries of A

Practice

Work each problem on paper first. Then open the answer to check your work.

1. Find the trace of 4−1203751−6.

4 + 3 − 6 = 1.

2. For A = 1203 and B = 2011, calculate AB and BA, and check that their traces are equal.

AB = 4233 and BA = 2415.

tr(AB) = 4 + 3 = 7tr(BA) = 2 + 5 = 7

3. tr A = 5 and tr B = −2 for two 3 by 3 matrices. Find tr(2A − 3B) and tr(AT).

tr(2A − 3B) = 2 tr A − 3 tr Bthe trace is lineartr(2A − 3B) = 2(5) − 3(−2) = 16tr(AT) = 5the transpose has the same trace

4. Explain why no two 2 by 2 matrices satisfy AB − BA = I.

The trace of AB − BA is 0 for any two square matrices. The trace of the 2 by 2 identity matrix is 2. A matrix with the trace 0 cannot be equal to a matrix with the trace 2.

5. A rotation matrix in three dimensions has the trace 2. Find the angle of the rotation.

1 + 2 cos θ = 2the trace of a rotation by θcos θ = (2 − 1)/2 = 1/2subtract 1, then divide by 2θ = 60°take the inverse cosine

6. Calculate tr(AT A) for A = 102−131 without multiplying the matrices.

It is the sum of the squares of the entries.

1 + 0 + 4 + 1 + 9 + 1 = 16

Python Practice

Step 1. Calculate a trace

The instruction np.trace calculates the trace of a square matrix.

Python

The result is 14, because 2 + 3 + 9 = 14. Now change the matrix to the one in Practice Problem 1, with the rows 4, −1, 2 and 0, 3, 7 and 5, 1, −6.

Python

Step 2. Compare the traces of two products

The code below calculates tr(AB) and tr(BA) for two 3 by 3 matrices. The @ sign multiplies two matrices.

Python

Both results are 42. Change any entries of A and B and run the code again. The two results stay equal.

Step 3. Calculate a commutator

In the next box, replace the zeros in the third line so that C is the commutator AB − BA. Use the @ sign for each product and the - sign between the two products.

Python

Step 4. Check the cyclic property

The code below prints the trace of ABC and the trace of ACB for the matrices of Part 3. The two results are different.

Python

The results are 52 and 50. In the next box, change the order of the three matrices in the last line to a different order that gives 52. Do not use the order A, B, C.

Python

Step 5. Find the angle of a rotation

The code below builds the matrix of a rotation by 60 degrees about the z-axis, and then finds the angle again from the trace. The instructions np.cos and np.sin need the angle in radians, so np.radians changes degrees to radians. The instruction np.arccos is the inverse cosine, and np.degrees changes its result back to degrees.

Python

The trace is 2, and the angle is very close to 60 degrees.

Now change the first line so that the rotation is by 135 degrees, and check that the last line finds the same angle.

Python

Quiz

This quiz has 5 questions on this topic. A question asks you either to choose one answer or to type numbers. The page grades your answers and explains each one. A number can be typed as a whole number, a decimal, a fraction such as 1/3, or a calculation such as sqrt(18)/2 or arccos(1/2). The page shows the value of a calculation next to the box. Angles in a calculation are in degrees. The Calculator button under each question opens a calculator. Your answers are saved in your browser, so they are still on the page when you come back to it.

Choose how your answers are checked

1. Find the trace of 5102−34782.

2. Which equation is true for all square matrices A and B of the same size?

3. Two square matrices have tr A = 6 and tr B = −1. Find tr(A − 2B).

4. A and B are square matrices of the same size. Find the trace of the commutator AB − BA.

5. A rotation in three dimensions has the trace 0. Find the angle of the rotation, in degrees. Give the angle between 0° and 180°.

Next: Topic 3.6, LU Factorization