Forest
Reference
3.14 Aggregate functions¶
Aggregate functions walk a nodewalk and use the information found in the visited nodes to calculate something.
All aggregate functions are available both as key handlers and pgfmath functions.
.count=⟨nodewalk⟩,(aggregate)¶
aggregate_count("⟨nodewalk⟩")
Store the number of nodes visited in the nodewalk into the handled option.
.sum=⟨forestmath⟩⟨nodewalk⟩,(aggregate)¶
aggregate_sum("⟨forestmath⟩","⟨nodewalk⟩")
.average=⟨forestmath⟩⟨nodewalk⟩,(aggregate)¶
aggregate_average("⟨forestmath⟩","⟨nodewalk⟩")
.product=⟨forestmath⟩⟨nodewalk⟩,(aggregate)¶
aggregate_product("⟨forestmath⟩","⟨nodewalk⟩")
.min=⟨forestmath⟩⟨nodewalk⟩,(aggregate)¶
aggregate_min("⟨forestmath⟩","⟨nodewalk⟩")
.max=⟨forestmath⟩⟨nodewalk⟩,(aggregate)¶
aggregate_max("⟨forestmath⟩","⟨nodewalk⟩")
Calculate the value of the given ⟨forestmath⟩ expression at each visited node. Store the sum / average / product / minimum / maximum of these values into the handled option (handlers) or return it (pgfmath functions).
.aggregate=⟨forestmath: start value⟩⟨forestmath: every step⟩⟨forestmath: after walk⟩⟨nodewalk⟩(aggregate)¶
aggregate("⟨forestmath: start value⟩","⟨forestmath: every step⟩","⟨forestmath: after walk⟩","⟨nodewalk⟩")(pgfmath function)¶
The generic aggregate function. First, register aggregate result is set to ⟨forestmath: start value⟩. Then, the given nodewalk is walked. After each step of the ⟨nodewalk⟩, ⟨forestmath: every step⟩ expression is evaluated in the context of the new current node and stored into aggregate result. After the walk, the current node is reset to the origin. ⟨forestmath: after walk⟩ expression is then evaluated in its context and stored into aggregate result as the final result.
Use aggregate result and aggregate n in the ⟨forestmath⟩ expressions to refer to the current result value and step number.
aggregate n=⟨count⟩(register)¶
the current step number
In the every-step expression of an aggregate function, refers to the (real) step number in the aggregate’s ⟨nodewalk⟩. In the after-walk expression, refers to the total number of (real) steps made.
aggregate result=⟨toks⟩(register)¶
the current value of the result
This register is where the intermediate results are stored.
aggregate value=⟨toks⟩(register)¶
the value of the expression at the current node
This only applies to special aggregates like .sum, not to the generic .aggregate.
aggregate postparse=none|int|print|macro¶
Roughly speaking, how should the result be formatted? For details, see [Tantau 2013, §89]. Applies only to pgfmath versions of aggregate functions, i.e. not to the ' variants.
none No formatting. int The result is an integer. print Use pgf’s number printing extension, see [Tantau 2013, §93]. macro Use a custom macro. Specify the macro using aggregate postparse macro=⟨cs⟩.
Example 1. Randomly generate the content of leaves. The content of a parent is the sum of its children’s content. Note how we use tree children-first to proceed in a bottom-up fashion.
\begin{forest}
delay={
aggregate postparse=int,
for tree children-first={
if n children=0
{content/.pgfmath={random(0,100)}}
{content/~.sum~={content}{children}}
}
}
[[[[][]][]][[][][[][][]]][[][[][]]]]
\end{forest}
Example 2: nested aggregate functions. We are given the black numbers. The inner aggregate, the sum of children, is applied at every blue node. (See how we actually display the blue numbers by storing aggregate value to content.) The outer aggregate stores the maximum blue number into the red root.
\begin{forest}
delay={
aggregate postparse=int,
content/~.max~=%
{~aggregate_sum~("content","children")}%
{every step={content/.register=~aggregate value~},children}%
} [ [[1][2][3]] [[3][4][5]] [[2][3][4]] ]
\end{forest}
Example 3: calculate root mean square of children using the generic .aggregate handler.
\begin{forest}
delay={
content/~.aggregate~=
{0}{~aggregate_result~()+content()^2}{sqrt(~aggregate_result~/~aggregate_n~)}
{children}
}
[[0][1][2][3][4]]
\end{forest}