# Automatic summing of dummy indices [closed]

I have: $$test1=t_{1,2} \delta _{1,a} x_{2,b} \left(-\frac{\partial L}{\partial x_{a,b}}\right)-t_{1,2} x_{1,a} \delta _{2,b} \frac{\partial L}{\partial x_{a,b}}$$

$$test2=t_{1,2} x_{2,a} \delta _{1,b} \frac{\partial L}{\partial x_{a,b}}+t_{1,2} \delta _{2,a} x_{1,b} \frac{\partial L}{\partial x_{a,b}}$$

I have to calculate: $$test = test2 + test3$$

$$test=2 t_{1,2} x_{2,a} \delta _{1,b} \frac{\partial L}{\partial x_{a,b}}+2 t_{1,2} \delta _{2,a} x_{1,b} \frac{\partial L}{\partial x_{a,b}}$$

For that I am using following code right now,

 ClearAll[\[Delta]]

Format[\[Delta][a_, b_]] := Subscript[\[Delta], a, b]

\[Delta][\[Beta], a_] := \[Delta][a, \[Beta]]

\[Delta][\[Alpha], a_] := \[Delta][a, \[Alpha]]

\[Delta][1, 1] = 1;

\[Delta][2, 2] = 1;

\[Delta][3, 3] = 1;

\[Delta][4, 4] = 1;

\[Delta][1, 2] = 0; \[Delta][2, 1] = 0;
\[Delta][1, 3] = 0; \[Delta][3, 1] = 0;
\[Delta][1, 4] = 0; \[Delta][4, 1] = 0;
\[Delta][2, 3] = 0; \[Delta][3, 2] = 0;
\[Delta][2, 4] = 0; \[Delta][4, 2] = 0;
\[Delta][3, 4] = 0; \[Delta][4, 3] = 0;

Format[t[a_, b_]] := Subscript[t, a, b]
$Assumptions = t \[Element] Matrices[{4, 4}, Reals, Antisymmetric[{1, 2}]]; t[arg__] /; SameQ[arg] := 0 t[arg__] /; ! OrderedQ@{arg} := Signature@{arg} t @@ Sort@{arg} Format[x[a_, b_]] := Subscript[x, a, b]$Assumptions = x \[Element] Matrices[{4, 4}, Reals, Antisymmetric[{1, 2}]];
x[arg__] /; SameQ[arg] := 0
x[arg__] /; ! OrderedQ@{arg} := Signature@{arg} x @@ Sort@{arg}

test1 = -t[1, 2] myD[L, x[a, b]]*x[2, b]*\[Delta][1, a] -
t[1, 2] myD[L, x[a, b]]*x[1, a]*\[Delta][2, b]

test2 = t[1, 2] myD[L, x[a, b]]*x[2, a]*\[Delta][1, b] +
t[1, 2] myD[L, x[a, b]]*x[1, b]*\[Delta][2, a]

test3 = test1 /. {a -> b, b -> a}

test = test2 + test3

Can I use an easier way such that mathematica detect it automatically without replacing indices:


I have a code as follows:

    dummyunify[expr_Plus,
possibledummylst_] := (term |->
Module[{indexlstold, indexlstnew,
varlst = DeleteCases[List @@ term, _?AtomQ]},
indexlstold =
DeleteDuplicates@
Cases[SortBy[Last]@varlst,
Alternatives @@ possibledummylst, \[Infinity]];
indexlstnew = possibledummylst[[;; Length@indexlstold]];
term /. Thread[indexlstold -> indexlstnew]]) /@ expr


But this is not summing automatically! dummyunify[test1 + test2, {a, b}]

Can anyone help?

• Are you sure your example is right? Is $test=2 t_{1,2} x_{2,a} \delta _{1,b} \frac{\partial L}{\partial x_{a,b}}+2 t_{1,2} \delta _{2,a} x_{1,b} \frac{\partial L}{\partial x_{a,b}}$ really what you want? Then $test=2 t_{1,2}\frac{\partial L}{\partial x_{a,b}}(x_{2,a} \delta _{1,b} +\delta _{2,a} x_{1,b})$ which doesn't match what you've given in your code. Is it safe to assume, based on what you've written, that $\delta$ is the Kronecker delta, and $x$ and $t$ are related to the Levi-Civita symbol? Please give more information and a cleaner explanation of what you want to do. Jul 22 '21 at 22:51

# Clean-up

We can make things a lot cleaner with a few tweaks. Firstly, you're defining $$\delta$$ by hand, when it's clearly just the KroneckerDelta.

Next, in your definitions of t and x, Signature already handles whether the arguments are equal and/or ordered. So you can delete these. Furthermore, I believe these definitions contain an error, namely that it sorts the arguments while they still contain the dummy variables. I.e. t[b,a] will become -t[a,b]. We can avoid this by requiring the arguments are numeric before applying the definitions. Better yet, we can do everything first and enforce the symmetry at the end.

We can also delete the assumptions since you're explicitly giving these properties to $$x$$ and $$t$$ and furthermore not using any tensor operations. I'll lastly add a formatting line for myD. Putting this altogether we can replace your entire top block of code with

δ = KroneckerDelta[#1, #2] &;
(*t[arg__?NumericQ] /; ! OrderedQ@{arg} := Signature@{arg} t @@ Sort@{arg}
x[arg__?NumericQ] /; ! OrderedQ@{arg} := Signature@{arg} x @@ Sort@{arg}*)
Format[t[a_, b_]] := Subscript[t, a, b]
Format[x[a_, b_]] := Subscript[x, a, b]
Format[myD[a_, b_]] := "\[PartialD]" <> ToString[a, TraditionalForm]/

test1 = -t[1, 2] myD[L, x[a, b]]*x[2, b]*δ[1, a] - t[1, 2] myD[L, x[a, b]]*x[1, a]*δ[2, b]
test2 = t[1, 2] myD[L, x[a, b]]*x[2, a]*δ[1, b] + t[1, 2] myD[L, x[a, b]]*x[1, b]*δ[2, a]
test3 = test1 /. {a -> b, b -> a}
test = test2 + test3


which gives

# Summation

If I understand correctly, you want a function that takes in expressions like above and returns an expression which has been summed over all variables appearing in it. For this example, I am going to assume that the dummy variables always appear as an argument to x or t. It's not terribly hard to generalize if need be. I'm also going to assume that myD[a,b]=-myD[a,-b] and that the user should specify a global max index for the variables.

We begin by getting a list of the variables to be summed.

getVars[exp_] :=
Union@Flatten@Cases[exp, ten_[a_?(And[AtomQ[#], ! NumericQ[#]] &),
b_?(And[AtomQ[#], ! NumericQ[#]] &)] :> {a, b}, ∞]


We then make a function that enforces the anti-symmetry of $$x$$ and $$t$$, and myD while we're at it

sort[exp_] := exp /. (p : x | t)[a__?NumericQ] :> (Signature[{a}] p @@ Sort[{a}])
myD[a_, -b_] := -myD[a, b]


We finally define our auto sum function

sum[exp_, max_, min_ : 1] := Block[{vars},
vars = getVars[exp];
sort[Sum @@ Prepend[Evaluate@Table[{v, min, max}, {v, vars}], exp]]
]


which gives

in which you can see the dummy variables have been summed over, as requested.