# Collect doesn't work properly

I run Want to write an expression in the form of

expr= (b rr^w+ q rr^v +...)/(c rr^n + p rr^m + d rr^s +...)


To do that, I write

Collect[expr,rr]


But the output is in the form:

(k rr^e (kk +u rr^c+..)+...)/(A rr^t)+o rr^d


The problem is this kind of terms (k rr^e (kk +u rr^c+..)) which MM doesn't write them as (k kk rr^e + k u rr^(c+e)).

Could any one help me? expr is presented here. I have also provided a simpler form for expr which has the same problem when using collect. It is shorter comparing to the above link.

expr=(-h Cos[t] (-(1/(
12 a eta g h (g + h) \[Pi]))(-2 g h (g + h) +
3 a (g^2 + g h + h^2)) H Cos[t] Cos[
tp] (-((g H^2 rr^3 Cos[
tp] (15 Cos[2 t - 4 th - tp] - 9 Cos[2 t - 2 th - tp] -
2 Cos[tp] +
3 (Cos[2 t + tp] + Cos[2 t - 2 th + tp] +
2 Cos[2 th + tp])))/(64 eta \[Pi])) + (
g H^2 rr^3 Sin[
tp] (-15 Sin[2 t - 4 th - tp] - 9 Sin[2 t - 2 th - tp] +
2 Sin[tp] + 3 Sin[2 t + tp] - 3 Sin[2 t - 2 th + tp] +
6 Sin[2 th + tp]))/(64 eta \[Pi])) + (1/(
6 a eta g \[Pi]))(-3 a + 2 g) H Cos[t] Cos[
tp] (-((h H^2 rr^3 Cos[
tp] (15 Cos[2 t - 4 th - tp] - 9 Cos[2 t - 2 th - tp] -
2 Cos[tp] +
3 (Cos[2 t + tp] + Cos[2 t - 2 th + tp] +
2 Cos[2 th + tp])))/(64 eta \[Pi])) + (
h H^2 rr^3 Sin[
tp] (-15 Sin[2 t - 4 th - tp] - 9 Sin[2 t - 2 th - tp] +
2 Sin[tp] + 3 Sin[2 t + tp] - 3 Sin[2 t - 2 th + tp] +
6 Sin[2 th + tp]))/(64 eta \[Pi]))) +
g Sin[t] (-(1/(
6 a eta h \[Pi]))(3 a - 2 h) H Cos[tp] Sin[
t] (-((g H^2 rr^3 Cos[
tp] (15 Cos[2 t - 4 th - tp] - 9 Cos[2 t - 2 th - tp] -
2 Cos[tp] +
3 (Cos[2 t + tp] + Cos[2 t - 2 th + tp] +
2 Cos[2 th + tp])))/(64 eta \[Pi])) + (
g H^2 rr^3 Sin[
tp] (-15 Sin[2 t - 4 th - tp] - 9 Sin[2 t - 2 th - tp] +
2 Sin[tp] + 3 Sin[2 t + tp] - 3 Sin[2 t - 2 th + tp] +
6 Sin[2 th + tp]))/(64 eta \[Pi])) + (1/(
12 a eta g h (g + h) \[Pi]))(2 g h (g + h) -
3 a (g^2 + g h + h^2)) H Cos[tp] Sin[
t] (-((h H^2 rr^3 Cos[
tp] (15 Cos[2 t - 4 th - tp] - 9 Cos[2 t - 2 th - tp] -
2 Cos[tp] +
3 (Cos[2 t + tp] + Cos[2 t - 2 th + tp] +
2 Cos[2 th + tp])))/(64 eta \[Pi])) + (
h H^2 rr^3 Sin[
tp] (-15 Sin[2 t - 4 th - tp] - 9 Sin[2 t - 2 th - tp] +
2 Sin[tp] + 3 Sin[2 t + tp] - 3 Sin[2 t - 2 th + tp] +
6 Sin[2 th + tp]))/(64 eta \[Pi])))) ((32 (3 a -
2 h) Cos[t] Cos[
tp])/(3 a h^2 H rr^3 (-15 Cos[2 t - 4 th - tp] Cos[tp] +
9 Cos[2 t - 2 th - tp] Cos[tp] + 2 Cos[tp]^2 -
3 Cos[tp] Cos[2 t + tp] - 3 Cos[tp] Cos[2 t - 2 th + tp] -
6 Cos[tp] Cos[2 th + tp] - 15 Sin[2 t - 4 th - tp] Sin[tp] -
9 Sin[2 t - 2 th - tp] Sin[tp] + 2 Sin[tp]^2 +
3 Sin[tp] Sin[2 t + tp] - 3 Sin[tp] Sin[2 t - 2 th + tp] +
6 Sin[tp] Sin[2 th + tp])) + (G (-(1/(
6 a eta h \[Pi]))(3 a - 2 h) H Cos[t] Cos[
tp] (((3 a -
2 H) (((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp]^2)/(
24 a eta H (G +
H) \[Pi]) + ((-4 G H (G + H) (G + 2 H) +
3 a (G^3 + G^2 H + 3 G H^2 + 2 H^3)) Cos[tp]^2)/(
24 a eta G (G + H) \[Pi])))/(
6 a eta \[Pi]) + (-((G (3 a - 2 H) Cos[tp] Sin[tp])/(
6 a eta H \[Pi])) - ((-2 G H (G + H) +
3 a (G^2 + G H + H^2)) Cos[tp] Sin[tp])/(
12 a eta G (G +
H) \[Pi])) (((4 G H (G + H) (2 G + H) -
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp] Sin[
tp])/(24 a eta G (G +
H) \[Pi]) + ((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp] Sin[
tp])/(24 a eta G (G + H) \[Pi]))) + (1/(
64 eta \[Pi]))
h H^2 rr^3 Cos[
tp] (((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp]^2)/(

24 a eta H (G + H) \[Pi]) + ((-4 G H (G + H) (G + 2 H) +
3 a (G^3 + G^2 H + 3 G H^2 + 2 H^3)) Cos[tp]^2)/(
24 a eta G (G + H) \[Pi])) (2 Cos[t] +
3 (2 Cos[t - 2 th] - 5 Cos[t - 4 th - 2 tp] -
Cos[t + 2 tp] - 3 Cos[t - 2 (th + tp)] +
Cos[t + 2 (th + tp)])) (-((
h H^2 rr^3 Cos[
tp] (15 Cos[2 t - 4 th - tp] -
9 Cos[2 t - 2 th - tp] - 2 Cos[tp] +
3 (Cos[2 t + tp] + Cos[2 t - 2 th + tp] +
2 Cos[2 th + tp])))/(64 eta \[Pi])) + (
h H^2 rr^3 Sin[
tp] (-15 Sin[2 t - 4 th - tp] -
9 Sin[2 t - 2 th - tp] + 2 Sin[tp] + 3 Sin[2 t + tp] -
3 Sin[2 t - 2 th + tp] + 6 Sin[2 th + tp]))/(
64 eta \[Pi]))))/(H ((-(((3 a -
2 H) (((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp]^2)/(
24 a eta H (G +
H) \[Pi]) + ((-4 G H (G + H) (G + 2 H) +
3 a (G^3 + G^2 H + 3 G H^2 + 2 H^3)) Cos[tp]^2)/(
24 a eta G (G + H) \[Pi])))/(
6 a eta \[Pi])) - (-((G (3 a - 2 H) Cos[tp] Sin[tp])/(
6 a eta H \[Pi])) - ((-2 G H (G + H) +
3 a (G^2 + G H + H^2)) Cos[tp] Sin[tp])/(
12 a eta G (G +
H) \[Pi])) (((4 G H (G + H) (2 G + H) -
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp] Sin[
tp])/(24 a eta G (G +
H) \[Pi]) + ((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp] Sin[
tp])/(24 a eta G (G + H) \[Pi]))) ((
G h H rr^3 Cos[
tp] (15 Cos[2 t - 4 th - tp] - 9 Cos[2 t - 2 th - tp] -
2 Cos[tp] +
3 (Cos[2 t + tp] + Cos[2 t - 2 th + tp] +
2 Cos[2 th + tp])))/(64 eta \[Pi]) - (
G h H rr^3 Sin[
tp] (-15 Sin[2 t - 4 th - tp] -
9 Sin[2 t - 2 th - tp] + 2 Sin[tp] + 3 Sin[2 t + tp] -
3 Sin[2 t - 2 th + tp] + 6 Sin[2 th + tp]))/(
64 eta \[Pi])) + ((-((G (3 a - 2 H) Cos[tp] Sin[tp])/(
6 a eta H \[Pi])) - ((-2 G H (G + H) +
3 a (G^2 + G H + H^2)) Cos[tp] Sin[tp])/(
12 a eta G (G +
H) \[Pi])) (-(((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp] Sin[
tp])/(24 a eta H (G +
H) \[Pi])) + ((4 G H (G + H) (G + 2 H) -
3 a (G^3 + G^2 H + 3 G H^2 + 2 H^3)) Cos[tp] Sin[
tp])/(24 a eta G (G +
H) \[Pi])) + (((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp]^2)/(
24 a eta H (G +
H) \[Pi]) + ((-4 G H (G + H) (G + 2 H) +
3 a (G^3 + G^2 H + 3 G H^2 + 2 H^3)) Cos[tp]^2)/(
24 a eta G (G + H) \[Pi])) ((
H (-(2/a) + 3/G + (3 G)/(G H + H^2)) Cos[tp]^2)/(
12 eta \[Pi]) - (G (3 a - 2 H) Sin[tp]^2)/(
6 a eta H \[Pi]))) (-((
h H^2 rr^3 Cos[
tp] (15 Cos[2 t - 4 th - tp] -
9 Cos[2 t - 2 th - tp] - 2 Cos[tp] +
3 (Cos[2 t + tp] + Cos[2 t - 2 th + tp] +
2 Cos[2 th + tp])))/(64 eta \[Pi])) + (
h H^2 rr^3 Sin[
tp] (-15 Sin[2 t - 4 th - tp] -
9 Sin[2 t - 2 th - tp] + 2 Sin[tp] + 3 Sin[2 t + tp] -
3 Sin[2 t - 2 th + tp] + 6 Sin[2 th + tp]))/(
64 eta \[Pi])))) + ((32 (3 a - 2 h) Cos[tp] Sin[
t])/(3 a h^2 H rr^3 (-15 Cos[2 t - 4 th - tp] Cos[tp] +
9 Cos[2 t - 2 th - tp] Cos[tp] + 2 Cos[tp]^2 -
3 Cos[tp] Cos[2 t + tp] -
3 Cos[tp] Cos[2 t - 2 th + tp] -
6 Cos[tp] Cos[2 th + tp] -
15 Sin[2 t - 4 th - tp] Sin[tp] -
9 Sin[2 t - 2 th - tp] Sin[tp] + 2 Sin[tp]^2 +
3 Sin[tp] Sin[2 t + tp] -
3 Sin[tp] Sin[2 t - 2 th + tp] +
6 Sin[tp] Sin[2 th + tp])) + (G (-(1/(
6 a eta h \[Pi]))(3 a - 2 h) H Cos[tp] Sin[
t] (((3 a -
2 H) (((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp]^2)/(
24 a eta H (G +
H) \[Pi]) + ((-4 G H (G + H) (G + 2 H) +
3 a (G^3 + G^2 H + 3 G H^2 + 2 H^3)) Cos[tp]^2)/(
24 a eta G (G + H) \[Pi])))/(
6 a eta \[Pi]) + (-((G (3 a - 2 H) Cos[tp] Sin[tp])/(
6 a eta H \[Pi])) - ((-2 G H (G + H) +
3 a (G^2 + G H + H^2)) Cos[tp] Sin[tp])/(
12 a eta G (G +
H) \[Pi])) (((4 G H (G + H) (2 G + H) -
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp] Sin[
tp])/(24 a eta G (G +
H) \[Pi]) + ((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp] Sin[
tp])/(24 a eta G (G + H) \[Pi]))) + (1/(
64 eta \[Pi]))
h H^2 rr^3 Cos[
tp] (((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp]^2)/(
24 a eta H (G +
H) \[Pi]) + ((-4 G H (G + H) (G + 2 H) +
3 a (G^3 + G^2 H + 3 G H^2 + 2 H^3)) Cos[tp]^2)/(
24 a eta G (G + H) \[Pi])) (-((
h H^2 rr^3 Cos[
tp] (15 Cos[2 t - 4 th - tp] -
9 Cos[2 t - 2 th - tp] - 2 Cos[tp] +
3 (Cos[2 t + tp] + Cos[2 t - 2 th + tp] +
2 Cos[2 th + tp])))/(64 eta \[Pi])) + (
h H^2 rr^3 Sin[
tp] (-15 Sin[2 t - 4 th - tp] -
9 Sin[2 t - 2 th - tp] + 2 Sin[tp] +
3 Sin[2 t + tp] - 3 Sin[2 t - 2 th + tp] +
6 Sin[2 th + tp]))/(64 eta \[Pi])) (2 Sin[t] +
3 (-2 Sin[t - 2 th] + 5 Sin[t - 4 th - 2 tp] +
Sin[t + 2 tp] - 3 Sin[t - 2 (th + tp)] +
Sin[t + 2 (th + tp)]))))/(H ((-(((3 a -
2 H) (((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp]^2)/(
24 a eta H (G +
H) \[Pi]) + ((-4 G H (G + H) (G + 2 H) +
3 a (G^3 + G^2 H + 3 G H^2 + 2 H^3)) Cos[tp]^2)/(
24 a eta G (G + H) \[Pi])))/(
6 a eta \[Pi])) - (-((G (3 a - 2 H) Cos[tp] Sin[tp])/(
6 a eta H \[Pi])) - ((-2 G H (G + H) +
3 a (G^2 + G H + H^2)) Cos[tp] Sin[tp])/(
12 a eta G (G +
H) \[Pi])) (((4 G H (G + H) (2 G + H) -
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp] Sin[
tp])/(24 a eta G (G +
H) \[Pi]) + ((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp] Sin[
tp])/(24 a eta G (G + H) \[Pi]))) ((
G h H rr^3 Cos[
tp] (15 Cos[2 t - 4 th - tp] -
9 Cos[2 t - 2 th - tp] - 2 Cos[tp] +
3 (Cos[2 t + tp] + Cos[2 t - 2 th + tp] +
2 Cos[2 th + tp])))/(64 eta \[Pi]) - (
G h H rr^3 Sin[
tp] (-15 Sin[2 t - 4 th - tp] -
9 Sin[2 t - 2 th - tp] + 2 Sin[tp] +
3 Sin[2 t + tp] - 3 Sin[2 t - 2 th + tp] +
6 Sin[2 th + tp]))/(
64 eta \[Pi])) + ((-((G (3 a - 2 H) Cos[tp] Sin[tp])/(
6 a eta H \[Pi])) - ((-2 G H (G + H) +
3 a (G^2 + G H + H^2)) Cos[tp] Sin[tp])/(
12 a eta G (G +
H) \[Pi])) (-(((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp] Sin[
tp])/(24 a eta H (G +
H) \[Pi])) + ((4 G H (G + H) (G + 2 H) -
3 a (G^3 + G^2 H + 3 G H^2 + 2 H^3)) Cos[tp] Sin[
tp])/(24 a eta G (G +
H) \[Pi])) + (((-4 G H (G + H) (2 G + H) +
3 a (2 G^3 + 3 G^2 H + G H^2 + H^3)) Cos[tp]^2)/(
24 a eta H (G +
H) \[Pi]) + ((-4 G H (G + H) (G + 2 H) +
3 a (G^3 + G^2 H + 3 G H^2 + 2 H^3)) Cos[tp]^2)/(
24 a eta G (G + H) \[Pi])) ((
H (-(2/a) + 3/G + (3 G)/(G H + H^2)) Cos[tp]^2)/(
12 eta \[Pi]) - (G (3 a - 2 H) Sin[tp]^2)/(
6 a eta H \[Pi]))) (-((
h H^2 rr^3 Cos[
tp] (15 Cos[2 t - 4 th - tp] -
9 Cos[2 t - 2 th - tp] - 2 Cos[tp] +
3 (Cos[2 t + tp] + Cos[2 t - 2 th + tp] +
2 Cos[2 th + tp])))/(64 eta \[Pi])) + (
h H^2 rr^3 Sin[
tp] (-15 Sin[2 t - 4 th - tp] -
9 Sin[2 t - 2 th - tp] + 2 Sin[tp] +
3 Sin[2 t + tp] - 3 Sin[2 t - 2 th + tp] +
6 Sin[2 th + tp]))/(64 eta \[Pi]))))) Tan[t]);

