# How can I convert $(x - 1)^2 + (y + 2)^2 + (z - 5)^2 - 25 = 0$ to $x^2 + y^2 + z^2 - 2x + 4y - 10z + 5 = 0$?

With Maple, I tried

expand((x - 1)^2 + (y + 2)^2 + (z - 5)^2 - 25 = 0);


and got

With Mathematica, I tried

Expand[(x - 1)^2 + (y + 2)^2 + (z - 5)^2 - 25 == 0]


I got

5 - 2 x + x^2 + 4 y + y^2 - 10 z + z^2 == 0

How can I get the result like result of Maple?

• Related: 33130 Commented Mar 10 at 10:37

HoldForm[+##] & @@
MonomialList[(x - 1)^2 + (y + 2)^2 + (z - 5)^2 - 25,
"DegreeLexicographic"]


$$x^2+y^2+z^2-2 x+4 y-10 z+5$$

cheating by using answer in The ordering problem of multivariate polynomials

With very minor change to make the order reverses from the original code.

p = Expand[(x - 1)^2 + (y + 2)^2 + (z - 5)^2 - 25];
orderedForm[poly_, var_List] :=
HoldForm[+##] & @@
MonomialList[poly,
var][[Ordering[Total[#] & @@@ CoefficientRules[poly, var], All,
GreaterEqual]]];
orderedForm[p, {x, y, z}]


There should be simpler way I would think.

While it doesn't give exactly the form you request, you can get an order that you might prefer, using TraditionalForm

Expand[(x - 1)^2 + (y + 2)^2 + (z - 5)^2 - 25 == 0] // TraditionalForm
(* x^2-2 x+y^2+4 y+z^2-10 z+5==0 *)