Trouble proving $\mbox{tr} (AB) = \mbox{tr} (BA)$ for $n \times n$ 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:
$$ \sum_{i=1}^{n} (AB)_{ii} = \sum_{i=1}^{n} \sum_{k=1}^{n} A_{nk}B_{kn} = \sum_{k=1}^{n} \sum_{i=1}^{n} B_{kn}A_{nk} = \sum_{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:
$$ \mbox{tr}(AB) = \sum_{i=1}^{n} (AB)_{ii} = \sum_{i=1}^{n} \sum_{k=1}^{n} A_{nk}B_{kn} $$
$$ \mbox{tr}(BA) = \sum_{i=1}^{n} (BA)_{ii} = \sum_{i=1}^{n} \sum_{k=1}^{n} B_{nk}A_{kn} $$
I would greatly appreciate it if someone could perhaps point out where my reasoning went wrong.
 A: 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.$$
The products in the sum are products of scalars, thus commutative, and therefore the two double sums are equal.
If you really want to be rigorous about this, you can probably show it by induction.
A: Of course one can prove this directly as above.
Another proof is to assume first that $A$, and $B$ are invertable (which is fine since in general case one can approximate any matrix by an invertable one) and check that $det(AB-\lambda *I) = det(BA-\lambda*I)$. In words, that characteristic polynomials of $AB$ and $BA$ are the same (this is an interesting fact on its own).
Since trace is the second coefficient of characteristic polynomial we get what we want. Now, $det(AB-\lambda*I) = det(AB-\lambda*B^{-1}B) = det(A-\lambda* B^{-1})*det(B) = det(B^{-1}BA-\lambda*B^{-1})*det(B) = det(B^{-1})det(BA-\lambda*I)*det(B) = det(BA-\lambda*I)$
