-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_dict.sage
274 lines (189 loc) · 6.93 KB
/
create_dict.sage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
'''
--------------------------------------------------------------------------------
First we have functions that create dictionaries based on a True or False
property that is used to identify the permutations
--------------------------------------------------------------------------------
'''
def perms_sat_prop(N, prop):
'''
Create a dictionary, D, containing keys 1, 2, 3, ..., N. Each key points to
a list of permutations satisfying the property prop
'''
D = {}
for n in range(1, N+1):
D[n] = filter(prop, Permutations(n))
return D
def perms_sat_prop_w_complement(N, prop):
'''
Create a dictionary, D, containing keys 1, 2, 3, ..., N. Each key points to
a list of permutations satisfying the property prop. The dictionary E has
the same keys and they point to the complement.
'''
D, E = {}, {}
for n in range(1, N+1):
Dn, En = [], []
for perm in Permutations(n):
if prop(perm):
Dn.append(perm)
else:
En.append(perm)
D[n], E[n] = Dn, En
return D, E
def perms_sat_prop_w_complement_different_sizes(Ng, Nb, prop, verb = False):
'''
Create a dictionary, D, containing keys 1, 2, 3, ..., Ng. Each key points to
a list of permutations satisfying the property prop.
Create a dictionary, E, containing keys 1, 2, 3, ..., Nb. Each key points to
a list of permutations NOT satisfying the property prop.
'''
D, E = {}, {}
# Putting into both dictionaries
for n in range(1, min(Ng, Nb)+1):
Dn, En = [], []
for perm in Permutations(n):
if prop(perm):
Dn.append(perm)
else:
En.append(perm)
D[n], E[n] = Dn, En
if verb:
print 'Done with length %s' %n
# Note that only one of the for-loops below will be non-empty
# Filling in the rest of D
for n in range(Nb+1, Ng+1):
Dn = []
for perm in Permutations(n):
if prop(perm):
Dn.append(perm)
D[n] = Dn
if verb:
print 'Done with length %s' %n
# Filling in the rest of E
for n in range(Ng+1, Nb+1):
En = []
for perm in Permutations(n):
if not prop(perm):
En.append(perm)
E[n] = En
if verb:
print 'Done with length %s' %n
return D, E
'''
--------------------------------------------------------------------------------
Next we have functions that create dictionaries D and E based on an image of a
map Sn -> Sn. The value of key n in D is the list of permutations of length n
in the image of Sn. Similarly, the value of key n in E is the list of
permutations of length n NOT in the image of Sn.
--------------------------------------------------------------------------------
'''
def perms_in_image(Ng, func, verb=False):
D, E = {}, {}
for n in range(1, Ng+1):
L = sorted(list(Set(map(lambda x : Permutation(func(x)).rank(), Permutations(n)))))
D[n] = [Permutations(n)[ell] for ell in L]
E[n] = [Permutations(n)[ell] for ell in range(factorial(n)) if ell not in L]
if verb:
print 'Done with length %s' %n
return D, E
def perms_in_image_different_sizes(Ng, Nb, func, verb=False):
N_low, N_high = min(Ng, Nb), max(Ng, Nb)
D, E = perms_in_image(N_low, func, verb=verb)
if N_low == N_high:
return D, E
# Filling in the rest of D or E
for n in range(N_low + 1, N_high+1):
L = sorted(list(Set(map(lambda x : Permutation(func(x)).rank(), Permutations(n)))))
if Ng > Nb:
D[n] = [Permutations(n)[ell] for ell in L]
elif Ng < Nb:
E[n] = [Permutations(n)[ell] for ell in range(factorial(n)) if ell not in L]
if verb:
print 'Done with length %s' %n
return D, E
'''
--------------------------------------------------------------------------------
Next we have functions that create dictionaries based on an already existing
(or easily created) set of permutations
--------------------------------------------------------------------------------
'''
def perms_to_dicts(Ng, func, verb=False):
D, E = {}, {}
for n in range(1, Ng+1):
Dn, En = func(n), []
L = sorted(map(lambda x : x.rank(), Dn))
s = 0
for ell in L+[factorial(n)]:
En.extend([Permutations(n)[i] for i in range(s,ell)])
s = ell+1
D[n], E[n] = Dn, En
if verb:
print 'Done with length %s' %n
return D, E
def perms_to_dicts_different_sizes(Ng, Nb, func, verb=False):
D, E = {}, {}
# Putting into both dictionaries
for n in range(1, min(Ng, Nb)+1):
Dn, En = func(n), []
L = sorted(map(lambda x : x.rank(), Dn))
s = 0
for ell in L+[factorial(n)]:
En.extend([Permutations(n)[i] for i in range(s,ell)])
s = ell+1
D[n], E[n] = Dn, En
if verb:
print 'Done with length %s' %n
# Note that only one of the for-loops below will be non-empty
# Filling in the rest of D
for n in range(Nb+1, Ng+1):
D[n] = func(n)
if verb:
print 'Done with length %s' %n
# Filling in the rest of E
for n in range(Ng+1, Nb+1):
En = []
L = sorted(map(lambda x : x.rank(), func(n)))
s = 0
for ell in L+[factorial(n)]:
En.extend([Permutations(n)[i] for i in range(s,ell)])
s = ell+1
E[n] = En
if verb:
print 'Done with length %s' %n
return D, E
'''
--------------------------------------------------------------------------------
Next we have functions that create dictionaries based on a precomputed output
--------------------------------------------------------------------------------
'''
def load_precomputed(Ng, Nb, prop, verb=False):
load('../permutation-sets/examples/precomputed/'+prop+'.sage')
D, E = {}, {}
# Putting into both dictionaries
for n in range(1, min(Ng, Nb)+1):
Dn, En = eval(prop)[n], []
L = sorted(map(lambda x : Permutation(x).rank(), Dn))
s = 0
for ell in L+[factorial(n)]:
En.extend([Permutations(n)[i] for i in range(s,ell)])
s = ell+1
D[n], E[n] = Dn, En
if verb:
print 'Done with length %s' %n
# # Note that only one of the for-loops below will be non-empty
# Filling in the rest of D
for n in range(Nb+1, Ng+1):
D[n] = eval(prop)[n]
if verb:
print 'Done with length %s' %n
# Filling in the rest of E
for n in range(Ng+1, Nb+1):
En = []
L = sorted(map(lambda x : Permutation(x).rank(), eval(prop)[n]))
s = 0
for ell in L+[factorial(n)]:
En.extend([Permutations(n)[i] for i in range(s,ell)])
s = ell+1
E[n] = En
if verb:
print 'Done with length %s' %n
return D, E