Today I noticed something weird concerning implicit sums. Apparently, Mathematica cannot recognize dummy indices as such and simplify accordingly. Consider:

Sum[Subscript[a, n],{n,1,q}]-Sum[Subscript[a, m],{m,1,q}]//FullSimplify

Obviously, the indices m and n are just labels and should not change the result. Yet the FullSimplify command does not yield zero. At some point I thought the program might be stuck because the nature of q is not clear. So I tried:

Sum[Subscript[a, n],{n,1,q},Assumptions->(q\[Element]Integers)]-Sum[Subscript[a, m],{m,1,q},Assumptions->(q\[Element]Integers)]//FullSimplify

This did not improve the situation. It appears to me that Mathematica should be able to recognize a dummy label in sums and simplify accordingly. Is there some function I am missing that facilitates that? Thanks for any help or suggestion!


As a more advanced example involving more than one summation index, consider the following:

Sum[2 Subscript[a,n] Subscript[b,m]-Subscript[a,m] Subscript[b,n],{n, 1, q},{m, 1, q}]//FullSimplify

Interestingly, if the factor of 2 is removed, the simplification occurs appropriately, but with the factor the result appears non-simplified.

  • 2
    $\begingroup$ It is too difficult for Mathematica to combine two sums. There is only one exception: if your expressions are exactly the same (not necessarily Sum) they will subtracted to 0. $\endgroup$ – ybeltukov Oct 14 '14 at 21:39
  • $\begingroup$ Use ReplaceAll: Sum[Subscript[a, n], {n, 1, q}] - Sum[Subscript[a, m], {m, 1, q}] /. m -> n gives 0 $\endgroup$ – Bob Hanlon Oct 14 '14 at 21:41
  • $\begingroup$ Well, making Mathematica simplify it by hand does not provide any advantage to doing it on paper. The reason I ask is because I hope that there is a way to automate this. Especially in more complicated examples with several dummy indices this kind of functionality could be very useful. $\endgroup$ – Kagaratsch Oct 14 '14 at 21:45
  • $\begingroup$ @Kagaratsch I suggest you include one of the more advanced examples in your Question. That increase the likelihood of getting an answer that handles that particular case. $\endgroup$ – Mr.Wizard Oct 14 '14 at 21:49

Recently there's someone who asked me for this, I prepared an answer and now post it here.

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]

You can test them with this:

test = SumHeld[f[a, i], {a, 1, 5}, {i, 1, 5}] + SumHeld[SumHeld[g[b, j], {b, 1, 5}], {j, 1, 5}]
% //IndexUnify
% //SumTogether
| improve this answer | |
  • $\begingroup$ Is this work for more than two summations? $\endgroup$ – Jasmine Jul 27 at 6:45
  • $\begingroup$ @Jasmine It works. But this example is far from enough. For example, expressions like SumTogether[2SumHeld[...]] should be handled. You can change the code to meet your requirements. $\endgroup$ – bcegkmqs23 Aug 4 at 6:39
  • $\begingroup$ But it’s not working well for more than two summations $\endgroup$ – Jasmine Aug 4 at 7:03
  • $\begingroup$ @bcegkmqs23 This is my question. mathematica.stackexchange.com/q/226564/73364 $\endgroup$ – Jasmine Aug 4 at 18:47
  • $\begingroup$ @asd1dsa mathematica.stackexchange.com/q/226564/73364 $\endgroup$ – Jasmine Aug 4 at 18:48

It is too difficult for Mathematica to combine two sums. Even in the following simple example

2 Sum[a[n], {n, 1, q}] - Sum[2 a[n], {n, 1, q}]
(* 2 Sum[a[n], {n, 1, q}] - Sum[2 a[n], {n, 1, q}] *)

There is only one exception: if your expressions are exactly the same (not necessarily Sum) they will subtracted to 0

Sum[a[n], {n, 1, q}] - Sum[a[n], {n, 1, q}]
(* 0 *)

A simple sum simplification for complicated sums with identical iterators

sumSimplify = # /. Times[a___, Sum[expr_, iter__], b___] :> 
      Sum[Times[a, expr, b], iter] /. HoldPattern[p : Plus[Sum[_, iter__] ..]] :> 
         Sum[Simplify@p[[All, 1]], iter] &;

2 Sum[a[n] + b[n], {n, 1, q}] - Sum[2 a[n], {n, 1, q}] // sumSimplify
(* Sum[2 b[n], {n, 1, q}] *)

It works also for identical lists of iterators

2 Sum[a[n, m] + b[n, m], {n, 1, q}, {m, 1, p}] - Sum[2 a[n, m], {n, 1, q}, {m, 1, p}] // 
(* Sum[2 b[n, m], {n, 1, q}, {m, 1, p}] *)

This short example is just a point to start.

| improve this answer | |
  • $\begingroup$ This works well for sums with a single index. Unfortunately, I do not understand most of the syntax you use and cannot improve upon in myself. If you generalize this to work for arbitrarily many dummy indices, it will be exactly the solution wanted! $\endgroup$ – Kagaratsch Oct 14 '14 at 22:04

I can't get it to work with "q", but I can get it to work with the dummy variables m & n.

Sum[f[n], {n, 1, 10}] - Sum[f[m], {m, 1, 10}] // FullSimplify


As you can see, I replaced your subscript with functional notation.

| improve this answer | |
  • 2
    $\begingroup$ If you keep only one of the sums and look at the output, you will notice that this effectively writes the 10 summands explicitly and then subtracts them. This does not address the issue that dummy indices are not recognized as such. $\endgroup$ – Kagaratsch Oct 14 '14 at 21:49
  • $\begingroup$ @Kagaratsch Oh. I guess that's why "q" didn't work. $\endgroup$ – Mitchell Kaplan Oct 15 '14 at 11:05

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.