1
$\begingroup$

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?

$\endgroup$
12
  • $\begingroup$ If your hundreds of data are in a file, Import[ ] it. $\endgroup$
    – Nicholas G
    Apr 28 at 2:12
  • $\begingroup$ It's not in a file. There are 100 terms in a sum. $\endgroup$
    – Jasmine
    Apr 28 at 4:11
  • $\begingroup$ Are you looking for Map? $\endgroup$
    – xzczd
    Apr 28 at 4:19
  • $\begingroup$ I am not sure how to modify above code to get summation without adding seperate terms. For Eg: It might be great if I get an answer just by writing, SumHeld[Test1] $\endgroup$
    – Jasmine
    Apr 28 at 13:40
  • $\begingroup$ It seems that I am missing something in the question. You have 100 terms. The terms are truly different rather than merely a variable with a changing counter subscript. The question is not about importing outside data. Then it sounds like you need to input each of the hundred different items to be summed, no? $\endgroup$
    – Nicholas G
    Apr 28 at 20:15
1
$\begingroup$
SumHeld[expr_, {lst_List, dim_}] := (term |-> SumHeld[term, ##] & @@ 
    Table[If[Count[term, patt, ∞] == 2, {patt, dim}, Nothing], {patt, lst}]) /@ expr    

SumHeld[p5, {{i, j, k, l}, 5}]

enter image description here

If you prefer the convention $$\sum _{i=1}^5$$

Modify the {patt, dim} to {patt, 1, dim}.

$\endgroup$
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.