Documentation on ComplexityFunction
says:
With the default setting
ComplexityFunction->Automatic
, forms are ranked primarily according to theirLeafCount
, with corrections to treat integers with more digits as more complex.
I need to use this default function on its own, not in Simplify
or FullSimplify
. Can I invoke it from my code?
If no, could you give me a custom function that behaves as close to the default function as possible?
Thanks!
Answer
The code for the default ComplexityFunction was posted on MathSource a number of years ago by Adam Strzebonski (of Wolfram Research). You will see reference to the original reply from Adam referenced in a MathGroup reply from Andrzej Kozlowski dated 12 Jan 2010 with the subject: "[mg106386] Re : Radicals simplify". I mention all that because I can't get the hyperlink to work (: The code Adam provided is there as well. The implementation from Adam used nested If statements. I can't resist the urge to use Which instead. I give my version below. I don't know for sure that the same function is used, but I have seen no reports indicating that it has changed.
SimplifyCount[p_]:=With[{hd=Head[p]},
Which[
hd===Symbol,1,
hd===Integer,If[p===0,1,Floor[N[Log[2,Abs[p]]/Log[2,10]]]+If[Positive[p],1,2]],
hd===Rational,SimplifyCount[Numerator[p]]+SimplifyCount[Denominator[p]]+1,
hd===Complex,SimplifyCount[Re[p]]+SimplifyCount[Im[p]]+1,
NumberQ[p],2,
True,SimplifyCount[Head[p]]+If[Length[p]==0,0,Plus@@(SimplifyCount/@(List@@p))]
]
]
Comments
Post a Comment