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?
8 Answers
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.
- 114,335
- 15
- 329
- 420
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}}*)
- 15,542
- 3
- 40
- 78
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
- 124,525
- 11
- 401
- 574
-
This method does not work with mixed products variables in the polynomial. E.g. Thies Heidecke's second example: x^2 - 4 x y + y^2 + 6 x - 4 (see below). – Romke Bontekoe May 04 '19 at 06:24
-
It wasn't designed for, and shouldn't work on, polynomials containing a cross-term like
x y. Conventionally (e.g. applications involving conic sections or quadric surfaces), one would apply something like rotation of axes to remove the cross term before one can do completing the square. – J. M.'s missing motivation Jul 28 '19 at 02:34
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
*)
- 115,881
- 13
- 203
- 453
-
I don't understand your answer. The right hand side must be 25. Your answer is 11. – minthao_2011 Feb 25 '13 at 05:43
-
What about this:
This is your left-hand-side:
expr1 = - 11 - 2 x + x^2 - 4 y + y^2 - 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
- 23,947
- 7
- 58
- 101
- 39,397
- 2
- 47
- 96
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. 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 *)
-
4You could express the last line neatly with a fold:
Fold[CompleteTheSquare, expr, {x, y, z}]– Thies Heidecke May 22 '13 at 08:36 -
@ThiesHeidecke: Sure,
Foldsimplifies 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 -
-
@murray Dear Murray, on my machine Presentations, stopped working. It either happened with passing to Win.10, or to a previous Mma version two versions ago. I cannot tell exactly. I tried to contact David Park and failed. Evidently, Presentations works for you. Could you please kindly comment on this. – Alexei Boulbitch Oct 03 '21 at 17:32
-
@AlexeiBoulbitch: What exactly do you mean by "stopped working"? It's working for me with Mathematica 12.3 under macOS Big Sur 11.6. As I recall, there a few names in the package, e.g. EulerAngle that are shadowed by names now in the Mathematica kernel and I had to modify Presentations so as to change the names to something like DPxxx instead of xxx. – murray Oct 04 '21 at 21:27
-
@murray It happened about a year ago, and I do not remember details now. I think, with passing to Win10 it stopped returning help, and something else also happened that I presently cannot recall. I need to experiment with it to answer you precisely. I will return to you in a few days, if you agree since I am awfully over-occupied now. – Alexei Boulbitch Oct 06 '21 at 12:32
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 *)
- 8,814
- 34
- 44
-
In my opinion, your code becomes clearer when you replace your last line by
(vars+u) . a . (vars+u) + (c-u . a . u). Or even by{vars, u, a, (c-u . a . u)}if you want to bring out the details of the completion of the square more explicitly. – Romke Bontekoe Mar 07 '24 at 08:19
My way for this is:
eq = (x - a)^2 + (y - b)^2 + (z - c)^2 + d;
eq == 0 /. Solve[ForAll[{x, y, z}, -11 - 2 x + x^2 - 4 y + y^2 - 6 z + z^2 == eq]] // TraditionalForm
(* {(x-1)^2+(y-2)^2+(z-3)^2-25} *)
- 3,867
- 1
- 20
- 35
- 4,503
- 3
- 31
- 46
-
This solution is already shown: https://mathematica.stackexchange.com/a/20159/1871 – xzczd Apr 21 '23 at 03:56
PlusisOrderless), so you will have a hard time achieving this. – Leonid Shifrin Feb 23 '13 at 16:19% // PolynomialForm[ #, TraditionalOrder -> True]&– Artes Feb 23 '13 at 16:35x:_:1rather thanx_:1etc.? – Mr.Wizard Feb 24 '13 at 07:24x_: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:07x^2 + y^2 + 2 (m - 1) x - 4 m y + 5 m^2 + 2 m //. (a : _ : 1)*s_Symbol^2 + (b : _ : 1)*s_ + rest__ :> a (s + b/(2 a))^2 - b^2/(4 a) + rest, gives-(1/5) - (-1 + m)^2 - 4 m^2 + 5 (1/5 + m)^2 + (-1 + m + x)^2 + (-2 m + y)^2,How can I get-1 + 4 m + (-1 + m + x)^2 + (-2 m + y)^2? – expression Aug 10 '13 at 06:55a_.different froma_:1? – Kuba Mar 30 '15 at 10:15