Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!

share|improve this question
2  
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 '13 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 '13 at 14:57

1 Answer 1

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] *)
share|improve this answer
    
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 '13 at 15:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.