• Can you provide a complete, self-contained example small enough to fit in this post? Commented Jul 24, 2018 at 4:53
• Sorry, no. I can not do that because I don't know where does the problem originate from @Mr.Wizard Commented Jul 24, 2018 at 4:54
• I did find a simpler example and put it in the question @Mr.Wizard Commented Jul 24, 2018 at 5:20
• Is it ok or I should find smpler example? @Mr.Wizard Commented Jul 24, 2018 at 5:44
• It's OK now as the question will not become invalid if that link is broken, which was my primary concern. However an even simpler example is always nice, and may increase the speed or likelihood of receiving an answer. Commented Jul 24, 2018 at 5:47

Collect[rr + rr^(1 + n) + rr^3, rr]
(*rr^3 + rr (1 + rr^n)*)


use

Collect[rr + rr^(1 + n) + rr^3, rr^_]
(*rr + rr^3 + rr^(1 + n)*)


Before using Numerator and Denominator you have to transform expr in a rational form:

exprTog=Together[expr];


The denominator doesn't depend on rr

D[Denominator[exprT], rr] (* ==0 *)


and the numerator includes terms rr^0 & rr^6

cl = CoefficientList[ Numerator[exprT], rr];
Length[cl]
(*7*)

Simplify[First[cl] ]
(*512 (3 a - 2 h) H^3 (-2 G H (G + H) + 3 a (G^2 + G H + H^2)) ((g^2 - h^2) (2 g h (g + h) -3 a (g^2 + g h + h^2)) + (-2 g h (g^3 + 2 g^2 h + 2 g h^2 + h^3) + 3 a (g^4 + 2 g^3 h + g^2 h^2 + 2 g h^3 + h^4)) Cos[2 t]) Cos[tp]^2 Sec[t]*)

