# How to reorder and combine terms in a polynomial with multi-variables?

I have a polynomial with the coefficients of {a1, b1, b2}

x = 1/8 (a1^4 E^(4 I τ ω) - 2 a1^2 E^(  2 I τ ω) (b2 Sqrt[1 - t] + b1 Sqrt[t])^2 + (b2 Sqrt[ 1 - t] + b1 Sqrt[t])^4); After expanding, it is :

Expand[x]

b2^4/8 - 1/4 a1^2 b2^2 E^(2 I τ ω) +  1/8 a1^4 E^(4 I τ ω) + 1/2 b1 b2^3 Sqrt[1 - t] Sqrt[t] - 1/2 a1^2 b1 b2 E^(2 I τ ω) Sqrt[1 - t] Sqrt[t] +  3/4 b1^2 b2^2 t - (b2^4 t)/4 - 1/4 a1^2 b1^2 E^(2 I τ ω) t +  1/4 a1^2 b2^2 E^(2 I τ ω) t +  1/2 b1^3 b2 Sqrt[1 - t] t^(3/2) - 1/2 b1 b2^3 Sqrt[1 - t] t^(3/2) + ( b1^4 t^2)/8 - 3/4 b1^2 b2^2 t^2 + (b2^4 t^2)/8


There are two problems :

1. The terms 1/2 b1 b2^3 Sqrt[1 - t] Sqrt[t] and -(1/2) b1 b2^3 Sqrt[1 - t] t^(3/2) have the same coefficient b1 b2^3 , but they donot combine automatically. I need all the terms with the same coefficient in {a1, b1, b2} be combined. e.g.,

1/2 b1 b2^3 Sqrt[1 - t] Sqrt[t] - 1/2 b1 b2^3 Sqrt[1 - t] t^(3/2) =  1/2 b1 b2^3 (1 - t)^(3/2) Sqrt[t]

2. I need the polynomial be reordered in a descending order of the variables {a1, b1, b2}, as shown below, but this is done by hand. I hope how to do so automatically by Mathematica.

1/8 a1^4 E^(4 I τ ω) -  1/4 a1^2 b1^2 E^(2 I τ ω) t -  1/2 a1^2 b1 b2 E^(2 I τ ω) Sqrt[1 - t] Sqrt[t] - 1/4 a1^2 b2^2 E^(2 I τ ω) +  1/4 a1^2 b2^2 E^(2 I τ ω) t + (b1^4 t^2)/8 +  1/2 b1^3 b2 Sqrt[1 - t] t^(3/2) + 3/4 b1^2 b2^2 t -  3/4 b1^2 b2^2 t^2 + 1/2 b1 b2^3 Sqrt[1 - t] Sqrt[t] -  1/2 b1 b2^3 Sqrt[1 - t] t^(3/2) + b2^4/8 - (b2^4 t)/4 + (b2^4 t^2)/8


After reordering in a descending order, and combining all the terms with the same coefficient, I achieve the final result of

1/8 a1^4 E^(4 I τ ω) - 1/4 a1^2 b1^2 E^(2 I τ ω) t - 1/2 a1^2 b1 b2 E^(2 I τ ω) Sqrt[1 - t] Sqrt[t] +1/4 a1^2 2^2 E^(2 I τ ω) (-1 + t) + (b1^4 t^2)/8 +  1/2 b1^3 b2 Sqrt[1 - t] t^(3/2) + -(3/4) b1^2 b2^2 (-1 + t) t +  1/2 b1 b2^3 (1 - t)^(3/2) Sqrt[t] + 1/8 b2^4 (-1 + t)^2


Or in figure format: I can do this work manually in this example with only 14 terms, but in my next step I need to process a polynomial with more than 30 terms. Doing this reordering and combining work automatically is very necessary.

I have checked the previous questions and answers on stackexchange, but my problems can not be solved. Thank you very much if you can help me!

Try this:

Collect[Expand[x], {a1^2 b2^2, b2^4, b1 b2^3, a1^2 b1 b2} ]


I do not give here the result, since it is much too long, but it looks like what you want up to reordering. Concerning the reordering, it is more complex in Mma. There are few approaches, but I do not recommend to go for that without too much need. Anyway, they are (most often) only decorative, since Mma usually reorders the expression according to internal simplicity. To override it one often needs to transform the expression into an inactive (held) form preventing any operation with it. So, it is only useful to visualize the result.

Have fun!

• Thank you very much for the good suggestion. It is really difficult for Mma to reordering the terms in a polynomial. – user14634 Oct 8 '15 at 0:41

I think this would do what you are asking for:

Normal[Series[x, {a1, 0, 4}, {b1, 0, 4}, {b2, 0, 4}]] or in copyable form:

(1/8)*a1^4*E^(4*I*τ*ω) + (1/8)*b2^4*(-1 + t)^2 + (1/2)*b1*b2^3*(1 - t)^(3/2)*Sqrt[t] + (1/2)*b1^3*b2*Sqrt[1 - t]*t^(3/2) + (b1^4*t^2)/8 +
a1^2*((1/4)*b2^2*E^(2*I*τ*ω)*(-1 + t) - (1/2)*b1*b2*E^(2*I*τ*ω)*Sqrt[1 - t]*Sqrt[t] - (1/4)*b1^2*E^(2*I*τ*ω)*t) + b1^2*b2^2*((3*t)/4 - (3*t^2)/4)

• Thank you very much for the good suggestion. – user14634 Oct 8 '15 at 0:41