Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

7 Answers 7

up vote 34 down vote accepted

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.

share|improve this answer
    
+1 Pretty neat ;) –  Vitaliy Kaurov Feb 23 '13 at 16:10
1  
@minthao_2011 Mathematica auto-sorts things (Plus is Orderless), so you will have a hard time achieving this. –  Leonid Shifrin Feb 23 '13 at 16:19
9  
@minthao_2011 You need % // PolynomialForm[ #, TraditionalOrder -> True]& –  Artes Feb 23 '13 at 16:35
2  
Leonid, is there any reason you are using x:_:1 rather than x_:1 etc.? –  Mr.Wizard Feb 24 '13 at 7:24
1  
@Mr.Wizard Re: x_:1 - yes, equivalent. Re: the logic - sorry, no time right now, will do later. This is a basic process of completing the square, nothing more. –  Leonid Shifrin Feb 24 '13 at 15:07

The following routine tries to eliminate the linear terms by completing the square for arbitrary number of variables:

CenterPoly[poly_] := Module[{a, b, c, u, vars},
  vars = Variables[poly];
  {c, b, a} = {#[[1]], #[[2]]/2, (#[[3]] + Transpose[#[[3]]])/2} &@
    Normal@CoefficientArrays[poly, vars];
  u = PseudoInverse[a].b;
  (#\[Transpose].a.#)[[1, 1]] &[{vars + u}\[Transpose]] + c - u.a.u
]

In case that the polynomial is not expressable solely in quadratic terms it uses the PseudoInverse to get a representation that gets as close to purely quadratic as possible.

CenterPoly[-11 - 2 x + x^2 - 4 y + y^2 - 6 z + z^2]
(* (x - 1)^2 + (y - 2)^2 + (z - 3)^2 - 25 *)

CenterPoly[x^2 - 4 x y + y^2 + 6 x - 4]
(* (x - 1)*(x - 2(y - 2) - 1) + (y - 2)*(-2(x - 1) + (y - 2)) - 1 *)
share|improve this answer

The operation of completing the square with respect to a specified variable is realized by the function CompleteTheSquare in the Manipulations set of routines from David Park's add-on Presentations (http://home.comcast.net/~djmpark/DrawGraphicsPage.html). In your example:

   expr = -11 - 2 x + x^2 - 4 y + y^2 - 6 z + z^2; 
   << Presentations`
   CompleteTheSquare[CompleteTheSquare[CompleteTheSquare[expr, x], y], z]
(* -25 + (-1 + x)^2 + (-2 + y)^2 + (-3 + z)^2 *) 
share|improve this answer
4  
You could express the last line neatly with a fold: Fold[CompleteTheSquare, expr, {x, y, z}] –  Thies Heidecke May 22 '13 at 8:36
    
@ThiesHeidecke: Sure, Fold simplifies the code. I was merely trying to point out that a user need not write --perhaps ought not to have to write -- his own code for such a common operation as completing the square. –  murray May 22 '13 at 13:35
eq = (x - a)^2 + (y - b)^2 + (z - c)^2 - d;
sol = SolveAlways[{-11 - 2 x + x^2 - 4 y + y^2 - 6 z + z^2 == eq}, {x, y, z}]
eq /. sol // PolynomialForm[#, TraditionalOrder -> True] &

(* {{d -> 25, a -> 1, b -> 2, c -> 3}} *)
(* {(x-1)^2+(y-2)^2+(z-3)^2-25} *)


eq = (x - a)^2 + (y - b)^2 + (z - c)^2 - d;
Solve[ForAll[{x, y, z}, -11 - 2 x + x^2 - 4 y + y^2 - 6 z + z^2 == eq], {a, b, c, d}]

(*{{a -> 1, b -> 2, c -> 3, d -> 25}}*)
share|improve this answer
1  
Your answer depend on the number 25. –  minthao_2011 Feb 25 '13 at 10:50
    
@minthao_2011 Update complete. –  chyaong Feb 25 '13 at 11:28
    
Thank you very much. –  minthao_2011 Feb 25 '13 at 14:18

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
share|improve this answer

What about this: This is your left-hand part:

    expr1 = \[Minus]11 \[Minus] 2 x + x^2 \[Minus] 4 y + 
   y^2 \[Minus] 6 z + z^2;

expr2=expr1 /. {x -> X + 1, y -> Y + 2, z -> Z + 3} // Simplify

The result is

-25 + X^2 + Y^2 + Z^2

Now back to old notations:

expr2 /. {X -> x - 1, Y -> y - 2, Z -> z - 3}

The result is:

-25 + (-1 + x)^2 + (-2 + y)^2 + (-3 + z)^2
share|improve this answer

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 
*)
share|improve this answer
    
I don't understand your answer. The right hand side must be 25. Your answer is 11. –  minthao_2011 Feb 25 '13 at 5:43
    
@minthao_2011 Sorry, I forgot one term, corrected now –  belisarius Feb 25 '13 at 6:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.