# Collect distributed sums

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]


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 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}] exp1 //. rl


resulting in • Can you please comment, why do you use the Default pattern (_.) here? What is the idea behind? Apr 21 '15 at 12:40

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}]

• Can you please comment, why do you use the Default pattern (_.) here? What is the idea behind? Apr 21 '15 at 12:41
• @AlexeiBoulbitch - without the defaults expr won't match the pattern in rule. Define a rule2 without the defaults and evaluate expr /. rule2 Apr 21 '15 at 13:00
• 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? Apr 21 '15 at 13:49
• @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. Apr 22 '15 at 3:31
• 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? Apr 22 '15 at 9:03

I found this question after having solved a similar issue. Here is my solution:

r = (a0_:1) Sum[t0_, i__] + (a1_:1) Sum[t1_, i__] :> Sum[a0 t0 + a1 t1, i]


Then, given the example in the accepted answer, recursively apply the rule:

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}]

InputForm[% //. r]

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