Simplify[Last[cl]]
(*9 a^2 G^2 h^3 H^5 (G +H) ((g^2 - h^2) (2 g h (g + h) -
3 a (g^2 + g h + h^2)) + (-2 g h (g^3 + 2 g^2 h + 2 g h^2 +
h^3) + 3 a (g^4 + 2 g^3 h + g^2 h^2 + 2 g h^3 + h^4)) Cos[2 t]) Cos[tp]^2
(-2 - 6 Cos[2 (t - th)] + 15 Cos[2 (t - 2 th - tp)] +
3 Cos[2 (t + tp)] + 6 Cos[2 (th + tp)])^2 Sec[t]*)

• It helps, but not always. For example it doesn't return what I want for the expr presented in the link Commented Jul 24, 2018 at 15:38
• "In the link" means huge expr of your question? Commented Jul 25, 2018 at 7:00
• Yes it does .... Commented Jul 25, 2018 at 10:18
• Could any one help me? expr is presented here. link is in the word here Commented Jul 25, 2018 at 10:19
• Your last comment is unclear! What do you know about the structure of your expression, is it rational ? Commented Jul 25, 2018 at 10:34

Since Collect seems to be able to give you a fraction that is almost on the form you want, I'll substitute an example fraction here:

frac = (c[1]+rr^2 (2 rr^4+3 rr^6+4 rr^8+5 rr^10) c[2]-
rr^3 (2 rr^6+3 rr^9+4 rr^12+5 rr^15) c[3])/
(d[1]+rr^7 (2 rr^4+3 rr^6+4 rr^8+5 rr^10) d[2]-
rr^11 (2 rr^6+3 rr^9+4 rr^12+5 rr^15) d[3])


