Skip to content

Instantly share code, notes, and snippets.

@gapolinario
Last active March 14, 2023 14:02
Show Gist options
  • Save gapolinario/b4b4120ad3032c43dddfcae08dd311b8 to your computer and use it in GitHub Desktop.
Save gapolinario/b4b4120ad3032c43dddfcae08dd311b8 to your computer and use it in GitHub Desktop.
Contraction of repeated indices, Mathematica
dim = 3;
SetAttributes[KD, Orderless]
KD[i_, i_] := dim
ContractDeltas[delta_] :=
With[{rule = {KD[x_, y_] f_[w___, x_, z___] -> f[w, y, z],
Power[KD[x_, y_], 2] -> dim}}, Expand[delta] //. rule]
(* A basic example *)
del2 = KD[i, j] KD[i, l] + KD[k, j] KD[i, k] KD[j, f] + KD[i, i] KD[g, f] KD[f, s] + KD[u, v] KD[w, v] KD[w, u];
ContractDeltas[del2] // TraditionalForm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment