# How can I get simplified answer for SUM?

I need to get the simplified answer after adding few SUM. In my code below, I know we have to define k ahead, so it will be unified and recognized for both, but I need it to be somehow symbolic, I mean we don't have to initialize it a constant integer. Please look at my code:

Sum[ar[j], { j, 1, k}] + Sum[- ar[j], { j, 2, k}]


What I expect to see is ar[1] because all items in second SUM is negative and will be simplified with first SUM items. But Mathematica just shows the same expressions. Is that possible?

EDITED
Thanks for the good answers, but I tried a similar code for a new expression I need. It does not work; please take a look:

Sum[ar[j], { j, 1, k}] + Sum[- ar[j], { j, 1, k + 1}]
sumRule2 = ((Sum[expr1_, {var_, min1_, max1_}] + Sum[expr2_, {var_, min1_, max2_}]) max1<=max2) :>  Sum[expr1 + expr2, {var, min1, max1}] +  Sum[expr2, {var, max1 + 1, max2}];

• I believe you will find that Mathematica does not automatically take Sums apart. I am guessing that is because of the many strange things that can happen when k turns out to be infinite and/or some of ar[j] turn out to be infinite or undefined or ... Think of the most malicious sums, things REALLY evil, and imagine what those borderline cases might do to a relatively mindless piece of software trying to follow a vast list of generic rules
– Bill
Sep 21, 2022 at 16:30
• Not a general solution but for the example you gave you could use Sum[ar[j], { j, 1, k}] + Sum[- ar[j], { j, 2, k}] /. Sum[-a_, b_] :> - Sum[a, b] /. Sum[a_, {b1_, b2_, b3_}] :> Sum[a, {b1, 1, b3}] - Sum[a, {b1, 1, b2 - 1}]. That replacement rule uses Sum[ar[j], {j, 1, 0}]=0 in the first sum of your example. Sep 21, 2022 at 17:09
• If you give k a definite values, you get ar[1] Sep 21, 2022 at 17:17
• @DanielHuber thanks. you are right but I need something like generic, symbolic one. Sep 21, 2022 at 17:18

\$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global*"]


You would need to define a replacement rule for each variation. In your example, the iterators have different initial values but the same end value.

EDIT 2: If there are additional factors the rules needs to be modified as below and if there are more than two sums, ReplaceRepeated is needed in place of ReplaceAll

sumRule1 = ((c1_. *Sum[expr1_, {var_, min1_, max_}] +
c2_. Sum[expr2_, {var_, min2_, max_}]) /; min1 <= min2) :>
c1*Sum[expr1, {var, min1, min2 - 1}] +
Sum[c1*expr1 + c2*expr2, {var, min2, max}];

Sum[ar[j], {j, 1, k}] + Sum[-ar[j], {j, 2, k}] /. sumRule1

(* ar[1] *)

Sum[ar[j], {j, 1, k}] + Sum[-ar[j], {j, 1, k}] /. sumRule1

(* 0 *)

Sum[ar[j], {j, 3, k}] + Sum[-ar[j], {j, 2, k}] /. sumRule1

(* -ar[2] *)

Sum[2*ar[j] + br[j], {j, 4, h}] - 2*Sum[ar[j], {j, 3, h}] +
Sum[-br[j], {j, 5, h}] //. sumRule1

(* 2 ar[4] - 2 (ar[3] + ar[4]) + br[4] *)


EDIT 1: For your second example, you left off the condition operator /; and since the upper bounds are not numbers, the condition must look at their difference.

sumRule2 = ((c1_. *Sum[expr1_, {var_, min1_, max1_}] +
c2_. *Sum[expr2_, {var_, min1_, max2_}]) /;
max2 - max1 >= 0) :>
Sum[c1*expr1 + c2*expr2, {var, min1, max1}] +
c2*Sum[expr2, {var, max1 + 1, max2}];

Sum[ar[j], {j, 1, k}] + Sum[-ar[j], {j, 1, k + 1}] /. sumRule2

(* -ar[1 + k] *)

Sum[ar[j], {j, 1, k + 3}] + Sum[-ar[j], {j, 1, k + 1}] /. sumRule2

(* ar[2 + k] + ar[3 + k] *)

• Are there some relevant documents i to read and leran more. I mean that was just an simple example, but I have more of these type of operations. Thanks that works. Sep 21, 2022 at 18:03
• Could you see my edited, thanks Sep 21, 2022 at 18:45
• I could not get sumRule1 to simplify Sum[2*ar[j] + br[j], {j, 4, h}] - 2*Sum[ar[j], {j, 3, h}] + Sum[-br[j], {j, 5, h}]  Sep 21, 2022 at 19:22
• I think one of the reasons that the above example did not work is that sumRule1 does not seem to work with Sum[ar[j], {j, 4, h}] - Sum[ar[j], {j, 5, h}]  although sumRule1 simplifies when the minus is inside of the sum as in Sum[ar[j], {j, 4, h}] + Sum[-ar[j], {j, 5, h}] Sep 21, 2022 at 19:33
• I am sorry, It seems I made a mistake as I wanted to just remove my upvote until the case Sum[ar[j], {j, 4, h}] - Sum[ar[j], {j, 5, h}] worked but it seems that doing that I also removed a point as the score went from 6 to 4. The system stops me from upvoting now untill there is an edit but I will upvote if I see an edit . Sorry about that. Sep 21, 2022 at 19:52

