# How to group terms in an expression according to the powers of their variables?

Let us consider the following non-linear 2nd-order ODE: $$$$\scriptsize{ 2 \zeta q_1'(\tau )+q_1''(\tau )+q_1(\tau ){}^3 \left(\theta +\sigma \left(-\epsilon -\frac{3}{2}\right)\right)+q_2(\tau ) q_1(\tau ){}^2 \left(\sigma \left(\frac{3 \epsilon }{2}+\frac{3}{2}\right)-\frac{3 \theta }{2}\right)+q_1(\tau ) \left(2 \theta +q_2(\tau ){}^2 \left(\frac{3 \theta }{2}+\sigma \left(-\frac{3 \epsilon }{2}-\frac{3}{2}\right)\right)+\sigma (-2 \epsilon -3)+1\right)+q_2(\tau ){}^3 \left(\sigma \left(\frac{\epsilon }{2}+\frac{1}{2}\right)-\frac{\theta }{2}\right)+q_2(\tau ) (\sigma (\epsilon +1)-\theta ) = 0 }$$$$ in which $$\tau$$ is the independent variable, $$q_1(\tau)$$ and $$q_2(\tau)$$ are the dependent variables and all other Greek letters are constant parameters.

I want to group and detach the terms of this ODE according to their powers in $$q_1(\tau)$$ and $$q_1(\tau)$$. Therefore, terms with $$q_1(\tau)$$ should be in Group 1, those ones with $$q_1(\tau)^2q_2(\tau)$$ should be in Group 2 etc. To achieve that, I proposed the strategy below:

1. First, I transformed the ODE into a list:

ode = (\[Theta]+(-(3/2)-\[Epsilon]) \[Sigma]) Subscript[q, 1][\[Tau]]^3+(-\[Theta]+(1+\[Epsilon]) \[Sigma]) Subscript[q, 2][\[Tau]]+(-(1/2) (3 \[Theta])+(3/2+(3 \[Epsilon])/2) \[Sigma]) Subscript[q, 1][\[Tau]]^2 Subscript[q, 2][\[Tau]]+(-(\[Theta]/2)+(1/2+\[Epsilon]/2) \[Sigma]) Subscript[q, 2][\[Tau]]^3+Subscript[q, 1][\[Tau]] (1+2 \[Theta]+(-3-2 \[Epsilon]) \[Sigma]+((3 \[Theta])/2+(-(3/2)-(3 \[Epsilon])/2) \[Sigma]) Subscript[q, 2][\[Tau]]^2)+2 \[Zeta] (Subscript[q, 1]^\[Prime])[\[Tau]]+(Subscript[q, 1]^\[Prime]\[Prime])[\[Tau]] == 0
list = List @@ Expand[ode[[1]]]

2. Then, I used Select command to collect the terms as I want. For example, to collect terms in $$q_1(\tau)q_2(\tau)^2$$ I used:

Select[Select[list, MemberQ[#, Subscript[q, 1][\[Tau]]] &], MemberQ[#, Subscript[q, 2][\[Tau]]^2] &]


which gives: $$$$\scriptsize \frac{3}{2}\theta{q}_1(\tau)q_2(\tau){}^2, -\frac{3}{2}\sigma{q}_1(\tau)q_2(\tau){}^2, -\frac{3}{2}\sigma\epsilon{q}_1(\tau)q_2(\tau){}^2$$$$ as I expected. However, my strategy does not work for all terms. For example, to select the terms in $$q_1(\tau)$$ I used:

Select[Select[list, MemberQ[#, Subscript[q, 1][\[Tau]]] &],FreeQ[#, Subscript[q, 2][\[Tau]]^2] &]


which gives: $$$$\scriptsize 2 \theta q_1(\tau ),-3 \sigma q_1(\tau ),-2 \sigma \epsilon q_1(\tau )$$$$ which is incomplete. It should actually be: $$$$\scriptsize q_1(\tau), 2 \theta q_1(\tau ),-3 \sigma q_1(\tau ),-2 \sigma \epsilon q_1(\tau )$$$$

I have tried to work that around employing different solutions, but so far none of them worked. I have also tried Cases command, but it was also unsuccessful.

Can somoene help me find what am I doing wrong? Thanks in advance.

odeA = (\[Theta] + (-(3/2) - \[Epsilon]) \[Sigma]) Subscript[q,
1][\[Tau]]^3 + (-\[Theta] + (1 + \[Epsilon]) \[Sigma]) \
Subscript[q,
2][\[Tau]] + (-(1/2) (3 \[Theta]) + (3/2 + (3 \[Epsilon])/
2) \[Sigma]) Subscript[q, 1][\[Tau]]^2 Subscript[q,
2][\[Tau]] + (-(\[Theta]/
2) + (1/2 + \[Epsilon]/2) \[Sigma]) Subscript[q,
2][\[Tau]]^3 +
Subscript[q, 1][\[Tau]] (1 +
2 \[Theta] + (-3 -
2 \[Epsilon]) \[Sigma] + ((3 \[Theta])/
2 + (-(3/2) - (3 \[Epsilon])/2) \[Sigma]) Subscript[q,
2][\[Tau]]^2) +
2 \[Zeta] (Subscript[q, 1]^\[Prime])[\[Tau]] + (Subscript[q,
1]^\[Prime]\[Prime])[\[Tau]] == 0;


Try this:

lst = Join[{Subscript[q, 1]''[\[Tau]], Subscript[q, 1]'[\[Tau]]},
Table[Subscript[q, 1][\[Tau]]^i, {i, 1, 3}],
Table[Subscript[q, 2][\[Tau]]^
i, {i, 1, 3}], {Subscript[q, 1][\[Tau]]*Subscript[q, 2][\[Tau]]^2,
Subscript[q, 1][\[Tau]]^2*Subscript[q, 2][\[Tau]]}]


and then

Collect[Expand[odeA], lst]


This is how the result looks on my screen:

Have fun!