7

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?

A.G.
  • 4,362
  • 13
  • 18
shelure21
  • 165
  • 5

3 Answers3

7

Try

Simplify[1 - (2 b^2)/a^2 + b^4/a^4 /. a ->  b/λ] /. λ -> b/a
(*(-1 + b^2/a^2)^2*)
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Ulrich Neumann
  • 53,729
  • 2
  • 23
  • 55
7

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

Simplify[
 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];
  ReplaceAll[
   Simplify@ReplaceAll[expr, vars[[1]] -> vars[[2]]/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 *)

enter image description here

or

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

PolynomialForm[
 (-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];
  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 *)
rhermans
  • 36,518
  • 4
  • 57
  • 149
  • 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? – shelure21 Aug 21 '19 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. – rhermans Aug 21 '19 at 22:29
  • 1
    Nice, exactly the answers that I want. – shelure21 Aug 21 '19 at 23:21
5

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.

A.G.
  • 4,362
  • 13
  • 18