https://mathematica.stackexchange.com/a/222410/73364 I have obtained the following code from the above link
SumHeld /: MakeBoxes[SumHeld[expr_, ranges__], form_] :=
MakeBoxes[Sum[expr, ranges], form]
SumHeld /:
SyntaxInformation[
SumHeld] = {"LocalVariables" -> {"Table", {2, Infinity}}};
IndexUnify[HoldPattern@Plus[sums : SumHeld[_, __] ..]] :=
Plus @@ With[{targetIndices = List @@ #[[-1, 2 ;;, 1]],
sourceIndicesList = List @@@ #[[;; , 2 ;;, 1]]},
Function[{sum, sourceIndices},
sum /. Thread[
sourceIndices ->
Take[targetIndices, Length@sourceIndices]]] @@@
Transpose@{#, sourceIndicesList}] &@
SortBy[Flatten /@ {sums}, Length]
SumTogether[HoldPattern@Plus[sums : SumHeld[_, sameRanges__] ..]] :=
SumHeld[Plus @@ {sums}[[;; , 1]], sameRanges]
SumTogether[HoldPattern@Plus[sums : SumHeld[_, __] ..]] /;
UnsameQ @@ {sums}[[;; , 2 ;;]] :=
Plus @@ SumTogether@*Plus @@@ GatherBy[{sums}, Rest]
It's working perfectly with another suggestion from @xzczd
SumHeld /: c_?NumericQ SumHeld[rest_, range__] := SumHeld[c rest, range]
If I need to do a summation for example:
$$\text{Test1}=x_{a,c} x_{b,d} K_{a,b,c,d}+x_{a,b} x_{c,d} K_{a,b,c,d}$$
ie,
Test1 = Subscript[K, a, b, c, d]* Subscript[x, a, b]*
Subscript[x, c, d] +
Subscript[K, a, b, c, d]* Subscript[x, b, d]*Subscript[x, a, c]
It works perfectly, but the drawback is I need to write each term. ie, If I need the answer I have to write the code as:
SumHeld[Test1[[1]], {a, 1, 5}, {b, 1, 5}, {c, 1, 5}, {d, 1, 5}] +
SumHeld[SumHeld[Test1[[2]]], {a, 1, 5}, {b, 1, 5}, {c, 1, 5}, {d, 1,
5}]
% // IndexUnify
% // SumTogether // FullSimplify
This is not possible if I have 100 terms!!! So is there any possibility to get the answer without writing term by term summation?
I can show an example: $$\text{p5}=4 M x_{i,q} x_{k,l} g_{i,k,l,p}-4 M x_{i,p} x_{k,l} g_{i,k,l,q}-2 x_{i,q} x_{k,l} g_{i,k,l,p}+2 x_{i,p} x_{k,l} g_{i,k,l,q}-4 M x_{j,p} x_{k,l} g_{j,k,l,q}+4 M x_{j,q} x_{k,l} g_{j,k,l,p}-2 x_{j,q} x_{k,l} g_{j,k,l,p}+2 x_{j,p} x_{k,l} g_{j,k,l,q}$$
Both x and g are antisymmetric tensors. So here to perform the summation I have to write as:
p5=2 Subscript[g, i, k, l, q] Subscript[x, i, p] Subscript[x, k, l] -
4 M Subscript[g, i, k, l, q] Subscript[x, i, p] Subscript[x, k, l] -
2 Subscript[g, i, k, l, p] Subscript[x, i, q] Subscript[x, k, l] +
4 M Subscript[g, i, k, l, p] Subscript[x, i, q] Subscript[x, k, l] +
2 Subscript[g, j, k, l, q] Subscript[x, j, p] Subscript[x, k, l] -
4 M Subscript[g, j, k, l, q] Subscript[x, j, p] Subscript[x, k, l] -
2 Subscript[g, j, k, l, p] Subscript[x, j, q] Subscript[x, k, l] +
4 M Subscript[g, j, k, l, p] Subscript[x, j, q] Subscript[x, k, l]
SumHeld[p5[[1]], {i, 1, 5}, {k, 1, 5}, {l, 1, 5}] +
SumHeld[SumHeld[p5[[2]]], {i, 1, 5}, {k, 1, 5}, {l, 1, 5}] +
SumHeld[SumHeld[p5[[3]]], {i, 1, 5}, {k, 1, 5}, {l, 1, 5}] +
SumHeld[SumHeld[p5[[4]]], {i, 1, 5}, {k, 1, 5}, {l, 1, 5}] +
SumHeld[SumHeld[p5[[5]]], {j, 1, 5}, {k, 1, 5}, {l, 1, 5}] +
SumHeld[SumHeld[p5[[6]]], {j, 1, 5}, {k, 1, 5}, {l, 1, 5}] +
SumHeld[SumHeld[p5[[7]]], {j, 1, 5}, {k, 1, 5}, {l, 1, 5}] +
SumHeld[SumHeld[p5[[8]]], {j, 1, 5}, {k, 1, 5}, {l, 1, 5}];
% // IndexUnify;
p5n = % // SumTogether
It's really hard to write it as above for only 8 terms. Then think about 50 or 100 terms!
What I got after the code is: $$8 M x_{i,j} x_{l,q} g_{i,j,l,p}-8 M x_{i,j} x_{l,p} g_{i,j,l,q}-4 x_{i,j} x_{l,q} g_{i,j,l,p}+4 x_{i,j} x_{l,p} g_{i,j,l,q}$$
Is there a way to get the same answer in a different manner without writing each terms into the sumheld?

Import[ ]it. – Nicholas G Apr 28 '21 at 02:12Map? – xzczd Apr 28 '21 at 04:19SumHeld[SumHeld[…typos? – xzczd Apr 29 '21 at 02:17SumHeld[ p5[[2]] ]are correct? – xzczd Apr 29 '21 at 02:26SumHeld[…, and the rest terms should beSumHeld[SumHeld[…? – xzczd Apr 29 '21 at 02:32