How do I get my equation to have the form $(x-a)^2 + (y-b)^2 + (z-c)^2-d = 0$?
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.
A different route:
(* polynomial depression *)
depress[poly_] := depress[poly, First@Variables[poly]]
depress[poly_, x_] /; PolynomialQ[poly, x] := Module[{n = Exponent[poly, x], x0},
x0 = -Coefficient[poly, x, n - 1]/(n Coefficient[poly, x, n]);
Normal[Series[poly, {x, x0, n}]]]
tst = -11 - 2 x + x^2 - 4 y + y^2 - 6 z + z^2;
vars = {x, y, z};
{cnst, lin, quad} = MapAt[Diagonal, Normal[CoefficientArrays[tst]], {3}];
cnst + Total[MapThread[depress[#1 FromDigits[{##2}, #1]] &, {vars, quad, lin}]]
-25 + (-1 + x)^2 + (-2 + y)^2 + (-3 + z)^2
An algebraic one:
h = -2 x + x^2 - 4 y + y^2 - 6 z + z^2 == 11;
((# /. {x -> 0, y -> 0, z -> 0}) + h[[2]] == #) &@
Total[(#2/2/Sqrt@#3 + Sqrt@#3 #4)^2 & @@@ (Join[CoefficientList[h[[1]], #], {#}] & /@ {x, y, z})]
(*
25 ==(-1 + x)^2 + (-2 + y)^2 + (-3 + z)^2
*)