-
Notifications
You must be signed in to change notification settings - Fork 0
/
marked_multiplier_cover.py
executable file
·373 lines (289 loc) · 9.69 KB
/
marked_multiplier_cover.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#! /usr/bin/env python
import numpy as np
from lamination import Lamination
from collections import namedtuple
import sys
import networkx as nx
import matplotlib.pyplot as plt
from math import sin, cos, pi
from cmath import exp
_Face = namedtuple("Face", ["vertices", "degree"])
class Face(_Face):
def edges(self):
return list(zip(
self.vertices,
self.vertices[1:] + (self.vertices[0],)
))
def __len__(self):
return len(self.vertices)
class PlanarCycle:
def __init__(self, face,
*args,
**kwargs,
):
self.face = face
self.pos, self.v_labels = self.place(*args, **kwargs)
def place(self,
base_edge=(0, 1),
base_loc=0,
base_dir=-1j,
edge_length=1,
):
n = len(self.face.vertices)
t0, t1 = base_edge
r = 1/(2*sin(pi/edge_length))
if t0 == (t1-1) % n:
t0, t1 = t1, t0
# pos = {
# t: (x0 + r*cos(((t-t0)*2-1)*pi/n),
# y0 - r*sin(((t-t0)*2-1)*pi/n))
# for t,v in enumerate(self.face.vertices)
# }
pos = {}
v_labels = {}
dir = base_dir
loc = base_loc - dir*edge_length/2
for t in tuple(range(t0, n)) + tuple(range(t0)):
pos[t] = (loc.real, loc.imag)
dir *= exp(-2j*pi/n)
loc += dir
v_labels[t] = self.face.vertices[t]
return pos, v_labels
def __len__(self):
return len(self.face.vertices)
class Tessellation:
def __init__(faces=[], edges=[], vertices=[]):
self.faces = faces
self.edges = edges
self.vertices = vertices
def euler_characteristic(self):
chi = \
len(self.vertices) - \
len(self.edges) + \
len(self.faces)
# It had better be even!
assert chi % 2 == 0
return chi
def genus(self):
return 1 - self.euler_characteristic()//2
def face_sizes(self):
return [len(f.vertices) for f in self.faces.values()]
def num_odd_faces(self):
return len([s for s in self.face_sizes() if s % 2])
def show(self):
fig, ax = plt.subplots()
ax.set_aspect(1)
x = 0j
seen_real_edges = {}
for name, face in self.faces.items():
for i, edge in enumerate(face.edges):
a, b = edge.endpoints
if (a, b) in self.real_edges:
if (a, b) in seen_real_edges.keys():
base_face, base_edge = seen_real_edges[(a, b)]
else:
seen_real_edges[(a, b)] = (name, i)
base_face = None
base_edge = None
planar_face = PlanarCycle(face, base_loc=x)
self.draw_face(name, planar_face, fig, ax,
)
x += 2.5
plt.show()
def draw_face(self, name, planar_face, fig, ax,
):
pos, vertex_labels = planar_face.pos, planar_face.v_labels
n = len(planar_face)
G = nx.Graph()
G.add_nodes_from(range(n))
G.add_edges_from(
zip(tuple(range(n)),
tuple(range(1, n)) + (0,))
)
nx.draw_networkx_nodes(G, pos,
node_size=120,
node_color='#ffffff',
)
nx.draw_networkx_edges(G, pos)
nx.draw_networkx_labels(G, pos,
labels=vertex_labels,
font_size=10,
font_family='sans-serif')
class Edge:
def __init__(self, angles, cycle_classes, period, degree=2):
self.endpoints = cycle_classes
self.angles = angles
self.period = period
self.degree = degree
self.is_real = (sum(self.angles) == self.degree ** self.period - 1)
def __str__(self):
n = self.period
m = len(str(self.degree**self.period))
t = self.angles[0]
a, b = self.endpoints
return f"{t:>{n}b} = {a:>{m}d} -- {b:<{m}d}"
def __repr__(self):
a, b = self.endpoints
s, t = self.angles
return f"Edge ({a}, {b}) with representatives ({s}, {t})"
class MarkedMultCover(Lamination, Tessellation):
"""
Represents the combinatorics of the branched cover
(c,lambda) -> c
sending a unicritical polynomial f(z) = z^d + c
with marked n-cycle multiplier lambda to the
unmarked polynomial.
The branched cover is described by a tessellation of
a Riemann surface, whose vertices and faces
may be labeled according to the marked cycles.
`period`:
The period of the cycles whose multipliers
we follow
`degree`:
The degree d of the unicritical polynomial
f(z) = z^d + c.
TODO: UNTESTED FOR DEGREES GREATER THAN 2
"""
def __init__(self, period=4, degree=2):
period = int(period)
degree = int(degree)
super().__init__(period, degree)
self.period = period
self.max_angle = self.degree**self.period-1
if self.period == 1 or self.degree > 2:
raise NotImplementedError
# Leaves of period p in Multibrot lamination
self.ray_sets = sorted([
tuple(map(
lambda x: int(x*(self.max_angle)),
angles))
for angles in self.arcs_of_period(period)
])
# Map each angle to the minimum angle in its cycle
self.cycles = {
angle: min(cycle)
for angle in range(self.max_angle)
if len(cycle := self.orbit(angle)) == self.period
}
# Map each angle to the minimum angle in its cycle class
self.cycle_classes = {
angle: min(
self.cycles[angle],
self.cycles.get(self.max_angle - angle, 0))
for angle in self.cycles.keys()
}
# Leaves of lamination, labeled by minimum cycle representative
self.cycle_pairs = [
(
angles,
tuple(map(
lambda x: self.cycles[x],
angles))
)
for angles in self.ray_sets
]
# Vertices, labeled by minimum cycle representative
self.vertices = sorted(set(self.cycles.values()))
# Primitive leaves of lamination,
# labeled by minimum cycle representative
self.edges = [
Edge(t, (a, b),
period=self.period,
degree=self.degree,
)
for (t, (a, b)) in self.cycle_pairs
if a != b
]
# Faces, labeled by cycle class representative
self.faces = {
angle:
self.get_face(angle)
for angle in set(self.cycle_classes.values())
}
self.real_edges = {
endpoints
for edge in self.edges
for endpoints in [
edge.endpoints,
tuple(reversed(edge.endpoints))
]
if edge.is_real
}
def orbit(self, angle):
"""
Compute the orbit of an angle under
multiplication by the degree
"""
return {
roll(angle, self.period, dist, self.degree)
for dist in range(self.period)
}
def get_face(self, angle):
start = self.cycles[angle]
node = start
nodes = [node]
deg = 1
while True:
for edge in self.edges:
a, b = edge.endpoints
if node == a:
node = b
nodes.append(node)
elif node == b:
node = a
nodes.append(node)
if node == start:
nodes = tuple(nodes)
if len(nodes) > 1:
return Face(nodes[:-1], deg)
return Face(nodes, deg)
deg += 1
def summarize(self, indent=4):
indent_str = ' '*indent
print(f"\n{len(self.vertices)} vertices:")
n = self.period
m = len(str(self.degree**n))
for v in self.vertices:
print(f"{indent_str}{v:>{m}d}")
# print(f"{indent_str}{v:0>{n}b}")
print(f"\n{len(self.edges)} edges:")
for edge in self.edges:
# print(f"{indent_str}{a:0>{n}b} - {b:0>{n}b}")
print(f"{indent_str}{edge}")
print(f"\n{len(self.faces)} faces:")
for p, face in self.faces.items():
print(f"{indent_str}[{p:0>{n}b}] = {face}")
print("\nFace sizes:")
print(f"{indent_str}{self.face_sizes()}")
print(f"\nSmallest face: {min(self.face_sizes())}")
print(f"\nLargest face: {max(self.face_sizes())}")
print(f"\nGenus is {self.genus()}")
def binary_roll(x, period, dist=1):
max_angle = (1 << period) - 1
x %= max_angle
dist %= period
return ((x << dist) | (x >> (max_angle - dist))) % max_angle
def roll(x, period, dist=1, d=2):
if dist == 0:
return x
if d == 2:
return binary_roll(x, period, dist)
max_angle = (d ** period) - 1
x %= max_angle
dist %= period
left_shift = (x * d**dist) % max_angle
right_shift = x // (d**(max_angle - dist))
return left_shift + right_shift
if __name__ == "__main__":
try:
period = int(sys.argv[1])
except:
period = 7
print((
f"Computing combinatorics of "
f"(c,lambda) -> c cover "
f"for period {period}"
))
cov = MarkedMultCover(period)
cov.summarize()
# cov.show()