def rotT(T, g):
    Tprime = T
for i in range(4):
    slices = [None] * 4
slices[i] = slice(None)
slices *= 2
Tprime = g[slices].T * Tprime
return Tprime.sum(-1).sum(-1).sum(-1).sum(-1)
Быстрое вращение тензора с NumPy