I am trying to simplify expressions such as
poly = 1 + (-2 + 4 a^2) x^2 + x^4
to the form
polySimple = (1-x^2)^2 + 4 a^2 x^2
Applying Simplify to poly
does not give polySimple
, even when I define
f[e_] := StringLength[ToString[InputForm[e]]]
and then try
Simplify[poly, ComplexityFunction -> f]
Note that f[poly]=26
, while f[polySimple]=23
, so I would have thought the above would have worked. Any ideas on how to get polySimple
?
Collect[poly,a,Simplify]
. The behaviour ofSimplify
is quite mysterious to me. For example, the intermediate result1 - 2 x^2 + 4 a^2 x^2 + x^4
has complexity27
, so we appendf[1-2 x^2+4 a^2 x^2+x^4]=25
by hand, but the result is still notpolySimple
. I guess that there are more involved things like attributes and inner definitions influcing the intermediate steps. $\endgroup$f[e_]:=(Sow[e];StringLength[ToString[InputForm[e]]])
and thenDeleteDuplicates[ClearSystemCache[];Reap[Simplify[1+(-2+4*a^2)*x^2+x^4,ComplexityFunction->f]][[2,1]]]
. If I interpret the result correctly, the expression that you are suggesting is never tried. It makes sense that not all expressions are tried. You could perhaps have it generate more expressions usingTransformationFunctions -> ...
. Maybe someone more knowledgeable will tell us. $\endgroup$f[e_]:=StringLength[ToString[InputForm[e]]]; t[e_]:=Collect[e,a,Simplify]; Simplify[poly,ComplexityFunction->f,TransformationFunctions->{Automatic,t}]
. $\endgroup$