LeafCount¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Examples¶
All examples below are verified against the current Mathilda build.
In[1]:= LeafCount[1 + a + b^2]
Out[1]= 6
In[2]:= LeafCount[f[a, b][x, y]]
Out[2]= 5
In[3]:= LeafCount[{1/2, 1 + I}]
Out[3]= 7
Implementation notes¶
builtin_leafcount (src/core.c) returns leaf_count_internal, which counts 1 per non-EXPR_FUNCTION (atomic) node and recurses into function arguments. By default heads are counted too; the option Heads -> False suppresses head counting.
Protected.- Counts the number of subexpressions in
exprthat correspond to "leaves" on the expression tree. - By default
Heads -> Trueincludes the head of expressions and their parts. WithHeads -> False, it excludes them. - Evaluates atoms like
RationalandComplexbased on their structural representation as functions.
Attributes: Protected.
Implementation status¶
Stable — documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
References¶
- Source:
src/core.c - Specification:
docs/spec/builtins/expression-information.md
Notes & additional examples¶
Worked examples¶
It counts every atomic subexpression, including operator heads:
A handy proxy for symbolic "size" — here the blow-up of an expanded binomial:
Measuring the complexity of a computed result, e.g. an antiderivative:
Mapped over a list, it ranks expressions by structural weight:
Notes¶
LeafCount[expr] gives the total number of indivisible subexpressions (leaves)
in expr, counting heads and structural atoms. It is the standard measure used
by Simplify and friends to decide which of two candidate forms is "smaller".