-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreegroup.py
250 lines (205 loc) · 5.4 KB
/
freegroup.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
from sage.all import FreeGroup, LaurentPolynomialRing, ZZ
def snappy_string_to_relator(s, F):
a,b = F.gens()
R = a*a.inverse()
for g in s:
if g == 'a':
R = R*a
elif g == 'A':
R = R*a.inverse()
elif g == 'b':
R = R*b
elif g == 'B':
R = R*b.inverse()
else:
raise Exception("Word must be in a and b and their inverses.")
return R
def change_coordinates(phi_a, phi_b):
G = FreeGroup(['t','u'])
t, u = G.gens()
F = FreeGroup(['a','b'])
a, b = F.gens()
g_a = a
g_b = b
nielsen_ops = []
if phi_a < 0:
g_a = g_a.inverse()
phi_a = -phi_a
nielsen_ops.append('t=t.inverse()')
if phi_a == 0:
if phi_b == 0:
raise Exception("phi is 0")
else:
return tuple(reversed(change_coordinates(phi_b, phi_a)))
if phi_b < 0:
g_b = g_b.inverse()
phi_b = -phi_b
nielsen_ops.append('u=u.inverse()')
while phi_b>0:
if phi_a>phi_b:
phi_a = phi_a-phi_b
g_a = g_a*g_b.inverse()
nielsen_ops.append('t = t*u')
else:
phi_b = phi_b-phi_a
g_b = g_b*g_a.inverse()
nielsen_ops.append('u = u*t')
for op in reversed(nielsen_ops):
exec(op)
return (t,u)
def abelianized(R, F):
a, b = F.gens()
h_a = 0
h_b = 0
S = R.syllables()
for s in S:
if s[0] == a:
h_a += s[1]
if s[0] == b:
h_b += s[1]
return (h_a, h_b)
def alexander_polynomial(R, F):
a, b = F.gens()
L = LaurentPolynomialRing(ZZ, ['t'])
t = L.gens()[0]
S = R.syllables()
h_a = 0
h_b = 0
for s in S:
if s[0] == a:
h_a += s[1]
if s[0] == b:
h_b += s[1]
new_a, new_b = change_coordinates(-h_b,h_a)
# print(new_a, new_b)
Rsub = R.subs(a=new_a, b=new_b)
height = 0
poly = t-t
# print(Rsub.Tietze())
for i in Rsub.Tietze():
# print(poly)
if i in (-1,1):
height += i
elif i == 2:
poly += t**height
else:
poly -= t**height
return poly
def ut_walk_just_a(R, F):
a, b = F.gens()
S = R.syllables()
h_a = 0
h_b = 0
for s in S:
if s[0] == a:
h_a += s[1]
if s[0] == b:
h_b += s[1]
new_a, new_b = change_coordinates(-h_b,h_a)
Rsub = new_a
# print(Rsub)
height = 0
heights = [0]
for i in Rsub.Tietze():
if i in (-1,1):
height += i
heights.append(height)
return heights
def u_heights(R, F):
a, b = F.gens()
S = R.syllables()
h_a = 0
h_b = 0
for s in S:
if s[0] == a:
h_a += s[1]
if s[0] == b:
h_b += s[1]
new_a, new_b = change_coordinates(-h_b,h_a)
Rsub = R.subs(a=new_a, b=new_b)
height = 0
heights = []
for i in Rsub.Tietze():
if i in (-1,1):
height += i
else:
if i>0:
heights.append((height, 1))
elif i<0:
heights.append((height,-1))
else:
raise Exception()
return heights
def ut_walk(R, F):
a, b = F.gens()
S = R.syllables()
h_a = 0
h_b = 0
for s in S:
if s[0] == a:
h_a += s[1]
if s[0] == b:
h_b += s[1]
new_a, new_b = change_coordinates(-h_b,h_a)
Rsub = R.subs(a=new_a, b=new_b)
# print(Rsub)
height = 0
heights = [0]
for i in Rsub.Tietze():
if i in (-1,1):
height += i
heights.append(height)
return heights
def ut_walk_with_orientations(R, F):
a, b = F.gens()
S = R.syllables()
h_a = 0
h_b = 0
for s in S:
if s[0] == a:
h_a += s[1]
if s[0] == b:
h_b += s[1]
new_a, new_b = change_coordinates(-h_b,h_a)
Rsub = R.subs(a=new_a, b=new_b)
return Rsub.Tietze()
def plot_walk(walk, filename):
import matplotlib.pyplot as plt
plt.clf()
plt.plot(range(len(walk)), walk)
plt.savefig(filename+'.png')
def plot_walk_with_orientations(tietze, filename):
import matplotlib.pyplot as plt
plt.clf()
height = 0
for i, step_type in enumerate(tietze):
if step_type == 1:
plt.plot((i, i+1), (height,height+1), color='black')
height += 1
elif step_type == -1:
plt.plot((i, i+1), (height,height-1), color='black')
height -= 1
elif step_type == 2:
plt.plot((i, i+1), (height,height),color = 'blue')
elif step_type == -2:
plt.plot((i, i+1), (height,height), color= 'red')
else:
raise Exception("Step type messed up.")
plt.savefig(filename+'.png')
def unique_max_and_min(walk):
max_height = max(walk)
min_height = min(walk)
max_count = 0
min_count = 0
for i in range(len(walk)-1):
height, next_height = walk[i], walk[i+1]
if height == next_height == max_height:
max_count += 1
elif height == next_height == min_height:
min_count += 1
return (max_count < 2) and (min_count < 2)
def relator_fibers(R, F):
return unique_max_and_min(ut_walk(R,F))
def monic_alexander_polynomial(R, F):
A = alexander_polynomial(R,F)
return A.coefficients()[-1] in (-1, 1)