# TensorContract and TensorProduct problem

I am trying the calculate the Kretschmann scalar $$K$$ of a metric $$K=R^{abcd}R_{abcd}$$ where $$R$$ is the Riemann tensor. If I do

TensorContract[TensorProduct[R,R],{{1,5},{2,6},{3,7},{4,8}}]


I get a scalar function, but is this code equivalent to the above formula? Should I raise the indices of the first Riemann tensor with the inverse of the metric, or does Mathematica do this for me? Even if the metric is pseudo-Riemannian and with complicated coefficients?

Mathematica contracts indices assuming flat Euclidean metric. You have to lower/raise the indices yourself.

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

RaiseIndex[T_, g_, i_] :=
Module[{indices = Append[Delete[Range[Length@Dimensions@T], i], i]},
Transpose[
TensorContract[TensorProduct[T, g], {i, Length[indices] + 1}],
indices]
]

Rcontra = Fold[RaiseIndex[#1, g, #2] &, R, {1, 2, 3, 4}];

TensorContract[TensorProduct[R, Rcontra], {{1, 5}, {2, 6}, {3, 7}, {4, 8}}]


Edit: Explanaion of RaiseIndex

Suppose, we want to raise second index $$j$$ in $$R_{ijkl}$$: $$R^{\ j'}_{i\ kl} = R_{ijkl}g^{jj'}$$

However, if we just do R1=TensorContract[R⊗g, {2,5}], then the order of the indices will be $$i,k,l,j'$$. In other words, we need to transpose the tensor to restore the order $$i,j',k,l$$. The order of transposition is calculated in variable indices. It says remove index i from sequence {1,2,...n} and put it to the end. In the case where $$i=2$$ and $$n=4$$: {1,2,3,4} → {1,3,4,2}. Now when we do Transpose[R1, {1,3,4,2}], with R1 having the order of dimensions $$i,k,l,j'$$, we basically say put dimension $$i$$ to the 1st place, put dimension $$k$$ go to the 3rd place, put dimension $$l$$ to the 4th place and put dimension $$j'$$ to the 2nd place. Thus, the order of our dimensions (indices) will be $$i,j',k,l$$.

Since we need to raise all of 4 indices, Fold is just a smart way to write this:

Rcontra = RaiseIndex[RaiseIndex[RaiseIndex[RaiseIndex[R, g, 1], g, 2], g, 3], g, 4];


Please, read help on Fold to get further insights.

• Could you explain the function RaiseIndex? It is a bit too difficult for me. And why do you use Fold? – mattiav27 Nov 18 at 14:17
• Added explanations – Vasily Mitch Nov 18 at 15:00
• In addition, we could probably write this: TensorContract[TensorProduct[R,R,g,g,g,g], {{1,9},{5,10},{2,11},{6,12},{3,13},{7,14},{4,15},{8,16}}]. However, it has $4^8=65k$ of components as intermediate tensor product. So, it is easier to raise indices one by one if possible. – Vasily Mitch Nov 18 at 15:06
• Thanks now I understand. – mattiav27 Nov 18 at 15:17