forked from defeo/ss-isogeny-software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.py
296 lines (247 loc) · 8.35 KB
/
paths.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
# Copyright (c) 2011-2016 Luca De Feo.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import math
class Path(object):
r""" A path represents what is called a "strategy" in the
paper. In practice, it is a tree-like structure looking like this:
/\
/\ \
/ / \
/\ \ /\
"""
def __init__(self, path):
r"""
To create a path,
>>> Path(list_of_integers)
where `list_of_integers` is a list coding each level of the
tree on the bits of an integer. This constructor is meant for
internal use.
Caveat: the bits of the integers, read from right to left,
determine the edges of the path from left to right.
"""
n = 4
for floor in path:
if floor >= n:
raise ValueError("Malformed path")
n <<= 2
self.path = path
def __repr__(self):
"ASCII-art representation of the path."
n = len(self.path)
s = '\n'
for i, floor in enumerate(self.path):
s += (n - i - 1) * ' '
for j in range(i+1):
edges = floor & 3
if edges == 0:
s += " `"
elif edges == 1:
s += "/ "
elif edges == 2:
s += " \\"
else:
s += "/\\"
floor >>= 2
s += (n - i - 1) * ' ' + '\n'
return s
def __hash__(self):
return hash(sum(f<<(2*i) for i,f in enumerate(self.path)))
def __eq__(self, other):
return self.path == other.path
def height(self):
"""
The height of a path is the number of levels, i.e. one plus
the number of edges.
"""
return len(self.path) + 1
def count(self):
"""
Count the number of left and right edges in the path and
return it as a tuple.
"""
left = 0
right = 0
for floor in self.path:
while floor > 0:
left += floor & 1
floor >>= 1
right += floor & 1
floor >>= 1
return left, right
def crosses(self):
"""
Returns False if the path is non-crossing (in the sense of the
paper).
"""
for floor in self.path:
floor >>= 1
while floor > 0:
if floor & 3 == 3:
return True
floor >>= 2
return False
def well_formed(self):
"""
Returns True if the path is well-formed (in the sense of the
paper).
"""
reachable = 2**self.height() - 1
for floor in reversed(self.path):
i = 1
newreachable = 0
r = reachable & i
if r != (floor & 1):
return False
reachable ^= r
newreachable |= r
floor >>= 1
while floor > 0:
i <<= 1
edge = floor & 3
r = reachable & i
if r:
if edge == 1:
newreachable |= i >> 1
elif edge == 2:
newreachable |= i
else:
return False
elif edge != 0:
return False
reachable ^= r
floor >>= 2
if reachable > 0:
return False
reachable = newreachable
return True
def cat(self, floor):
"Add one level to the bottom of the path. For internal use."
if floor >= 4**self.height():
raise ValueError("Malformed path: floor %s is too large" % floor)
return Path(self.path + [floor])
def __mul__(self, other):
"""
Multiplication of two paths: construct the path with minimal
number of edges having `self` as left sub-path and `other` as
right sub-path.
"""
sh = self.height()
oh = other.height()
newpath = []
if sh >= oh:
h, H = oh, sh
else:
h, H = sh, oh
for i in range(h):
newpath.append(1 | (1 << (2*i + 1)))
lit = iter(self.path)
rit = iter(other.path)
for i in range(h, H):
if sh > oh:
newpath.append(lit.next() | (1 << (2*i + 1)))
else:
newpath.append(1 | (rit.next() << (2*sh)))
for l, r in zip(lit, rit):
newpath.append(l | (r << (2*sh)))
return Path(newpath)
def paths(n):
"""
Construct all paths of height `n`. Even non well-formed ones.
"""
if n <= 0:
raise ValueError
elif n == 1:
return [Path([])]
else:
subpaths = paths(n-1)
return [p.cat(j) for p in subpaths for j in range(4**(n-1))]
def wf_paths(reachable):
"""
Construct all well-formed paths satisfying a given condition.
The condition is as follows: all the paths have height equal to
the ceiling of log_2(`reachable` + 1). `reachable` is interpreted
as a bitfield, with 1 meaning that the corresponding leaf on the
floor of the path should be reachable from the root, 0 meaning the
opposite.
This function has been used to count well-formed paths and guess
the link with Gelfand-Zetlin polytopes.
"""
if reachable <= 0:
raise ValueError
elif reachable == 1:
return [Path([])]
else:
floors = [reachable & 1]
reachable >>= 1
left = 2; right = 4
while reachable > 1:
if reachable & 1:
floors = [f | left for f in floors] + [f | right for f in floors]
left <<= 2; right <<= 2
reachable >>= 1
floors = [f | left for f in floors]
paths = []
for f in floors:
paths.extend([p.cat(f) for p in wf_paths(_h4(f))])
return paths
def _h4(n):
"""
Utility function on bitfields.
"""
res = 0
i = 1
while n > 0:
if n & 3 != 0:
res |= i
i <<= 1
n >>= 2
return res
def optimal_paths(n, l, r, construct=True):
"""
Compute optimal paths (in the sense of the paper) of height up to
`n`, with the cost of a left (resp. right) edge being given by `l`
(resp. `r`).
If `construct` is True, the output is a list of pairs cost-path,
with `output[i]` holding the optimal pair of height `i`
(`output[0]` contains nothing interesting).
If `construct` is False, the output is a list of pairs of
integers. The first integer is the same cost as for the
`construct` case, the second integer is the height of the topmost
branching in the left branch of the optimal path (so that the
sub-path starting at that point is given by the optimal path of
that height).
"""
if n <= 0:
raise ValueError
else:
if l > r:
l, r = r, l
swapped = True
else:
swapped = False
opaths = [(0,0), (0,1)]
for i in range(2, n+1):
opaths.append(((i + 1)**2*(l+r), 0))
for j in range(1, i//2 + 1):
lscore = opaths[j][0]
rscore = opaths[i-j][0]
score = lscore + rscore + (i-j)*l + j*r
if score <= opaths[i][0]:
opaths[i] = (score, j)
if swapped:
opaths = [(x,i-j) for (i,(x,j)) in enumerate(opaths)]
if construct:
opaths[1] = (0, Path([]))
for i in range(2,len(opaths)):
j = opaths[i][1]
opaths[i] = (opaths[i][0], opaths[j][1] * opaths[i-j][1])
return opaths