Is there a way to automatically Collect[] the ω terms by their powers for this expression?

expression=Sum[I*E^(I*m*t*ω)*m*ω*p[1, m, t] + E^(I*m*t*ω)*Derivative[0, 0, 1][p][1, m, t], {m, 1, mlimit}]/ω;
Collect[ExpandAll[expression], ω]

This just returns expression -- it fails to separate terms within the summations. What I really want is this:

Sum[I*E^(I*m*t*ω)*m*p[1, m, t], {m, 1, mlimit}] + Sum[E^(I*m*t*ω)*Derivative[0, 0, 1][p][1, m, t], {m, 1, mlimit}]/ω

1 Answer 1


You'll probably have to pull out the factor of ω yourself. You can automate this with something like:

myCollect[a_. + b_.*sum : Sum[c_.*Power[var_, n_.] + d_., argsSum__], 
  vars : var_ | {___, var_, ___}, argsCollect___] := 
    myCollect[a + b*(Power[var, n] Sum[c, argsSum] + Sum[d, argsSum]), 
      vars, argsCollect]

myCollect[args___] := Collect[args]

Then you can do

myCollect[ExpandAll[expression], ω] 

$\frac{\sum _{m=1}^{\text{mlimit}} e^{i m t \omega } p^{(0,0,1)}(1,m,t)}{\omega }+\sum _{m=1}^{\text{mlimit}} i m e^{i m t \omega } p(1,m,t)$

Of course this implementation has some limitations. Most notably:

  • It is assumed that Power[var,n] is otherwise independent of the sum's iterator. This would fail, for example, for $\sum_m \omega^m $
  • Powers of $\omega$ are only found at level 2 in the sum. For example, the $\omega$ in $\sum_m(a + b (\omega + c))$ would not be picked up.
  • $\begingroup$ Excellent, thanks $\endgroup$ Commented Oct 21, 2017 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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