1
$\begingroup$

I have another task that somehow should be trivial. Suppose I have the following expression

$$ \sum_{i=1}^n \frac{2}{9} x_i + 2 \sum_{i=1}^n \frac{1}{9} x_i, $$

or in Mathematica-FullForm

Plus[Sum[Times[Rational[2,9],Subscript[x,i]],List[i,1,n]],Times[2,Sum[Times[Rational[1,9],Subscript[x,i]],List[i,1,n]]]]

Obviously, this is equal to $ \sum_{i=1}^n \frac{4}{9} x_i $ but Mathematica 6 wouldn't see that. How can I teach Mathematica to simplify this expression in a most general way (the actual expressions I need to simplify are way more complicated, they involve multiple sums and are mostly generated by some other routines)?

EDIT (21.04.2015): By multiple sums, I meant the following general expression: $$ a \sum_{i_1=1}^n \ldots\sum_{i_k=1}^n b\cdot f(x_{i_1},\ldots,x_{i_k}) + c \sum_{i_1=1}^n \ldots \sum_{i_k=1}^n d\cdot g(x_{i_1},\ldots,x_{i_k}). $$ The constants $a,b,c,d$ may be arbitrarily complex, the $k$ may vary as well. I needed a rule that, when applied, produces $$ \sum_{i_1=1}^n \ldots \sum_{i_k=1}^n \left((a\cdot b) f(x_{i_1},\ldots,x_{i_k}) + (c \cdot d) g(x_{i_1},\ldots,x_{i_k})\right). $$

Based on your replies, I have concocted an amazingly simple solution :

HoldPattern[a_ Sum[c_, y___]] :> Sum[a c, y]
HoldPattern[Sum[a_ b_, y___] + Sum[c_ b_, y___]] :> Sum[Together[a+c] b, y]
$\endgroup$
2
$\begingroup$

You may consider this,

rl= (a_. Sum[b_. Subscript[x_, i_], {i_, i0_: 1, n_}]) :> 
   Sum[(a b) Subscript[x, i], {i, i0, n}];

exp = Sum[2/9 Subscript[x, i], {i, 1, n}] + 
   2 Sum[1/9 Subscript[x, i], {i, 1, n}];

Apply transformation rule repeatedly:

exp //. rl

enter image description here

Collecting more than two sums:

exp1 = 1 Sum[3/9 Subscript[x, i], {i, 1, n}] + 
  3 Sum[1/9 Subscript[x, i], {i, 1, n}] + 
  3 Sum[2/9 Subscript[x, i], {i, 1, n}]

enter image description here

exp1 //. rl

resulting in

enter image description here

$\endgroup$
  • $\begingroup$ Can you please comment, why do you use the Default pattern (_.) here? What is the idea behind? $\endgroup$ – Alexei Boulbitch Apr 21 '15 at 12:40
1
$\begingroup$

Use a replacement rule

rule = (a1_. * Sum[c1_. Subscript[x_, i_], {i_, i0_: 1, n_}] +
     a2_. * Sum[c2_. Subscript[x_, i_], {i_, i0_: 1, n_}]) :>
   Sum[(a1*c1 + a2*c2) Subscript[x, i], {i, i0, n}];

expr = Sum[2/9 Subscript[x, i], {i, 1, n}] +
   2 Sum[1/9 Subscript[x, i], {i, 1, n}];

expr /. rule

Sum[(4*Subscript[x, i])/9, {i, 1, n}]

$\endgroup$
  • $\begingroup$ Can you please comment, why do you use the Default pattern (_.) here? What is the idea behind? $\endgroup$ – Alexei Boulbitch Apr 21 '15 at 12:41
  • $\begingroup$ @AlexeiBoulbitch - without the defaults expr won't match the pattern in rule. Define a rule2 without the defaults and evaluate expr /. rule2 $\endgroup$ – Bob Hanlon Apr 21 '15 at 13:00
  • $\begingroup$ Yes, Bob, I understand that. I already played with it. My question is more, why it does not match without and matches with that definition? $\endgroup$ – Alexei Boulbitch Apr 21 '15 at 13:49
  • $\begingroup$ @AlexeiBoulbitch - because the rule is more general than the specific example (expr). The rule allows additional factors which may or may not be present and in this case not all are present. To handle the missing factors the defaults must be explicitly invoked. For example only one of the sums in expr has an external factor but the rule handles none, one (either), or both. $\endgroup$ – Bob Hanlon Apr 22 '15 at 3:31
  • $\begingroup$ Hanllon OK, thank you. This approach seems to should work also for integrals, such as expr = 3*Integrate[g[x], {x, 0, 1}] and rule = a_.*Integrate[f_[x], {x, a_: 0, b_}] -> a*Integrate[f[x], {x, a, b}] but it does not. The operation expr /. rule returns the initial expression. Could you please comment on the reason and difference? $\endgroup$ – Alexei Boulbitch Apr 22 '15 at 9:03

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.