### Composition and Matrix Multiplication

Exercise The trace `tr`**A** of a square matrix is the sum of
its diagonal entries. For example,

`tr`[ |
1 |
2 |
] = 1 + 4 = 5 |

3 |
4 |

`tr`[ |
0 |
3 |
-4 |
] = 0 + 5 - 3 = 2 |

2 |
5 |
7 |

0 |
4 |
-3 |

Prove the trace has the following properties

`tr`(**A** + **B**) = `tr`**A** + `tr`**B**, `tr c`**A** = `c tr`**A**, `tr`**A**^{T} = `tr`**A**, `tr`**AB** = `tr`**BA**.

Answer The first two properties mean that the trace is linear. The third property means the trace is not changed by the transpose. All are quite obvious.

In the property `tr`**AB** = `tr`**BA**, we do not need **A** and **B** to be square matrices. We will only assume **A** is an `m` by `n` matrix, and **B** is an `n` by `m` matrix.

The `i`-th diagonal entry of `AB` is
∑_{1≤j≤n}`a`_{ij}b_{ji} =
`a`_{i1}`b`_{1i} +
`a`_{i2}`b`_{2i} +
... + `a`_{in}b_{ni}. Thus

`tr`**AB** =
∑_{1≤i≤m}(`a`_{i1}`b`_{1i} +
`a`_{i2}`b`_{2i} +
... + `a`_{in}b_{ni}) =
∑_{1≤i≤m,1≤j≤n}`a`_{ij}b_{ji}.

Exchanging **B** and **A**, we have

`tr`**BA**
= ∑_{1≤j≤n}(`b`_{j1}`a`_{1j} + `b`_{j2}`a`_{2j} + ... + `b`_{jm}a_{mj})
= ∑_{1≤j≤n,1≤i≤m}`b`_{ij}a_{ji}
= ∑_{1≤i≤m,1≤j≤n}`a`_{ji}b_{ij}
= `tr`**AB**.