Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Documentation on ComplexityFunction says:

With the default setting ComplexityFunction->Automatic, forms are ranked primarily according to their LeafCount, 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?


share|improve this question
You could use this as one of your test cases. And then explain to me what's going on :) –  Rojo May 30 '13 at 22:42
Do you mean that you want to know the Complexity as measured by Mathematica of a function? Can you define your own using LeafCount and integer sizes or does it need to be precisely that as defined by Mathematica? –  Jonathan Shock May 30 '13 at 23:16
The code for the automatic complexity function is listed in the help file for ComplexityFunction reference.wolfram.com/mathematica/ref/ComplexityFunction.html It is the first item under Properties and Relations. –  bill s May 31 '13 at 5:26
@bills nice find. –  rcollyer May 31 '13 at 12:16

2 Answers 2

up vote 13 down vote accepted

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.

share|improve this answer
The automatic function is also provided in the last example of the ComplexityFunction help page. –  Sjoerd C. de Vries May 31 '13 at 5:27

I ended up with this code:

NumberComplexity[x_Integer] := IntegerLength[x];
NumberComplexity[x_Real] := Round[Precision[x]];
NumberComplexity[x_Rational] := NumberComplexity[Numerator[x]] + 
NumberComplexity[x_Complex] := NumberComplexity[Re[x]] + NumberComplexity[Im[x]];
MyComplexityFunction[x_] := LeafCount[x] + 
    Total[NumberComplexity /@ Cases[x, _Integer | _Real | _Rational | _Complex, {0, ∞}]];

Please suggest improvements.

share|improve this answer
You might want to be careful of the Hold attributes of these functions. When you put in, for instance 4000/2000, Mathematica will automatically convert this to 2 before calculating the complexity of the expression in its original form. Try using Trace to see precisely what is happening. When you ask for improvements, it also depends what you are looking for. You haven't specified precisely what you want so it's hard to recommend improvements. Finally, I would recommend using lower cases for your function names as this avoids accidentally chosing Mathematica's built in functions. –  Jonathan Shock May 31 '13 at 0:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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