# Factoring the square of this polynomial?

I have a polynomial $$\frac{b^4}{a^4}-\frac{2 b^2}{a^2}+1$$ and I want to complete the square to get this kind of expression: $$\left(1-\frac{b^2}{a^2}\right)^2.$$

Simplify[1 - (2 b^2)/a^2 + b^4/a^4] (*(a^2 - b^2)^2/a^4*)


and the

Simplify[1 - (2 b^2)/a^2 + b^4/a^4, ComplexityFunction -> LeafCount] (*(a^2 - b^2)^2/a^4*)

It seems that the option LeafCount for ComplexityFunction doesn't make sense here. However we know that

(1 - b^2/a^2)^2 // LeafCount (*12*)

is Less than that

(a^2 - b^2)^2/a^4 // LeafCount (*15*)


How can I achieve it? Is it possible to achieve it by Simplify?

• Just for reference, this feels a bit related to this question. Aug 21, 2019 at 13:58
• Aug 21, 2019 at 16:16
• In particular, using this answer, do depressPolynomial[1 - (2 b^2)/a^2 + b^4/a^4 /. b -> Sqrt[c], c] /. c -> b^2 or depressPolynomial[1 - (2 b^2)/a^2 + b^4/a^4 /. b -> Sqrt[c] a, c] /. c -> b^2/a^2. Aug 21, 2019 at 16:18
• My interpretation is that the OP explicitly asks "Is it possible to achieve it by Simplify", so the question doesn't ask for a function that completes squares, but a way to tell Simplify to find the desired form as optimal, by virtue of been minimal by LeafCount . Therefore IMO this is not a duplicate. Aug 21, 2019 at 16:38

## Explanation of the problem

The reason it never reaches the form you want despite of being minimal by LeafCount is that that form is never tried, so its LeafCount is never measured nor compared. Look at the attempts with a fresh kernel (as Simplify is cashed).

So the solution is to find a way for Simplify to explore the form you want using TransformationFunctions and score it as optimal as per your definition ComplexityFunction -> LeafCount.

{sol, {attempts}} = Reap@Simplify[
1 - (2 b^2)/a^2 + b^4/a^4
, ComplexityFunction -> ((Sow[#]; LeafCount[#]) &)
]; ## Solution requested: Simplify

We use TransformationFunctions to tell Simplify to explore the form you want, and ComplexityFunction to define how these alternatives are scored.

One way is to use the CompleteSquare described below

Simplify[
1 - (2 b^2)/a^2 + b^4/a^4
, TransformationFunctions -> {Automatic, CompleteSquare[#, b^2] &}
, ComplexityFunction -> LeafCount
]

(-1 + b^2/a^2)^2 Or based on the answer by Ulrich Neumann, also

transf[expr_] := Module[
{vars, tvar},
vars = Variables[expr];
ReplaceAll[
Simplify@ReplaceAll[expr, vars[] -> vars[]/tvar]
, tvar -> Divide @@ vars[[{2, 1}]]
]
]

Simplify[
1 - (2 b^2)/a^2 + b^4/a^4
, TransformationFunctions -> {Automatic, transf}
, ComplexityFunction -> LeafCount
]
(* (-1 + b^2/a^2)^2 *)


## PolynomialForm

The undocumented PolynomialForm allows placing the terms in the desired order with the option TraditionalOrder->True.

PolynomialForm[(-1+b^2/a^2)^2,TraditionalOrder->True]
(* (b^2/a^2-1)^2 *) or

Format[bda] = DisplayForm@FractionBox["b", "a"];

PolynomialForm[
(-1 + b^2/a^2)^2 /. b -> bda a
] ## Alternative Solution: CompleteSquare

Other people have suggested way to complete the square. You can force that form by

CompleteSquare[f_, x_] := Module[
{a, b, c},
{c, b, a} = CoefficientList[f, x];
Assuming[
Sqrt[a] > 0,
(FullSimplify[Sqrt[a] x] +
FullSimplify[b/(2 Sqrt[a])])^2 + (FullSimplify[(a c - b^2/4)])
]]

CompleteSquare[1 - (2 b^2)/a^2 + b^4/a^4, b^2]
(* (-1 + b^2/a^2)^2 *)

• So, it means that I should supply a method to simplify that could reach the form, and let the simplify to choose it. It means that I should know some methods previous? Aug 21, 2019 at 22:24
• @shelure21 It seems there are no general method implemented in Mathematica that do reach your form. The methods in all the answers are ad-hoc, tailored for a specific case because there seems to be no general way. So yes, you should know based on your case what operation could achieve the form you want and how to gauge it so that it scores optimally. Aug 21, 2019 at 22:29
• Nice, exactly the answers that I want. Aug 21, 2019 at 23:21

Try

Simplify[1 - (2 b^2)/a^2 + b^4/a^4 /. a ->  b/λ] /. λ -> b/a
(*(-1 + b^2/a^2)^2*)


You can do

Factor[1 - (2 b^2)/a^2 + b^4/a^4] // FullSimplify


or (as suggested by @Thies Heidecke)

FullSimplify[1 - (2 b^2)/a^2 + b^4/a^4]


with identical output: $$\frac{\left(a^2-b^2\right)^2}{a^4}$$

which is close.

• It also works without Factor (Mma V12, Windows). Aug 21, 2019 at 13:52