-
Notifications
You must be signed in to change notification settings - Fork 0
/
fragment.py
351 lines (321 loc) · 9.22 KB
/
fragment.py
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env python
# -*- coding: utf-8 -*-
r"""
Code for working with Dyck paths.
"""
__all__ = [
'is_fragment',
'lvaluated_fragment',
'rvaluated_fragment',
'translated_fragment',
]
# ---------------------------------------------------------
import itertools as it
import operator
from path import *
from perm import *
# ---------------------------------------------------------
def normalize_root_product(roots):
r"""
Return a `sign, roots` pair where `sign` is ``\pm 1`` or 0
and `roots` is a sorted tuple of roots `(i, j)` with `i < j`.
>>> normalize_root_product([])
(1, ())
>>> normalize_root_product([(0, 1), (0, 2), (1, 2)])
(1, ((0, 1), (0, 2), (1, 2)))
>>> normalize_root_product([(0, 1), (0, 2), (2, 1)])
(-1, ((0, 1), (0, 2), (1, 2)))
>>> normalize_root_product([(0, 1), (2, 0), (2, 1)])
(1, ((0, 1), (0, 2), (1, 2)))
>>> normalize_root_product([(0, 1), (1, 2), (0, 2)])
(1, ((0, 1), (0, 2), (1, 2)))
>>> normalize_root_product([(0, 1), (0, 0), (1, 2)])
(0, ())
"""
sign = 1
result = []
for (i, j) in roots:
if i < j:
result.append((i, j))
elif i > j:
result.append((j, i))
sign = -sign
else:
return 0, ()
result.sort()
return sign, tuple(result)
def project_root_product(roots, fro, to):
r"""
Return a `sign, roots` pair `sign` is ``\pm 1`` or 0
and `roots` is obtained by replacing every instance
of `fro` by `to` in the given roots (and normalizing).
>>> project_root_product([(0, 1)], fro=1, to=3)
(1, ((0, 3),))
>>> project_root_product([(0, 1)], fro=2, to=3)
(1, ((0, 1),))
>>> project_root_product([(0, 1)], fro=3, to=1)
(1, ((0, 1),))
>>> project_root_product([(0, 1)], fro=0, to=3)
(-1, ((1, 3),))
>>> project_root_product([(0, 1)], fro=0, to=1)
(0, ())
"""
return normalize_root_product(
(i if i != fro else to,
j if j != fro else to)
for (i, j) in roots)
# ---------------------------------------------------------
def is_fragment(frag, path):
r"""
Check whether `frag` is a valid vector fragment.
A vector fragment is given as a dict of (blist->root_product).
The coefficient for a missing blist is assumed to be zero or don't-care.
The fragment is valid if the coefficients for the given
blists satisfy the divisibility conditions associated with `path`
with all blists that are *below* them.
>>> test_is_fragment()
"""
boxes = boxes_under_path(path)
for bl in frag:
if not is_blist(bl): return False
for i, j in it.combinations(range(len(bl)), 2):
if bl[i] > bl[j] and (bl[j], bl[i]) in boxes:
tmp = list(bl)
tmp[i], tmp[j] = tmp[j], tmp[i]
tmp = tuple(tmp)
root_prod_above = frag[bl]
root_prod_below = frag.get(tmp, ((0, 0),))
if (project_root_product(root_prod_above, fro=bl[i], to=bl[j]) !=
project_root_product(root_prod_below, fro=bl[i], to=bl[j])):
return False
return True
def prod_lperm(lp1, lp2):
r"""
Return the composition of the permutations of L variables `lp1` and `lp2`.
>>> lp1 = (1, 0, 2, 3, 4)
>>> lp2 = (1, 2, 3, 4, 0)
>>> prod_lperm(lp1, lp2)
(2, 1, 3, 4, 0)
>>> prod_lperm(lp2, lp1)
(0, 2, 3, 4, 1)
"""
n = len(lp1)
assert n == len(lp2)
assert is_blist(lp1)
assert is_blist(lp2)
return tuple(lp2[lp1[i]] for i in range(n))
def translated_fragment(lperm, frag):
r"""
Return the result of acting by the permutation of L variables `lperm`
on the vector fragment `frag`.
Note that the result may not be valid according to `is_fragment`,
but this may be fine anyway.
>>> lp1 = (1, 0, 2)
>>> lp2 = (1, 2, 0)
>>> frag = {
... (0, 1, 2): (),
... (1, 0, 2): ((0, 1),),
... (0, 2, 1): ((0, 1), (0, 1)),
... (1, 2, 0): ((0, 1), (0, 1), (0, 1)),
... }
>>> translated_fragment(lp1, frag) == {
... (1, 0, 2): (),
... (0, 1, 2): ((0, 1),),
... (2, 0, 1): ((0, 1), (0, 1)),
... (2, 1, 0): ((0, 1), (0, 1), (0, 1)),
... }
True
>>> translated_fragment(lp2, frag) == {
... (1, 2, 0): (),
... (0, 2, 1): ((0, 1),),
... (2, 1, 0): ((0, 1), (0, 1)),
... (2, 0, 1): ((0, 1), (0, 1), (0, 1)),
... }
True
"""
result = {}
for bl in frag:
tmp = tuple(bl[lperm[i]] for i in range(len(lperm)))
result[tmp] = frag[bl]
return result
def lvaluated_fragment(frag):
r"""
Return the result of evaluating all coordinates of `frag` at ``L_i = i``.
>>> frag = {
... (0, 1, 2): ((0, 1), (0, 2)),
... (1, 0, 2): ((0, 1), (0, 2)),
... (0, 2, 1): ((0, 1), (0, 2)),
... (1, 2, 0): ((0, 1), (0, 2)),
... (2, 0, 1): ((0, 1), (0, 2)),
... (2, 1, 0): ((0, 1), (0, 2)),
... }
>>> lvaluated_fragment(frag) == {
... (0, 1, 2): 2,
... (1, 0, 2): -1,
... (0, 2, 1): 2,
... (1, 2, 0): 2,
... (2, 0, 1): -1,
... (2, 1, 0): 2,
... }
True
"""
result = {}
for bl, coeff in frag.iteritems():
result[bl] = reduce(
operator.mul,
[bl.index(i)-bl.index(j) for (i, j) in coeff],
1,
)
return result
def rvaluated_fragment(frag):
r"""
Return the result of evaluating all coordinates of `frag` at ``R_i = i``.
>>> frag = {
... (0, 1, 2): (),
... (1, 0, 2): ((0, 1), (0, 2)),
... (0, 2, 1): ((0, 2), (0, 2), (0, 2)),
... (1, 2, 0): ((0, 1), (1, 2)),
... (2, 0, 1): ((0, 2),),
... (2, 1, 0): ((0, 1), (0, 2), (0, 2), (0, 2), (0, 2)),
... }
>>> rvaluated_fragment(frag) == {
... (0, 1, 2): 1,
... (1, 0, 2): 2,
... (0, 2, 1): -8,
... (1, 2, 0): 1,
... (2, 0, 1): -2,
... (2, 1, 0): -16,
... }
True
"""
result = {}
for bl, coeff in frag.iteritems():
result[bl] = reduce(
operator.mul,
[i-j for (i, j) in coeff],
1,
)
return result
# ---------------------------------------------------------
def test_is_fragment():
r"""
Test the function `is_fragment` on various cases.
>>> test_is_fragment()
"""
paths = [
(0, 0, 0),
(0, 0, 1),
(0, 0, 2),
(0, 1, 1),
(0, 1, 2),
]
frags = [
{#0
(0, 1, 2): (),
(1, 0, 2): (),
(0, 2, 1): (),
(1, 2, 0): (),
(2, 0, 1): (),
(2, 1, 0): (),
},
{#1
(1, 0, 2): ((0, 1),),
(1, 2, 0): ((0, 1),),
(2, 0, 1): ((0, 2),),
(2, 1, 0): ((0, 2),),
},
{#2
(0, 2, 1): ((1, 2),),
(1, 2, 0): ((0, 2),),
(2, 0, 1): ((1, 2),),
(2, 1, 0): ((0, 2),),
},
{#3
(1, 2, 0): ((0, 1), (0, 2)),
(2, 1, 0): ((0, 1), (0, 2)),
},
{#4
(2, 0, 1): ((0, 2), (1, 2)),
(2, 1, 0): ((0, 2), (1, 2)),
},
{#5
(2, 1, 0): ((0, 1), (0, 2), (1, 2)),
},
{#6
(1, 2, 0): ((0, 1),),
(2, 1, 0): ((0, 1),),
},
{#7
(2, 0, 1): ((1, 2),),
(2, 1, 0): ((1, 2),),
},
{#8
(2, 1, 0): ((0, 1), (1, 2)),
},
{#9
(1, 0, 2): (),
},
{#10
(1, 2, 0): (),
},
{#11
(2, 1, 0): (),
},
{#12
(1, 2, 0): ((0, 1),),
(2, 1, 0): ((0, 0),),
},
]
assert ([is_fragment(frag, path) for frag in frags for path in paths] ==
[
1, 1, 1, 1, 1, #0
1, 1, 1, 1, 1, #1
1, 1, 1, 1, 1, #2
1, 1, 1, 1, 1, #3
1, 1, 1, 1, 1, #4
1, 1, 1, 1, 1, #5
0, 1, 1, 1, 1, #6
0, 1, 1, 1, 1, #7
0, 1, 1, 1, 1, #8
0, 0, 0, 1, 1, #9
0, 0, 0, 1, 1, #10
0, 0, 0, 0, 1, #11
0, 0, 1, 0, 1, #12
])
def test_associativity():
r"""
Test that `prod_lperm` and `translated_fragment` are associative
(as a group and a group action).
>>> test_associativity()
"""
perms = [
(0, 1, 2),
(1, 0, 2),
(0, 2, 1),
(1, 2, 0),
(2, 0, 1),
(2, 1, 0),
]
frag = {
(0, 1, 2): (),
(1, 0, 2): ((0, 1),),
(0, 2, 1): ((0, 1), (0, 1)),
(1, 2, 0): ((0, 1), (0, 1), (0, 1)),
}
assert all(
prod_lperm(x, prod_lperm(y, z)) == prod_lperm(prod_lperm(x, y), z)
for x in perms
for y in perms
for z in perms
)
assert all(
translated_fragment(x, translated_fragment(y, frag)) ==
translated_fragment(prod_lperm(x, y), frag)
for x in perms
for y in perms
)
# ---------------------------------------------------------
if __name__ == '__main__':
import doctest
doctest.testmod()
# ---------------------------------------------------------