Hi Simon,
> On 2012-10-10, Florent Hivert <
Florent...@lri.fr> wrote:
> > I'd like to gather proposal and find an agreement on the names for
> > SearchForest and post_process.
>
> I don't know if my answer to sage-devel will automatically appear here
> as well. If not: I suggested "RecursiveSet" and "branch_cut" as new
> names for SearchForest and post_process (though I am not so sure if
> "branch_cut" is the actual purpose of post_process).
Thanks for your suggestion. Actually "branch_but" doesn't describe what
"post_process" does. Cutting branch is done by children. The precise
description of the purpose of post_process is the following:
Through roots and children we are given a recursive tree of nodes n. From this
tree we want to derive a set S. First of all, often we need to apply a certain
function f(n) to compute the actual element of S. So in this sense we are just
applying a map f to the set of the node. On the other hand sometimes, all the
nodes are used to build element of S. Sometime only certain node are kept (eg
leaves), the other node are just intermediate steps of the recursion. In this
case f can return None to indicate that no element of s is associated to the
node n.
For example, suppose that you want to iterate through all permutations of a
given set S. One solution is to take element of S one by one an to insert them
at every position. So a node contains two informations
- the list lst of already inserted element
- the set st of the yet to be inserted element
You want to generate a permutation only if st is empty (leaves on the
tree). Also instead of list you may want to generate tuples. Here is the code:
sage: def children((lst, st)):
... st = set(st) # make a copy
... if st:
... el = st.pop()
... for i in range(0, len(lst)+1):
... yield (lst[0:i]+[el]+lst[i:], st)
sage: list(children(([1,2], {3,7,9})))
[([9, 1, 2], set([3, 7])), ([1, 9, 2], set([3, 7])), ([1, 2, 9], set([3, 7]))]
sage: S = SearchForest( [([], {1,3,6,8})],
... children,
... post_process = lambda (l, s): tuple(l) if not s else None,
... category=FiniteEnumeratedSets())
sage: S.list()
[(6, 3, 1, 8), (3, 6, 1, 8), (3, 1, 6, 8), (3, 1, 8, 6), (6, 1, 3, 8), (1, 6, 3, 8), (1, 3, 6, 8), (1, 3, 8, 6), (6, 1, 8, 3), (1, 6, 8, 3), (1, 8, 6, 3), (1, 8, 3, 6), (6, 3, 8, 1), (3, 6, 8, 1), (3, 8, 6, 1), (3, 8, 1, 6), (6, 8, 3, 1), (8, 6, 3, 1), (8, 3, 6, 1), (8, 3, 1, 6), (6, 8, 1, 3), (8, 6, 1, 3), (8, 1, 6, 3), (8, 1, 3, 6)]
sage: S.cardinality()
24
By the way, I'm adding this to the documentation ;-)
Nathann is suggesting to call post_process "map_function" because we just map
it on the node, dropping the None results.
I hope it is more clear,
Cheers,
Florent