-
Notifications
You must be signed in to change notification settings - Fork 2
/
general.py
215 lines (170 loc) · 6.24 KB
/
general.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
from functools import reduce
from itertools import count
from operator import mul
from typing import Iterable
from matplotlib import pyplot as plt
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d
class Count(Iterable):
def __init__(self, start: int = 0, step: int = 1):
self.start = start
self.step = step
def __iter__(self):
return count(self.start, self.step)
def __getitem__(self, key):
if isinstance(key, int):
return self.start + key
elif isinstance(key, slice):
# Bounded, use range
if key.start is None:
start = self.start
else:
start = self.start + self.step * key.start
if key.stop is None:
stop = None
else:
stop = self.start + self.step * key.stop
if key.step is None:
step = self.step
else:
step = self.step * key.step
if stop is None:
# Unbounded, use Count
return Count(start, step)
else:
return range(start, stop, step)
else:
raise TypeError(f'Count indexes must be int or slice, not {type(key).__name__}')
def prod(iterable):
return reduce(mul, iterable, 1)
def floored_root(x, n):
"""Integer component of nth root of x.
Uses binary search to find the integer y such that y ** n <= x < (y + 1) ** n.
Adapted from https://stackoverflow.com/a/356206/1485877
"""
high = 1
while high ** n <= x:
high *= 2
low = high // 2
while low < high:
mid = (low + high) // 2
if low < mid and mid ** n < x:
low = mid
elif high > mid and mid ** n > x:
high = mid
else:
return mid
return mid + 1
def pairing_function_plot(points, arrows):
n = 4
fig, ax = plt.subplots(figsize=(3, 3))
fig.tight_layout()
fig.patch.set_color('black')
x_ticks = list(range(n))
y_ticks = list(range(n))
ax.scatter(*zip(*points), linestyle="None", marker='o', color='yellow')
for i, (x, y) in enumerate(points):
offset = 0.06
ax.annotate(i, (x + offset, y + offset), color='white')
for i, arrow in enumerate(arrows):
x1 = arrow[0]
y1 = arrow[1]
x2 = arrow[2]
y2 = arrow[3]
additional = arrow[4] if len(arrow) >= 5 else {}
ax.add_patch(FancyArrowPatch((x1, y1), (x2, y2), **additional, shrinkA=7, shrinkB=7, color='white',
arrowstyle='simple,head_width=4,head_length=8'))
ax.set_aspect('equal')
ax.patch.set_alpha(0)
ax.xaxis.set_ticks(x_ticks)
ax.yaxis.set_ticks(y_ticks)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.xaxis.label.set_color('white')
ax.yaxis.label.set_color('white')
ax.spines['bottom'].set_color('white')
ax.spines['top'].set_color('white')
ax.spines['right'].set_color('white')
ax.spines['left'].set_color('white')
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
margin = 0.25
ax.axis([-margin, n - 1 + margin + 0.2, -margin, n - 1 + margin + 0.1])
return fig
def three_dimensional_plot(points, arrows):
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
n = 3
fig = plt.figure(figsize=(5, 5))
ax = Axes3D(fig)
ax.view_init(25, 30)
ax.grid(False)
fig.patch.set_color('black')
x_ticks = list(range(n))
y_ticks = list(range(n))
z_ticks = list(range(n))
ax.scatter(*zip(*points), linestyle='None', marker='o', color='yellow')
for i, (x, y, z) in enumerate(points):
offset = 0.1
ax.text(x + offset, y + offset, z + offset, i, color='white')
for i, arrow in enumerate(arrows):
x1 = arrow[0]
y1 = arrow[1]
z1 = arrow[2]
x2 = arrow[3]
y2 = arrow[4]
z2 = arrow[5]
additional = arrow[6] if len(arrow) >= 7 else {}
ax.add_artist(Arrow3D((x1, x2), (y1, y2), (z1, z2), **additional, shrinkA=7, shrinkB=7, color='white',
arrowstyle='simple,head_width=4,head_length=8'))
ax.patch.set_alpha(0)
ax.xaxis.set_ticks(x_ticks)
ax.yaxis.set_ticks(y_ticks)
ax.zaxis.set_ticks(z_ticks)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.w_xaxis.line.set_color('white')
ax.w_yaxis.line.set_color('white')
ax.w_zaxis.line.set_color('white')
ax.w_zaxis.line.set_color('white')
ax.xaxis.label.set_color('white')
ax.yaxis.label.set_color('white')
ax.zaxis.label.set_color('white')
ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False
ax.xaxis.pane.set_edgecolor('w')
ax.yaxis.pane.set_edgecolor('w')
ax.zaxis.pane.set_edgecolor('w')
ax.xaxis._axinfo['tick']['color'] = 'w'
ax.yaxis._axinfo['tick']['color'] = 'w'
ax.zaxis._axinfo['tick']['color'] = 'w'
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
ax.tick_params(axis='z', colors='white')
return fig
def assert_consistency(product, pairing, unpairing):
for i, (x, y) in zip(range(100), product(Count(), Count())):
print(f'{i} == {pairing(x, y)}, {[x, y]} == {unpairing(i)}')
assert i == pairing(x, y)
assert [x, y] == unpairing(i)
def assert_multidimensional_consistency(product, pairing, unpairing):
dims = 4
for dim in range(1, dims+1):
iterables = [Count()] * dim
for i, elements in zip(range(100), product(*iterables)):
paired = pairing(*elements)
unpaired = unpairing(dim, i)
print(f'{i} == {paired}, {elements} == {unpaired}')
assert i == paired
assert elements == unpaired
class Arrow3D(FancyArrowPatch):
def __init__(self, xs, ys, zs, *args, **kwargs):
FancyArrowPatch.__init__(self, (0, 0), (0, 0), *args, **kwargs)
self._verts3d = xs, ys, zs
def draw(self, renderer):
xs3d, ys3d, zs3d = self._verts3d
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
self.set_positions((xs[0], ys[0]), (xs[1], ys[1]))
FancyArrowPatch.draw(self, renderer)