Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Compute a sum or other complexity measure over all atoms of an expression
ResourceFunction["LeafComplexity"][expr] gives the sum of all the numeric indivisible subexpressions in expr. | |
ResourceFunction["LeafComplexity"][expr, f] apply a function f to each indivisible subexpression in expr and take the Total. | |
ResourceFunction["LeafComplexity"][expr, f, g] apply a function f to each indivisible subexpression in expr and Apply to it recursively another unary or binary function g. |
| Heads | True | consider also the subexpression heads as leaves |
| "DecomposedHeads" | {Rational,Complex} | choose which special heads to decompose further |
| "SymbolWeights" | {e_?NumericQ⧴e,_Symbol→1} | ReplaceAll rule determining how to weigh each symbol |
| None | do not decompose any special head further |
| Rational | decompose rational numbers into NumeratorDenominator |
| Complex | decompose complex numbers into ReIm |
| Association | decompose an association via Normal |
| {head1,head2, …} | decompose a list of the above special heads |
Expressions with the same LeafCount may have different total over their leaves:
| In[1]:= |
| Out[1]= |
| In[2]:= |
| Out[2]= |
Apply a function to each leaf before taking the total:
| In[3]:= |
| Out[3]= |
Take the total over the leaves of a rational expression:
| In[4]:= |
| Out[4]= |
Sum over all leaves of an algebraic or complex expression:
| In[5]:= |
| Out[5]= |
| In[6]:= |
| Out[6]= |
Sum over non-integer leaves:
| In[7]:= |
| Out[7]= |
| In[8]:= |
| Out[8]= |
Sum over the leaves of any expression:
| In[9]:= |
| Out[9]= |
| In[10]:= |
| Out[10]= |
Apply an arbitrary function to each leaf:
| In[11]:= |
| Out[11]= |
| In[12]:= |
| Out[12]= |
Apply a custom wrapping function other than Plus to each leaf and function-of-the-leaf:
| In[13]:= |
| Out[13]= |
| In[14]:= |
| Out[14]= |
| In[15]:= |
| Out[15]= |
| In[16]:= |
| Out[16]= |
Through the option Heads set to False, only proper leaf nodes are counted (parent nodes count 0):
| In[17]:= |
| Out[17]= |
| In[18]:= |
| Out[18]= |
Another example with 4 discarded heads:
| In[19]:= |
| In[20]:= |
| Out[20]= |
| In[21]:= |
| Out[21]= |
Check the FullForm:
| In[22]:= |
| Out[22]= |
Do not decompose Rational or Complex heads:
| In[23]:= |
| Out[23]= |
| In[24]:= |
| Out[24]= |
Compare with the default:
| In[25]:= |
| Out[25]= |
| In[26]:= |
| Out[26]= |
Decompose alternatively only Rational or Complex heads:
| In[27]:= |
| Out[27]= |
| In[28]:= |
| Out[28]= |
Count only Values or consider the full Normal expression (list of key-value pairs) of an Association:
| In[29]:= |
| Out[29]= |
| In[30]:= |
| Out[30]= |
Personalize the weight of each Symbol leaf:
| In[31]:= |
| Out[31]= |
Compare with the default:
| In[32]:= |
| Out[32]= |
A more complex example:
| In[33]:= |
| Out[33]= |
| In[34]:= |
| Out[34]= |
Note that the input symbols like Exp and Sinh get automatically simplified into E and Csch and it is to these that weights should be assigned:
| In[35]:= |
| Out[35]= |
LeafComplexity can be used as option value for ComplexityFunction to Simplify algebraic expressions:
| In[36]:= |
| Out[36]= |
| In[37]:= |
| Out[37]= |
Another example with Log:
| In[38]:= |
| Out[38]= |
| In[39]:= |
| Out[39]= |
Select the "simplest" element of a list according to a custom function applied to each leaf:
| In[40]:= |
| Out[40]= |
| In[41]:= | ![]() |
| Out[41]= |
Certain functions like ResourceFunction["AlgebraicRange"] tend to overproduce complex closed forms:
| In[42]:= |
| In[43]:= |
| Out[43]= |
| In[44]:= |
| Out[44]= |
| In[45]:= |
| Out[45]= | ![]() |
LeafComplexity[expr] allows to select only the simpler ones:
| In[46]:= |
| In[47]:= |
| Out[47]= |
| In[48]:= |
| Out[48]= | ![]() |
The resulting numeric distribution can still be nearly uniform:
| In[49]:= |
| Out[49]= |
Sort a range of transcendental numbers by giving a different weight to each symbol:
| In[50]:= |
| Out[50]= |
| In[51]:= |
| Out[51]= |
This can speed up the search of closed forms:
| In[52]:= |
| Out[52]= |
| In[53]:= |
| In[54]:= |
| Out[54]= |
| In[55]:= |
| In[56]:= |
| Out[56]= |
Check the result found:
| In[57]:= |
| Out[57]= |
If all numeric leaves of an algebraic expression are equal to 1 or -1, LeafComplexity[expr] has the same value as LeafCount[expr]+1:
| In[58]:= |
| Out[58]= |
| In[59]:= |
| Out[59]= |
If no 0 appears in the expression, LeafComplexity[expr] is greater than LeafCount[expr]+1:
| In[60]:= |
| Out[60]= |
| In[61]:= |
| Out[61]= |
If 0s are present in the expression, LeafComplexity[expr] can be less than LeafCount[expr]+1:
| In[62]:= |
| Out[62]= |
| In[63]:= |
| Out[63]= |
If Infinity appears anywhere in the expression and does evaluate to 0, infinite is also the value of LeafComplexity:
| In[64]:= |
| Out[64]= |
| In[65]:= |
| Out[65]= |
LeafComplexity[expr] can be also computed in terms of Cases as follows:
| In[66]:= | ![]() |
| In[67]:= |
| In[68]:= |
| Out[68]= |
| In[69]:= |
| Out[69]= |
Notice though this is more involved and less efficient than the actual recursive definition.
The values of this complexity function turn out to match and generalize those of Factorial:
| In[70]:= |
| In[71]:= |
| Out[71]= |
| In[72]:= |
| Out[72]= |
| In[73]:= |
| Out[73]= | ![]() |
| In[74]:= |
| Out[74]= | ![]() |
Compared to Gamma, this involves shift relations more complex than just Gamma[x+1]==x Gamma[x], but it also remains remarkably discrete (with Integer rather than Real values).
Define a general LeafComplexity with doubly nested Log and Exp:
| In[75]:= |
Applying this to Range[n] for a few natural numbers n shows no Overflow and some interesting symbolic and numeric pattern:
| In[76]:= | ![]() |
| Out[76]= | ![]() |
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License