Nested Sums to multiple sum

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.

Thanks!

-
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. – jVincent Jan 21 at 12:50
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. – Paco Jan 21 at 14:57

1 Answer

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] *)

-
 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]; – Paco Jan 21 at 15:51