# How to simplify Sum's and Product's of arbitrary length?

I would expect

Sum[Subscript[x,i], {i, 1, n}] + Sum[-Subscript[x,i], {i, 1, n}]


and

Product[Subscript[x,i], {i, 1, n}]*Product[Subscript[x,i]^-1, {i, 1, n}]]


to simplify to 0 and 1, which they do not. The only way I was able to make them do so was when assuming n == 9 or any other specific integer. But clearly they should do so for any integer (assuming they are integer doesn't help either; nor does assuming the x's are finite).

How can I simplify Sum's and Product's for sequences of arbitrary length?

• Consider x[i_]:=RandomReal[] or x[i_]:=Infinity or x[i_]:=Indeterminant How does Mathematica know these and other even stranger cases are not what the unknown abstract function x[i] and abstract sum are? Mathematica's handling of abstract vectors, sums, products, functions... is limited.
– Bill
Jan 18 '20 at 21:29
• If these strange cases are an issue for an unspecified integer, why are they not then an issue when I specify the integer? Also Simplify[...,ForAll[i,Element[x[i],Reals]]] doesn't lead to a result for me. Jan 18 '20 at 22:13
• @Bill Does using subscripts like now avoid the additional complication from the abstract functions? Jan 18 '20 at 22:27
• It is guessing plus some evidence that the implementation of Mathematica is not strictly exactly correct in many border cases. That may be a tradeoff between ease of conventional use versus formal rigor. Subscripts have a history of problems. One example: I recently changed all Subscript[b,i] into bi in a differential equation that a person had been struggling with for days with no success and it magically worked. I always assume the more you desktop publish your input the more problems you are inviting. Google subscript problem site:mathematica.stackexchange.com and see what you find.
– Bill
Jan 18 '20 at 22:40
• Thanks for your explanations. What I'm trying to find out is: given an unspecified sequence in the Reals, how can I simplify Sum's and Product's of it. If there's a better version to define the sequence, it would be helpful to have the question changed accordingly. Jan 18 '20 at 22:54

sumRule =
Sum[expr1_, iter_List] + Sum[expr2_, iter_List] :>
Sum[expr1 + expr2, iter];

productRule =
Product[expr1_, iter_List]*Product[expr2_, iter_List] :>
Product[expr1*expr2, iter];

Sum[Subscript[x, i], {i, 1, n}] +
Sum[-Subscript[x, i], {i, 1, n}] /. sumRule

(* 0 *)

Product[Subscript[x, i], {i, 1, n}]*
Product[Subscript[x, i]^-1, {i, 1, n}] /. productRule

(* 1 *)

• Do you think sumRule = c1_ Sum[expr1_, iter_List] + c2_ Sum[expr2_, iter_List] :> Sum[c1 expr1 + c2 expr2, iter] is a suitable generalization for cases when there are prefactors? Jul 2 '21 at 21:22
• @ChrisK - almost. You need to include handling the defaults, i.e., sumRule = c1_. Sum[expr1_, iter_List] + c2_. Sum[expr2_, iter_List] :> Sum[c1 expr1 + c2 expr2, iter] Note the presence of the periods. Jul 2 '21 at 23:35
• Excellent, thanks! Jul 3 '21 at 1:51
rule1 = Sum[a_Times, b : {i_, __}] :> Select[FreeQ[i]][a] Sum[Select[Not@*FreeQ[i]][a], b]
rule2 = Product[Power[a_, b_.], c_] :> Product[a, c]^b;


Examples:

expr1 = Sum[Subscript[x, i], {i, 1, n}] + Sum[-Subscript[x, i], {i, 1, n}];

TeXForm @ expr1


$$\sum _{i=1}^n -x_i+\sum _{i=1}^n x_i$$

expr1 /. rule1


0

expr2 = Product[Subscript[x, i], {i, 1, n}]*Product[Subscript[x, i]^-1, {i, 1, n}];

TeXForm @ expr2


$$\left(\prod _{i=1}^n \frac{1}{x_i}\right) \prod _{i=1}^n x_i$$

expr2 /. rule2


1

• What if the term includes more than one factor, e.g., Sum[Subscript[x, i]*Subscript[y, i], {i, 1, n}] + Sum[-Subscript[x, i]*Subscript[y, i], {i, 1, n}] /. rule1 Jan 19 '20 at 17:12
• @Thank you @BobHanlon; very good point. I updated rule1 with something that works for your example.
– kglr
Jan 19 '20 at 19:18