I don't know of a Maxima function that completes the square of
a 2nd degree polynomial. You can *experiment* with:
sfactor(p,x) := block([n, cf, s, d],
p : expand(p),
n : hipow(p,x),
if oddp(n) or n = 0 then p else (
cf : coeff(p,x,n),
s : x^(n/2),
d : expand(cf * s^2 - p),
while hipow(d,x) > 0 do (
d : expand(first(divide(cf * s^2 - p, 2 * cf * s))),
s : s - d),
cf * s^2 + sfactor(p - cf * s^2,x)))$
(%i2) sfactor(x^2 + x + 2007,x);
(%o2) (x+1/2)^2+8027/4
(%i3) expand(%);
(%o3) x^2+x+2007
(%i4) sfactor(-x^2 + a*x + b,x);
(%o4) -(x-a/2)^2+b+a^2/4
(%i5) expand(%);
(%o5) -x^2+a*x+b
(%i6) p : (x^2 + x + 1)^2 + 5*x + 3;
(%o6) (x^2+x+1)^2+5*x+3
(%i7) sfactor(expand(p),x);
(%o7) (x^2+x+1)^2+5*x+3
sfactor does not factor every sos (sum of squares) polynomial into an
explicit sos polynomial; example:
(%i8) p : (x^2 + x + 1)^2 + (x+12)^2;
(%o8) (x^2+x+1)^2+(x+12)^2
(%i9) sfactor(expand(p),x);
(%o9) (x^2+x+3/2)^2+23*x+571/4
My function is poorly tested--maybe you can write a function that
is specialized for 2nd degree polynomials.
BW
-----maxima-bounces at math.utexas.edu wrote: -----
>To:
>From: "J. Simons"
>Sent by: maxima-bounces at math.utexas.edu
>Date: 10/21/2007 03:17PM
>Subject: completesquare
>
>
>
>
>
>
>
>I was wondering if there's a built in maxima function to
>compute the
>completesquare of a quadratic expression .Is there
>one?
>
>Example : x^2-2*x+2 → (x-1)^2+1
>
>
>Thanks a lot for your solution.
>
>Regards,
>
>Jos? Simons_______________________________________________
>Maxima mailing list
>Maxima at math.utexas.edu
>http://www.math.utexas.edu/mailman/listinfo/maxima