Another take. This is a function that takes a polynomial in any number of variables. Where all variables appear to the 2nd power, and not higher. The 2nd power terms has to have coefficients of +1
:
completeTheSquare[poly_] := With[
{form = Total@Table[(i + Unique[])^2, {i, Variables@poly}] + Unique[]},
form /. Flatten@Solve[ForAll[Evaluate@Variables@poly, poly == form]]
] /;
And[
Union[Exponent[poly, Variables@poly]] == {2},
Union[Coefficient[poly, #, 2] & /@ Variables@poly] == {1}
]
Applying the function:
completeTheSquare[t^2 + 10 t - 39]
(* -64+(5+t)^2 *)
completeTheSquare[x^2 + y^2 - 2 x + 6 y + 9]
(* -1+(-1+x)^2+(3+y)^2 *)
completeTheSquare[i^2 + j^2 + k^2 - 2 i - 4 j - 6 k - 11]
(* -25+(-1+i)^2+(-2+j)^2+(-3+k)^2 *)