3
$\begingroup$

I would like to automatically "move nested sums to the left". I mean, just take out of an expression all the summations and go from a nested Sum to a multiple sum. Something like starting with:

Sum[f[y] Sum[g[x],x],y]

And changing it into:

Sum[ f[y] g[x],y,x]

But I can't think of a way to do it.

$\endgroup$
  • 2
    $\begingroup$ Sum[f[y] Sum[g[x], x], y] //. Sum[a_ Sum[b_, x___], y___] :> Sum[a b, x, y] Will do what you are asking, now comes the question of when you can actually do this manipulation which is not addressed. $\endgroup$ – jVincent Jan 21 '13 at 12:50
  • $\begingroup$ It does work, thanks. Do you mean when rearranging the sum is allowed mathematically? If that is the case it should not be a problem, I am dealing with sums over a finite number of elements. $\endgroup$ – Paco Jan 21 '13 at 14:57
3
$\begingroup$

Since my comment seamed to answer the question I'll expand it to an answer. The general notion is simply that when you want to manipulate an expression you can use patterns and replacement to achieve that goal. Here I simply took almost verbatim the replacement you wanted and inserted some patterns to match. The reason x___ and y___ are used is simply so the rule will work for any number of nested sum:

Sum[f[y] Sum[g[x], x], y] //. Sum[a_ Sum[b_, x___], y___] :> Sum[a b, x, y]
(* Sum[f[y]*g[x], x, y] *)

Sum[ m[d] Sum[f[y] Sum[g[x], x], y], d] //. Sum[a_ Sum[b_, x___], y___] :> Sum[a b, x, y]
(* Sum[f[y]*g[x]*m[d], x, y, d] *)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks. By the way, I found that "in practice" I needed to add a couple of extra rules for it to work as I wanted: ruleA= Sum[b_, x___] + Sum[c_, x___] :> Sum[b + c, x] ; ruleB= Sum[b_, x___] - Sum[c_, x___] :> Sum[b - c, x]; ruleC= Sum[a_ Sum[b_, x___], y___] :> Sum[a b, x, y]; $\endgroup$ – Paco Jan 21 '13 at 15:51

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.