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.$$

I had try:

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?

  • $\begingroup$ Just for reference, this feels a bit related to this question. $\endgroup$ – Thies Heidecke Aug 21 '19 at 13:58
  • $\begingroup$ Possible duplicate of Defining a function that completes the square given a quadratic polynomial expression $\endgroup$ – march Aug 21 '19 at 16:16
  • $\begingroup$ 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. $\endgroup$ – march Aug 21 '19 at 16:18
  • $\begingroup$ 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. $\endgroup$ – rhermans Aug 21 '19 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[#]) &)

enter image description here

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

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

(-1 + b^2/a^2)^2

enter image description here

Or based on the answer by Ulrich Neumann, also

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

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


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

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

enter image description here


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

 (-1 + b^2/a^2)^2 /. b -> bda a
 , TraditionalOrder -> True

enter image description here

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];
   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 *)
  • $\begingroup$ 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? $\endgroup$ – shelure21 Aug 21 '19 at 22:24
  • $\begingroup$ @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. $\endgroup$ – rhermans Aug 21 '19 at 22:29
  • 1
    $\begingroup$ Nice, exactly the answers that I want. $\endgroup$ – shelure21 Aug 21 '19 at 23:21


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.

  • 2
    $\begingroup$ It also works without Factor (Mma V12, Windows). $\endgroup$ – Thies Heidecke Aug 21 '19 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.