FAQ
overflow

Great Answers to
Questions About Everything

QUESTION

I want Mathematica to express the equation $$-11 - 2 x + x^2 - 4 y + y^2 - 6 z + z^2=0$$ in the form $$(x - 1)^2 + (y - 2)^2 + (z - 3)^2 - 25=0$$ How do I tell Mathematica to do that?

{ asked by minthao_2011 }

ANSWER

You can use custom transformation rules, for example:

-11 - 2 x + x^2 - 4 y + y^2 - 6 z + z^2 //. 
   (a : _ : 1)*s_Symbol^2 + (b : _ : 1)*s_ + rest__ :> 
       a (s + b/(2 a))^2 - b^2/(4 a) + rest

returns

(* -25 + (-1 + x)^2 + (-2 + y)^2 + (-3 + z)^2  *)

The above rule does not account for cases where b is zero, but those are easy to add too, if needed.

{ answered by Leonid Shifrin }
Tweet