You can treat the numerator and denominators as polynomials in rr:

num = Numerator[frac];
den = Denominator[frac];
newfrac = With[{cl1 = CoefficientList[num, rr], cl2 = CoefficientList[den, rr]},
(cl1.rr^Range[0,Length[cl1]-1])/(cl2.rr^Range[0,Length[cl2]-1])
]


(c[1]+2 rr^6 c[2]+3 rr^8 c[2]+4 rr^10 c[2]+rr^12 (5 c[2]-3 c[3])-2 rr^9 c[3]-4 rr^15 c[3]-5 rr^18 c[3])/(d[1]+2 rr^11 d[2]+3 rr^13 d[2]+4 rr^15 d[2]+rr^17 (5 d[2]-2 d[3])-3 rr^20 d[3]-4 rr^23 d[3]-5 rr^26 d[3])

I would suggest the following hack which I invented when working with extremely large expressions containing millions of terms in many vars. The hack works much better and faster than Collect, when one needs to collect terms to the given powers of specific vars. Of course, as was suggested by Marius Ladegård Meyer your need to work with numerator and denominator separately

The numerator:

FromCoefficientRules[
Normal[Association @@ (CoefficientRules[
Expand[Numerator[Together[expr]]], {rr}])], {rr}]


And similarly for denominator.

