# Symbolic Series Expansion collecting specific term

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}]/ω


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

ClearAll[myCollect]
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.
• Excellent, thanks – Rupert Gillyweed Oct 21 '17 at 17:58