8

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!

EDIT:

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.

Kagaratsch
  • 11,955
  • 4
  • 25
  • 72
  • 2
    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. – ybeltukov Oct 14 '14 at 21:39
  • Use ReplaceAll: Sum[Subscript[a, n], {n, 1, q}] - Sum[Subscript[a, m], {m, 1, q}] /. m -> n gives 0 – Bob Hanlon Oct 14 '14 at 21:41
  • 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. – Kagaratsch Oct 14 '14 at 21:45
  • @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. – Mr.Wizard Oct 14 '14 at 21:49

3 Answers3

5

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
asd1dsa
  • 573
  • 4
  • 8
3

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}] // 
   sumSimplify
(* Sum[2 b[n, m], {n, 1, q}, {m, 1, p}] *)

This short example is just a point to start.

ybeltukov
  • 43,673
  • 5
  • 108
  • 212
  • 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! – Kagaratsch Oct 14 '14 at 22:04
-2

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

0

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

Mitchell Kaplan
  • 3,696
  • 22
  • 34
  • 2
    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. – Kagaratsch Oct 14 '14 at 21:49
  • @Kagaratsch Oh. I guess that's why "q" didn't work. – Mitchell Kaplan Oct 15 '14 at 11:05