Note, that instead of just single rr, you have possibility to collect using many vars. And the most important thing is that the collection is extremely fast.

Edit 1

As Daniel Lichtblau noted the Normal[Association @@ is superfluous. Therefore fast collection of coefficients of variables var1,var2,... of the polynomial poly can be obtained with

FromCoefficientRules[ CoefficientRules[poly, {var1,var2,...}], {var1,var2,...}]

• It is very very slow indeed. Commented Jul 24, 2018 at 15:34
• For the expression your gave in the question (I didn't try the file) the collection method based on Association Key handling took few seconds on 10 year old laptop. Most of time, of course, being spend inside Together and Expand. I will be surprised (and very happy as well) if somebody will suggest a faster method.
– Acus
Commented Jul 24, 2018 at 16:05
• I used your code, after 10 minutes, my laptop confused Commented Jul 24, 2018 at 16:54
• It takes 1.34251 seconds for the numerator on Mathematica version 10.3, 2008 year laptop running old Linux OS. The leaf count of the result is 162982. You have to split the code into the pieces to find the problem. I only can repeat that using the Association approach I was able to Collect terms for expressions with millions of terms and typical leaf count of the collected result of hundreds of millions (intermediate results taking more than 100Gb RAM). With system Collect and pattern matching approach the transformation would hardly be possible.
– Acus
Commented Jul 24, 2018 at 19:28
• @Daniel Lichtblau Dear Daniel, if you programmed FromCoefficientRules[], would it be possible to extend the command to accept the Association as an argument?
– Acus
Commented Jul 28, 2018 at 10:08