@@ -21,121 +21,6 @@ AUTHORS:
21
21
22
22
EXAMPLES:
23
23
24
- .. TODO:: Merge in the following documentation by Florent:
25
-
26
- How to define a set using those classes ?
27
-
28
- Only two things are necessary to define a set using a
29
- :class:`RecursivelyEnumeratedSet` object ( the other classes being very similar) :
30
-
31
- .. MATH::
32
-
33
- \b egin{picture}( -300,0) ( 600,0)
34
- % R oot
35
- \p ut( 0,0) {\c ircle*{7} }
36
- \p ut( 0,10) {\m akebox( 0,10) {``\ ''}}
37
- % F irst Children
38
- \p ut( -150,-60) {\m akebox( 0,10) {``a''}}
39
- \p ut( 0,-60) {\m akebox( 0,10) {``b''}}
40
- \p ut( 150,-60) {\m akebox( 0,10) {``c''}}
41
- \m ultiput( -150,-70) ( 150,0) {3} {\c ircle*{7} }
42
- % S econd children
43
- \p ut( -200,-130) {\m akebox( 0,10) {``aa''}}
44
- \p ut( -150,-130) {\m akebox( 0,10) {``ab''}}
45
- \p ut( -100,-130) {\m akebox( 0,10) {``ac''}}
46
- \p ut( -50,-130) {\m akebox( 0,10) {``ba''}}
47
- \p ut( 0,-130) {\m akebox( 0,10) {``bb''}}
48
- \p ut( 50,-130) {\m akebox( 0,10) {``bc''}}
49
- \p ut( 100,-130) {\m akebox( 0,10) {``ca''}}
50
- \p ut( 150,-130) {\m akebox( 0,10) {``cb''}}
51
- \p ut( 200,-130) {\m akebox( 0,10) {``cc''}}
52
- \m ultiput( -200,-140) ( 50,0) {9} {\c ircle*{7} }
53
- % Le gend
54
- \p ut( 100,-5) {\m akebox( 0,10) [l ]{1) An initial element}}
55
- \p ut( -250,-5) {\m akebox( 0,10) [l ]{2) A function of an element enumerating}}
56
- \p ut( -235,-20) {\m akebox( 0,10) [l ]{its children ( if any) }}
57
- % A rrows
58
- \t hicklines
59
- \p ut( 0,-10) {\v ector( 0,-1) {30} }
60
- \p ut( -15,-5) {\v ector( -2,-1) {110} }
61
- \p ut( 15,-5) {\v ector( 2,-1) {110} }
62
- \m ultiput( -150,-80) ( 150,0) {3} {\v ector( 0,-1) {30} }
63
- \m ultiput( -160,-80) ( 150,0) {3} {\v ector( -1,-1) {30} }
64
- \m ultiput( -140,-80) ( 150,0) {3} {\v ector( 1,-1) {30} }
65
- \p ut( 90,0) {\v ector( -1,0) {70} }
66
- \p ut( -215,-30) {\v ector( 1,-1) {40} }
67
- \e nd{picture}
68
-
69
- For the previous example, the two necessary pieces of information are :
70
-
71
- - The initial element ``""``
72
-
73
- - The function ``lambda x : [x+letter for letter in ['a', 'b', 'c' ]``
74
-
75
- Err.. Well, this would actually describe an ** infinite** set, as such rules
76
- describes "all words" on 3 letters. Hence, it is a good idea to replace the
77
- function by
78
-
79
- ``lambda x : [x+letter for letter in ['a', 'b', 'c' ]] if len( x) < 2 else []``
80
-
81
- or even::
82
-
83
- sage: def children( x) :
84
- .... : if len( x) < 2:
85
- .... : for letter in ['a', 'b', 'c' ]:
86
- .... : yield x+ letter
87
-
88
- We can then create the :class:`RecursivelyEnumeratedSet` object with either ::
89
-
90
- sage: S = RecursivelyEnumeratedSet( ['' ],
91
- .... : lambda x: [x+letter for letter in ['a', 'b', 'c' ]]
92
- .... : if len( x) < 2 else [],
93
- .... : structure='forest', enumeration='depth',
94
- .... : category=FiniteEnumeratedSets( ))
95
- sage: S. list( )
96
- ['', 'a', 'aa', 'ab', 'ac', 'b', 'ba', 'bb', 'bc', 'c', 'ca', 'cb', 'cc' ]
97
-
98
- Or::
99
-
100
- sage: S = RecursivelyEnumeratedSet( ['' ], children,
101
- .... : structure='forest', enumeration='depth',
102
- .... : category=FiniteEnumeratedSets( ))
103
- sage: S. list( )
104
- ['', 'a', 'aa', 'ab', 'ac', 'b', 'ba', 'bb', 'bc', 'c', 'ca', 'cb', 'cc' ]
105
-
106
- Here is a little more involved example. We want to iterate through all
107
- permutations of a given set S. One solution is to take elements of S one by
108
- one an insert them at every positions. So a node of the generating tree
109
- contains two informations
110
-
111
- - the list ``lst`` of already inserted element;
112
- - the set ``st`` of the yet to be inserted element.
113
-
114
- We want to generate a permutation only if ``st`` is empty ( leaves on the
115
- tree) . Also suppose for the sake of the example, that instead of list we want
116
- to generate tuples. This selection of some nodes and final mapping of a
117
- function to the element is done by the ``post_process = f`` argument. The
118
- convention is that the generated elements are the ``s := f( n) ``, except when
119
- ``s`` not ``None`` when no element is generated at all. Here is the code::
120
-
121
- sage: def children(( lst, st)) :
122
- .... : st = set( st) # make a copy
123
- .... : if st:
124
- .... : el = st. pop( )
125
- .... : for i in range( 0, len( lst) + 1) :
126
- .... : yield ( lst[0:i ]+ [el ]+ lst[i: ], st)
127
- sage: list( children(( [1,2 ], {3,7,9})))
128
- [([9, 1, 2 ], {3, 7}), ( [1, 9, 2 ], {3, 7}) , ( [1, 2, 9 ], {3, 7}) ]
129
- sage: S = RecursivelyEnumeratedSet( [([ ], {1,3,6,8}) ],
130
- .... : children,
131
- .... : post_process = lambda ( l, s) : tuple( l) if not s else None,
132
- .... : structure='forest', enumeration='depth',
133
- .... : category=FiniteEnumeratedSets( ) )
134
- sage: S. list( )
135
- [(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) ]
136
- sage: S. cardinality( )
137
- 24
138
-
139
24
Forest structure
140
25
----------------
141
26
0 commit comments