Skip to main content
edited tags
Link
xzczd
  • 68.4k
  • 9
  • 174
  • 489
added 372 characters in body
Source Link
Jasmine
  • 1.3k
  • 4
  • 10

https://mathematica.stackexchange.com/a/222410/73364 I have obtained the following code from the above link. But it is not working well with my situation. Can anyone help me out?

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]

Here please see the below test case.

test = SumHeld[f[a, i], {a, 1, 5}, {i, 1, 5}] + 
  SumHeld[SumHeld[2*f[b, j], {b, 1, 5}], {j, 1, 5}] + 
  SumHeld[SumHeld[2*f[c, j], {c, 1, 5}], {j, 1, 5}]
% // IndexUnify
% // SumTogether

The output I am receiving is

$$\text{SumTogether}\left(\sum _{c=1}^5 \sum _{j=1}^5 f(c,j)+2 \sum _{c=1}^5 \sum _{j=1}^5 2 f(c,j)\right)$$

Here the issue is it's not simplifying further as $$\left(\sum _{c=1}^5 \sum _{j=1}^55f(c,j)\right)$$

Another question is when I have more than 2 summation signs as below:

test = SumHeld[f[a, i, j], {a, 1, 5}, {i, 1, 5}, {j, 1, 5}] + 
  SumHeld[SumHeld[2*f[b, j, i], {b, 1, 5}, {j, 1, 5}], {i, 1, 5}] + 
  SumHeld[SumHeld[2*f[c, j, i], {c, 1, 5}, {j, 1, 5}], {i, 1, 5}]
% // IndexUnify
% // SumTogether

Is the bracket I have placed correctly?

https://mathematica.stackexchange.com/a/222410/73364 I have obtained the following code from the above link. But it is not working well with my situation. Can anyone help me out?

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]

Here please see the below test case.

test = SumHeld[f[a, i], {a, 1, 5}, {i, 1, 5}] + 
  SumHeld[SumHeld[2*f[b, j], {b, 1, 5}], {j, 1, 5}] + 
  SumHeld[SumHeld[2*f[c, j], {c, 1, 5}], {j, 1, 5}]
% // IndexUnify
% // SumTogether

The output I am receiving is

$$\text{SumTogether}\left(\sum _{c=1}^5 \sum _{j=1}^5 f(c,j)+2 \sum _{c=1}^5 \sum _{j=1}^5 2 f(c,j)\right)$$

Here the issue is it's not simplifying further as $$\left(\sum _{c=1}^5 \sum _{j=1}^55f(c,j)\right)$$

https://mathematica.stackexchange.com/a/222410/73364 I have obtained the following code from the above link. But it is not working well with my situation. Can anyone help me out?

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]

Here please see the below test case.

test = SumHeld[f[a, i], {a, 1, 5}, {i, 1, 5}] + 
  SumHeld[SumHeld[2*f[b, j], {b, 1, 5}], {j, 1, 5}] + 
  SumHeld[SumHeld[2*f[c, j], {c, 1, 5}], {j, 1, 5}]
% // IndexUnify
% // SumTogether

The output I am receiving is

$$\text{SumTogether}\left(\sum _{c=1}^5 \sum _{j=1}^5 f(c,j)+2 \sum _{c=1}^5 \sum _{j=1}^5 2 f(c,j)\right)$$

Here the issue is it's not simplifying further as $$\left(\sum _{c=1}^5 \sum _{j=1}^55f(c,j)\right)$$

Another question is when I have more than 2 summation signs as below:

test = SumHeld[f[a, i, j], {a, 1, 5}, {i, 1, 5}, {j, 1, 5}] + 
  SumHeld[SumHeld[2*f[b, j, i], {b, 1, 5}, {j, 1, 5}], {i, 1, 5}] + 
  SumHeld[SumHeld[2*f[c, j, i], {c, 1, 5}, {j, 1, 5}], {i, 1, 5}]
% // IndexUnify
% // SumTogether

Is the bracket I have placed correctly?

edited body
Source Link
Jasmine
  • 1.3k
  • 4
  • 10

https://mathematica.stackexchange.com/a/222410/73364 I have obtained the following code from the above link. But it is not working well with my situation. Can anyone help me out?

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]

Here please see the below test case.

test = SumHeld[f[a, i], {a, 1, 5}, {i, 1, 5}] + 
  SumHeld[SumHeld[2*f[b, j]]j], {b, 1, 5}], {j, 1, 5}] + 
  SumHeld[SumHeld[2*f[c, j]]j], {c, 1, 5}], {j, 1, 5}]
% // IndexUnify
% // SumTogether

The output I am receiving is

$$\text{SumTogether}\left(\sum _{c=1}^5 \sum _{j=1}^5 f(c,j)+2 \sum _{c=1}^5 \sum _{j=1}^5 2 f(c,j)\right)$$

Here the issue is it's not simplifying further as $$\left(\sum _{c=1}^5 \sum _{j=1}^53f(c,j)\right)$$$$\left(\sum _{c=1}^5 \sum _{j=1}^55f(c,j)\right)$$

https://mathematica.stackexchange.com/a/222410/73364 I have obtained the following code from the above link. But it is not working well with my situation. Can anyone help me out?

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]

Here please see the below test case.

test = SumHeld[f[a, i], {a, 1, 5}, {i, 1, 5}] + 
  SumHeld[SumHeld[2*f[b, j]], {b, 1, 5}, {j, 1, 5}] + 
  SumHeld[SumHeld[2*f[c, j]], {c, 1, 5}, {j, 1, 5}]
% // IndexUnify
% // SumTogether

The output I am receiving is

$$\text{SumTogether}\left(\sum _{c=1}^5 \sum _{j=1}^5 f(c,j)+2 \sum _{c=1}^5 \sum _{j=1}^5 2 f(c,j)\right)$$

Here the issue is it's not simplifying further as $$\left(\sum _{c=1}^5 \sum _{j=1}^53f(c,j)\right)$$

https://mathematica.stackexchange.com/a/222410/73364 I have obtained the following code from the above link. But it is not working well with my situation. Can anyone help me out?

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]

Here please see the below test case.

test = SumHeld[f[a, i], {a, 1, 5}, {i, 1, 5}] + 
  SumHeld[SumHeld[2*f[b, j], {b, 1, 5}], {j, 1, 5}] + 
  SumHeld[SumHeld[2*f[c, j], {c, 1, 5}], {j, 1, 5}]
% // IndexUnify
% // SumTogether

The output I am receiving is

$$\text{SumTogether}\left(\sum _{c=1}^5 \sum _{j=1}^5 f(c,j)+2 \sum _{c=1}^5 \sum _{j=1}^5 2 f(c,j)\right)$$

Here the issue is it's not simplifying further as $$\left(\sum _{c=1}^5 \sum _{j=1}^55f(c,j)\right)$$

deleted 512 characters in body
Source Link
Jasmine
  • 1.3k
  • 4
  • 10
Loading
added 167 characters in body
Source Link
Jasmine
  • 1.3k
  • 4
  • 10
Loading
Source Link
Jasmine
  • 1.3k
  • 4
  • 10
Loading