As mentioned in the comments, there might be a reason why this is not already implemented. In particular, when the sums are divergent this might be risky.

However, if the sums are finite the following rules should be fine (I used [FormalK] to make it unlikely that the user would use that specific variable as an upper bound in the sum or that it would appear in the expression to be summed) :

sumrule = {Sum[a_ + b_, c_] :> Sum[a, c] + Sum[b, c],
Sum[a_*b_, {c1_, c2_, c3_}] /; FreeQ[b, c1] :>
b*Sum[a, {c1, c2, c3}],
Sum[a_, {b1_, b2_, b3_}] :>
Sum[ReplaceAll[a, b1 -> \[FormalK]], {\[FormalK], 1, b3}] -
Sum[ReplaceAll[a, b1 -> \[FormalK]], {\[FormalK], 1, b2 - 1}]};


Using [FormalK] as a "canonical" iterator has the advantage that the the dummy indices do not need to be the same in each sum for the simplification to work.

Examples:

Sum[ar[j], {j, 1, k}] + Sum[-ar[j], {j, 2, k}] //. sumrule


Output: (* ar[1] *)

Sum[2*ar[j] + br[j], {j, 4, h}] - 2*Sum[ar[j], {j, 3, h}] +
Sum[-br[p], {p, 5, h}] //. sumrule // Simplify


Output: (* -2 ar[3] + br[4] *)

Edit:

In an attempt to fit the case with a symbolic upper bound I ended up making the code above quite longer. Maybe not the best method but I will leave it here in case it is of use.

First, we may consider an auxiliary function that scans the expression for the largest upper bound and the smallest lower bound. The function below will probably work even when there is no clear upper bound as it uses Sort which can sort symbols based on there names. I should add a bunch off error catching scenarios but instead I will trust that the user only uses the function below when there is a clear maximal upper bound or lower bound.

scanbounds[expr_] :=
Module[{ilist, a, b, maxupper,
minlower, upperlist, lowerlist,
upperlistsymbols, lowerlistsymbols},

ilist = Cases[expr, Sum[a_, b_] -> b, Infinity];

upperlist = Last /@ ilist;
lowerlist = (#[[2]] &) /@ ilist;

(* add different error possibilities here *)

maxupper = Last@Sort[upperlist];
minlower = First@Sort[lowerlist];

{minlower, maxupper}]


The simplifying function (I recall that I used [FormalK] to make it unlikely that the user would use that specific variable as an upper bound in the sum or that it would appear in the expression to be summed):

simpsum[s_, maxtimes_ : 1000] :=
Module[{lower, upper, sumrule, a, b, c, c1, c2, c3, b1, b3, b2},
{lower, upper} = scanbounds[s];

sumrule = {Sum[a_ + b_, c_] :> Sum[a, c] + Sum[b, c],
Sum[a_*b_, {c1_, c2_, c3_}] /; FreeQ[b, c1] :>
b*Sum[a, {c1, c2, c3}],
Sum[a_, {b1_, b2_, b3_}] :>
Sum[ReplaceAll[a, b1 -> \[FormalK]], {\[FormalK], lower,
upper}] -
Sum[ReplaceAll[a, b1 -> \[FormalK]], {\[FormalK], lower,
b2 - 1}] -
Sum[ReplaceAll[a, b1 -> \[FormalK]], {\[FormalK], b3 + 1,
upper}]};

ReplaceRepeated[s, sumrule, MaxIterations -> maxtimes]  // Simplify

]


Examples:

Sum[ar[j], {j, 1, k}] + Sum[-ar[j], {j, 1, k + 1}] // simpsum


Output: (* -ar[1 + k] *)

simpsum[Sum[2*ar[j] + br[j], {j, 4, h + 1}] -
2*Sum[ar[j], {j, 3, h}] + Sum[-br[p], {p, 5, h - 1}]]


Output: (* -2 ar[3] + 2 ar[1 + h] + br[4] + br[h] + br[1 + h] *)

If the terms do not completely simplify then simpsum might get lost applying the rules over and over again. In that case one may specify the maximal number of times that the rules are applied using the second option of simpsum. When the second argument is not specified, the rule is applied at most 1000 times. This can be lowered by changing the default value in the definition of the function. In cases where everything simplifies the program should stop before reaching that limit.

In the example below the rule is applied at most 11 times

simpsum[Sum[2*ar[j] + br[j], {j, 4, h + 1}] -
2*Sum[ar[j], {j, 3, h}] + Sum[-br[p], {p, 5, h - 1}] +
Sum[cr[p], {p, 2, g}], 11]


But depending on which phase/period the iteration is on different results may be obtained. A nicer result is obtained by decreasing (or increasing) the maximum number by one in this case.

 simpsum[Sum[2*ar[j] + br[j], {j, 4, h + 1}] -
2*Sum[ar[j], {j, 3, h}] + Sum[-br[p], {p, 5, h - 1}] +
Sum[cr[p], {p, 2, g}], 10]
`
• Could you see my edited, thanks Sep 21, 2022 at 18:45