# Tensor product and contraction keeping track of indexes order

I have two tensors $\eta_{\mu\nu}$ and $M_{\mu\nu\rho}$ which are defined as following

η = DiagonalMatrix[{-1,1,1,1}]
M = Array[Subscript[mSymb, ##] &, {4, 4,4}]


I want to consider the tensor product and then contract the indexes in pairs. I want to get the following object

$$\overline{M}_{\sigma\,\,\,\,\,\alpha}^{\,\,\,\,\mu} = \sum_\nu \eta^{\mu\nu} M_{\sigma \nu\alpha}$$

I tried with the following function

TensorContract[TensorProduct[η,M],{2,4}]


But this function instead returns the following object

$$\tilde{M}^{\mu}_{\,\,\sigma\alpha} = \sum_\nu \eta^{\mu\nu} M_{\sigma \nu\alpha}$$

You see that the RHS is the same in both cases but the order of the indexes is important when calling the element M[[σ, μ, α]]. For example, If M was a square matrix, $\tilde{M}$ would be the transpose of the object that I call $\overline{M}$.

How can I tell Mathematica to keep track of the order??

Thanks.

Mathematica is keeping track of the order, you just gave it a different order than you intended. TensorProduct[eta,m] will put the the two slots of eta first, then the three slots m. If you want to think of the \[Sigma] index as coming first, you need to move it there. Then the two \[Nu] indices will be slots 3 and 4, and you have

TensorContract[
TensorTranspose[
TensorProduct[eta, M],
Cycles[{{1, 3}}]
],
{3, 4}
]


Altenatively, you could fix up your first attempt after the fact, again using TensorTranspose:

TensorTranspose[TensorContract[TensorProduct[eta, M], {2, 4}], Cycles[{{1, 2}}]]


Perhaps you can create a function to do this:

raise[m_, slot_, η_] := TensorContract[
TensorTranspose[
TensorProduct[η,m],
RotateRight[Range[slot+1],2]
],
{{slot+1,slot+2}}
]


η = DiagonalMatrix[{-1,1,1,1}];

$\left( \begin{array}{cccc} \left( \begin{array}{c} -\operatorname{mySymb}_{1,1,1} \\ -\operatorname{mySymb}_{1,1,2} \\ -\operatorname{mySymb}_{1,1,3} \\ -\operatorname{mySymb}_{1,1,4} \\ \end{array} \right) & \left( \begin{array}{c} \operatorname{mySymb}_{1,2,1} \\ \operatorname{mySymb}_{1,2,2} \\ \operatorname{mySymb}_{1,2,3} \\ \operatorname{mySymb}_{1,2,4} \\ \end{array} \right) & \left( \begin{array}{c} \operatorname{mySymb}_{1,3,1} \\ \operatorname{mySymb}_{1,3,2} \\ \operatorname{mySymb}_{1,3,3} \\ \operatorname{mySymb}_{1,3,4} \\ \end{array} \right) & \left( \begin{array}{c} \operatorname{mySymb}_{1,4,1} \\ \operatorname{mySymb}_{1,4,2} \\ \operatorname{mySymb}_{1,4,3} \\ \operatorname{mySymb}_{1,4,4} \\ \end{array} \right) \\ \left( \begin{array}{c} -\operatorname{mySymb}_{2,1,1} \\ -\operatorname{mySymb}_{2,1,2} \\ -\operatorname{mySymb}_{2,1,3} \\ -\operatorname{mySymb}_{2,1,4} \\ \end{array} \right) & \left( \begin{array}{c} \operatorname{mySymb}_{2,2,1} \\ \operatorname{mySymb}_{2,2,2} \\ \operatorname{mySymb}_{2,2,3} \\ \operatorname{mySymb}_{2,2,4} \\ \end{array} \right) & \left( \begin{array}{c} \operatorname{mySymb}_{2,3,1} \\ \operatorname{mySymb}_{2,3,2} \\ \operatorname{mySymb}_{2,3,3} \\ \operatorname{mySymb}_{2,3,4} \\ \end{array} \right) & \left( \begin{array}{c} \operatorname{mySymb}_{2,4,1} \\ \operatorname{mySymb}_{2,4,2} \\ \operatorname{mySymb}_{2,4,3} \\ \operatorname{mySymb}_{2,4,4} \\ \end{array} \right) \\ \left( \begin{array}{c} -\operatorname{mySymb}_{3,1,1} \\ -\operatorname{mySymb}_{3,1,2} \\ -\operatorname{mySymb}_{3,1,3} \\ -\operatorname{mySymb}_{3,1,4} \\ \end{array} \right) & \left( \begin{array}{c} \operatorname{mySymb}_{3,2,1} \\ \operatorname{mySymb}_{3,2,2} \\ \operatorname{mySymb}_{3,2,3} \\ \operatorname{mySymb}_{3,2,4} \\ \end{array} \right) & \left( \begin{array}{c} \operatorname{mySymb}_{3,3,1} \\ \operatorname{mySymb}_{3,3,2} \\ \operatorname{mySymb}_{3,3,3} \\ \operatorname{mySymb}_{3,3,4} \\ \end{array} \right) & \left( \begin{array}{c} \operatorname{mySymb}_{3,4,1} \\ \operatorname{mySymb}_{3,4,2} \\ \operatorname{mySymb}_{3,4,3} \\ \operatorname{mySymb}_{3,4,4} \\ \end{array} \right) \\ \left( \begin{array}{c} -\operatorname{mySymb}_{4,1,1} \\ -\operatorname{mySymb}_{4,1,2} \\ -\operatorname{mySymb}_{4,1,3} \\ -\operatorname{mySymb}_{4,1,4} \\ \end{array} \right) & \left( \begin{array}{c} \operatorname{mySymb}_{4,2,1} \\ \operatorname{mySymb}_{4,2,2} \\ \operatorname{mySymb}_{4,2,3} \\ \operatorname{mySymb}_{4,2,4} \\ \end{array} \right) & \left( \begin{array}{c} \operatorname{mySymb}_{4,3,1} \\ \operatorname{mySymb}_{4,3,2} \\ \operatorname{mySymb}_{4,3,3} \\ \operatorname{mySymb}_{4,3,4} \\ \end{array} \right) & \left( \begin{array}{c} \operatorname{mySymb}_{4,4,1} \\ \operatorname{mySymb}_{4,4,2} \\ \operatorname{mySymb}_{4,4,3} \\ \operatorname{mySymb}_{4,4,4} \\ \end{array} \right) \\ \end{array} \right)$