1
$\begingroup$
(4 - 2 y1 + y1^2) (4 - 2 y2 + y2^2) 

How to convert the above form to the form below?

(3 + (-1 + y1)^2) (3 + (-1 + y2)^2) 

Defining a function that completes the square given a quadratic polynomial expression

Factoring the square of this polynomial?

CompleteTheSquare::notquad="The expression is not quadratic in the variables `1`"; CompleteTheSquare[expr_]:=CompleteTheSquare[expr,Variables[expr]] CompleteTheSquare[expr_,Vars_Symbol]:=CompleteTheSquare[expr,{Vars}] CompleteTheSquare[expr_,Vars:{__Symbol}]:=Module[{array,A,B,C,s,vars,sVars},vars=Intersection[Vars,Variables[expr]]; Check[array=CoefficientArrays[expr,vars],Return[expr],CoefficientArrays::poly]; If[Length[array]!=3,Message[CompleteTheSquare::notquad,vars];Return[expr]]; {C,B,A}=array;A=Symmetrize[A]; s=Simplify[1/2 Inverse[A] . B,Trig->False]; sVars=Hold/@(vars+s);A=Map[Hold,A,{2}]; Expand[A . sVars . sVars]+Simplify[C-s . A . s,Trig->False]//ReleaseHold] CompleteTheSquare[x^2+29x+1000,x] 

The post and code above can only complete the square for a single polynomial.

How to complete the square for each factor of a polynomial product?

$\endgroup$
6
  • 1
    $\begingroup$ CompleteTheSquare /@ expr $\endgroup$ Commented Jan 15 at 3:27
  • $\begingroup$ @BobHanlon However, using this command gives the following prompt:The expression is not quadratic in the variables {y1} $\endgroup$ Commented Jan 15 at 3:34
  • $\begingroup$ There is no message with version "14.1.0 for Mac OS X ARM (64-bit) (July 16, 2024)". What version are you using? $\endgroup$ Commented Jan 15 at 3:45
  • $\begingroup$ @BobHanlon "14.1.0 for Microsoft Windows (64-bit) (July 16, 2024)" $\endgroup$ Commented Jan 15 at 3:46
  • $\begingroup$ @BobHanlon The CompleteTheSquare function requires an additional parameter. $\endgroup$ Commented Jan 15 at 4:01

2 Answers 2

0
$\begingroup$
ResourceFunction["CompleteSquare"] @@@ {{4 - 2 y1 + y1^2, y1}, {4 - 2 y2 + y2^2, y2}} {3 + (-1 + y1)^2, 3 + (-1 + y2)^2} 
$\endgroup$
0
$\begingroup$

Thank you, Bob and J.M. With your hints and help, there are many ways to solve it now.

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}]]] expr = (4 - 2 y1 + y1^2) (4 - 2 y2 + y2^2) (4 x1^2 - 2 x1 + 8) (9 x2^2 - 26 x2 + 100) (x^2 + 2 x y + y^2); List @@ expr depress[#] & /@ (List @@ expr) Times @@ % 
depress /@ expr 

enter image description here

completeSq[a_. x_^2 + b_. x_ + c_ : 0] := -(b^2/(4 a)) + a (b/(2 a) + x)^2 + completeSq[c] completeSq[d_] := d expr = (4 - 2 y1 + y1^2) (4 - 2 y2 + y2^2) (4 x1^2 - 2 x1 + 8) (9 x2^2 - 26 x2 + 100) (x^2 + 2 x y + y^2); completeSq /@ expr 
CompleteTheSquare::notquad = "The expression is not quadratic in the variables `1`"; CompleteTheSquare[expr_] := CompleteTheSquare[expr, Variables[expr]] CompleteTheSquare[expr_, Vars_Symbol] := CompleteTheSquare[expr, {Vars}] CompleteTheSquare[expr_, Vars : {__Symbol}] := Module[{array, A, B, C, s, vars, sVars}, vars = Intersection[Vars, Variables[expr]]; Check[array = CoefficientArrays[expr, vars], Return[expr], CoefficientArrays::poly]; If[Length[array] != 3, Message[CompleteTheSquare::notquad, vars]; Return[expr]]; {C, B, A} = array; A = Symmetrize[A]; s = Simplify[1/2 Inverse[A] . B, Trig -> False]; sVars = Hold /@ (vars + s); A = Map[Hold, A, {2}]; Expand[A . sVars . sVars] + Simplify[C - s . A . s, Trig -> False] // ReleaseHold] CompleteTheSquare[x^2 + 29 x + 1000, x] Times @@ CompleteTheSquare /@ List @@ ((4 - 2 y1 + y1^2) (4 - 2 y2 + y2^2)) 
$\endgroup$