matrix trace
- \(tr(XY) = tr(YX)\)
- Why? Let \(X\) and \(Y\) be \(n\times n\) matrices. Then, the trace \(tr(XY)\) can be computed as:
trace = 0 for i in range(n): #i selects row of X for j in range(n): #i selects column of Y trace += X[i][j]*Y[j][i]
The for loops can be re-ordered to compute \(tr(YX)\).
- Then, taking \(X=AB\) and \(Y=C\) we get \(tr(ABC)=tr(CBA)\)