# How to arrange the polynomial according to the descending power of x?

The polynomial is as follows：

xpoly=-a^2 b^2 + (b^2 + a^2 k^2) x^2 + a^2 k^2 x0^2 - 2 a^2 k x0 y0 +
a^2 y0^2 + x (-2 a^2 k^2 x0 + 2 a^2 k y0)


How to get the results I want:

(b^2 + a^2 k^2) x^2+x (-2 a^2 k^2 x0 + 2 a^2 k y0)+a^2 k^2 x0^2 - 2 a^2 k x0 y0 + a^2 y0^2-a^2 b^2


If I want to get a further result, it is to factorize each item and how to deal with it. The further result is as follows:

(b^2 + a^2 k^2) x^2+2a^2 k (- k x0 + y0)x+a^2 k^2 x0^2 - 2 a^2 k x0 y0 + a^2 y0^2-a^2 b^2



(b^2 + a^2 k^2) x^2+2a^2 k (- k x0 + y0)x+a^2 k^2 x0^2 - 2 a^2 k x0 y0 + a^2 y0^2-a^2 b^2

The result in bold is what I finally want, and it is to combine the similar items and arrange them according to the descending power of x, and the coefficients in each similar item can be factorized. Or in the form of a whole

Thank you！

• Welcome to Mathematica S.E. To start: 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, since the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) consider accepting the answer, if any, that solves your problem, by clicking checkmark sign, 4) give help too, by answering questions in your areas of expertise.
– bmf
Jan 23, 2023 at 4:51

One way of doing it is the following:

Collect[xpoly, x, # &, Defer[+##]~Reverse~2 &]


Edit: we have established that

res = (b^2 + a^2 k^2) x^2 +
x (-2 a^2 k^2 x0 + 2 a^2 k y0) + (-a^2 b^2 + a^2 k^2 x0^2 -
2 a^2 k x0 y0 + a^2 y0^2);


Then, we can

Factor@FactorTerms[Coefficient[res, x], x]


However, after the edit in the OP it seems that the desired thing is to manipulate each term separately. I suggest

CoefficientList[xpoly, x] // Factor // FullSimplify


Edit 2: it is obvious that the following

Collect[Coefficient[res,
x^2] x^2 + (Factor@FactorTerms[Coefficient[res, x], x]) x +
Select[res, FreeQ[x]], x, # &, Defer[+##]~Reverse~2 &]


gives

Edit 3: using only the code from the O.P -which means xpoly- and a one-liner

Collect[Coefficient[xpoly, x^2] x^2 +
Factor@FactorTerms[Coefficient[xpoly, x], x] x +
Select[xpoly, FreeQ[x]], x, # &, Defer[+##]~Reverse~2 &]


• excellent! thank you! If I want to get a further result, it is to factorize each item and how to deal with it. The further result is as follows: (b^2 + a^2 k^2) x^2+2a^2k (- k x0 + y0)x+a^2 k^2 x0^2 - 2 a^2 k x0 y0 + a^2 y0^2-a^2 b^2 Jan 23, 2023 at 4:50
• @csn899 please see the edit.
– bmf
Jan 23, 2023 at 5:01
• thank you! I want the complete equation form, not the coefficient of a single term. That is to say, the equations are arranged according to the descending power of x after merging the similar terms. What is better is that the coefficients in front of each term of the equation can be factorized. Overall maintenance of equation Jan 23, 2023 at 5:37
• Factor@FactorTerms[Coefficient[res, x], x] the result is:-2 a^2 k (k x0 - y0) Jan 23, 2023 at 5:40
• CoefficientList[xpoly, x] // Factor // FullSimplify the result is:{-a^2 (b + k x0 - y0) (b - k x0 + y0), 2 a^2 k (-k x0 + y0), b^2 + a^2 k^2} Jan 23, 2023 at 5:42