# 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.

• 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. Jan 21, 2013 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, 2013 at 14:57

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]