8
$\begingroup$

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?

$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – Bill
    Commented Jan 18, 2020 at 21:29
  • $\begingroup$ 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. $\endgroup$
    – Kai Daniel
    Commented Jan 18, 2020 at 22:13
  • $\begingroup$ @Bill Does using subscripts like now avoid the additional complication from the abstract functions? $\endgroup$
    – Kai Daniel
    Commented Jan 18, 2020 at 22:27
  • $\begingroup$ 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. $\endgroup$
    – Bill
    Commented Jan 18, 2020 at 22:40
  • $\begingroup$ 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. $\endgroup$
    – Kai Daniel
    Commented Jan 18, 2020 at 22:54

2 Answers 2

8
$\begingroup$
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 *)
$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$
    – Chris K
    Commented Jul 2, 2021 at 21:22
  • 1
    $\begingroup$ @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. $\endgroup$
    – Bob Hanlon
    Commented Jul 2, 2021 at 23:35
  • $\begingroup$ Excellent, thanks! $\endgroup$
    – Chris K
    Commented Jul 3, 2021 at 1:51
7
$\begingroup$
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

$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$
    – Bob Hanlon
    Commented Jan 19, 2020 at 17:12
  • $\begingroup$ @Thank you @BobHanlon; very good point. I updated rule1 with something that works for your example. $\endgroup$
    – kglr
    Commented Jan 19, 2020 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.