Maxima Function
tree_reduce (F, s)
tree_reduce(F,s,s_0)
Extends the binary function F to an n-ary function by composition, where s is a set or list.
tree_reduce
is equivalent to the following:
Apply F to successive pairs of elements
to form a new list [F(s_1, s_2), F(s_3, s_4), ...]
,
carrying the final element unchanged if there are an odd number of elements.
Then repeat until the list is reduced to a single element, which is the return value.
When the optional argument s_0 is present,
the result is equivalent tree_reduce(F, cons(s_0, s)
.
For addition of floating point numbers,
tree_reduce
may return a sum that has a smaller rounding error
than either rreduce
or lreduce
.
The elements of s and the partial results may be arranged in a minimum-depth binary tree, thus the name "tree_reduce".
Examples:
tree_reduce
applied to a list with an even number of elements.
(%i1) tree_reduce (f, [a, b, c, d]); (%o1) f(f(a, b), f(c, d))
tree_reduce
applied to a list with an odd number of elements.
(%i1) tree_reduce (f, [a, b, c, d, e]); (%o1) f(f(f(a, b), f(c, d)), e)