# Trouble proving tr(AB)=tr(BA) for nxn matrices

This might be a really obvious question so I apologize in advance, but I'm having trouble seeing when matrices are commutative for general nxn matrices. For example, when proving tr(AB)=tr(BA), I can easily prove this in a 2x2 matrix but I'm getting confused for proving it in a nxn matrix.

I've searched online and the recurring solution that comes up is:

$$\Sigma_{i=1}^{n} (AB)_{ii}$$ $$= \Sigma_{i=1}^{n} \Sigma_{k=1}^{n} A_{nk}B_{kn}$$ $$= \Sigma_{k=1}^{n} \Sigma_{i=1}^{n} B_{kn}A_{nk}$$ $$= \Sigma_{i=1}^{n} (BA)_{ii}$$

How come we're able to switch the sums in line 3? I originally tried solving the question by trying to do TR(AB) and TR(BA) separately:

$$\mathrm{tr}(AB) = \Sigma_{i=1}^{n} (AB)_{ii} = \Sigma_{i=1}^{n} \Sigma_{k=1}^{n} A_{nk}B_{kn}$$

$$\mathrm{tr}(BA) = \Sigma_{i=1}^{n} (BA)_{ii} = \Sigma_{i=1}^{n} \Sigma_{k=1}^{n} B_{nk}A_{kn}$$

I would greatly appreciate it if someone could perhaps point out where my reasoning went wrong.

• You can change the order of the sums, because you're summing a finite number of real numbers. – Larara Mar 8 '16 at 2:28
• The second line is already wrong, by the way. I would suggest you try this with $3 \times 3$ matrices and write the corresponding sums out in full, comparing them with your summations. This is a kind of sanity check for what you're writing. – David Mar 8 '16 at 2:29

Basically, you have $$(A_{11}B_{11}+A_{12}B_{21}+\ldots)+(A_{21}B_{12}+A_{22}B_{22}+\ldots)+\ldots$$ and by simply regrouping the terms you get $$(A_{11}B_{11}+A_{21}B_{12}+\ldots)+(A_{12}B_{21}+A_{22}B_{22}+\ldots)+\ldots.$$