From 7fa4c14378534e98ee3aebf00e6c4be13642723a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Sat, 5 Aug 2023 18:23:25 +0200 Subject: [PATCH] fix and activate pycodestyle E225 in dev/ (#52) --- .github/workflows/lint.yml | 2 +- dev/DTcodes.py | 15 ++- dev/dev_jennet/spanning_trees.py | 3 +- dev/dev_jennet/test_links.py | 26 ++--- dev/dev_malik/fast_jones_patch.py | 3 +- dev/dev_malik/knots/random_knots.py | 7 +- dev/dev_malik/mutation/link_planar_isotopy.py | 6 +- dev/dev_malik/mutation/tangle_patch.py | 100 +++++++++--------- .../petal_diagram/random_petaluma.py | 2 +- dev/dev_malik/random_knot.py | 24 ++--- dev/dev_malik/thompson/graphknotter.py | 32 +++--- dev/dev_malik/thompson/thompson.py | 5 +- dev/old/braidclosure.py | 4 +- dev/old/seifert_patch.py | 18 ++-- dev/orthogonal/orthogonal.py | 24 ++--- dev/signature.py | 4 +- dev/tangle_patch.py | 31 +++--- 17 files changed, 162 insertions(+), 144 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 0c0f784..fd05a90 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -20,7 +20,7 @@ jobs: - name: Run pycodestyle shell: bash -l {0} # We currently only check for some warnings. We should enable & fix more of them. - run: pycodestyle --select=E111,E302,E306,E401,E701,E702,E703,E704,W391,W605,E711,E713,E721 spherogram_src/ + run: pycodestyle --select=E111,E225,E302,E306,E401,E701,E702,E703,E704,W391,W605,E711,E713,E721 spherogram_src/ - name: Run cython-lint shell: bash -l {0} run: cython-lint . diff --git a/dev/DTcodes.py b/dev/DTcodes.py index 006a733..500d8d0 100644 --- a/dev/DTcodes.py +++ b/dev/DTcodes.py @@ -101,21 +101,20 @@ def entry_slot(self, N): def exit_slot(self, N): if N == self[0]: return North - elif N == self[1]: + if N == self[1]: return West - else: - raise ValueError('%d is not a label of %s'% (N, self)) + raise ValueError('%d is not a label of %s' % (N, self)) def first_under(self): first, second, even_over = self if even_over: - return first-1 if first%2 == 1 else second-1 + return first-1 if first % 2 == 1 else second-1 else: - return first-1 if first%2 == 0 else second-1 + return first-1 if first % 2 == 0 else second-1 def upper_pair(self): first, second, even_over = self - return (0,2) if bool(first%2) ^ even_over else (1,3) + return (0,2) if bool(first % 2) ^ even_over else (1,3) class DTPath(): @@ -471,7 +470,7 @@ def sign(self, vertex): See the documentation for Spherogram.link. """ flipped = self.flipped(vertex) - even_first = bool(vertex[0] %2 == 0) + even_first = bool(vertex[0] % 2 == 0) return -1 if (flipped ^ vertex[2] ^ even_first) else 1 def KLP_strand(self, vertex, edge): @@ -481,7 +480,7 @@ def KLP_strand(self, vertex, edge): """ W = edge(vertex) slot = edge.slot(W) - return 'X' if (slot==0 or slot==2) ^ self.flipped(W) else 'Y' + return 'X' if (slot == 0 or slot == 2) ^ self.flipped(W) else 'Y' def KLP_dict(self, vertex, indices): """ diff --git a/dev/dev_jennet/spanning_trees.py b/dev/dev_jennet/spanning_trees.py index f0e4868..8d1f88d 100644 --- a/dev/dev_jennet/spanning_trees.py +++ b/dev/dev_jennet/spanning_trees.py @@ -21,6 +21,7 @@ def spanning_trees(G): else: return [] + def rec(part_G,G): """ Recursive function 'rec' from Read paper @@ -31,7 +32,7 @@ def rec(part_G,G): return [] if len(G.edges()) == len(part_G.edges()): - trees +=[part_G.copy()] + trees += [part_G.copy()] else: # let e be an edge not in part_G diff --git a/dev/dev_jennet/test_links.py b/dev/dev_jennet/test_links.py index 4bfcf38..6466b53 100644 --- a/dev/dev_jennet/test_links.py +++ b/dev/dev_jennet/test_links.py @@ -25,10 +25,10 @@ def setUp(self): # Hopf Link a = spherogram.Crossing('a') b = spherogram.Crossing('b') - a[0]=b[1] - a[1]=b[0] - a[2]=b[3] - a[3]=b[2] + a[0] = b[1] + a[1] = b[0] + a[2] = b[3] + a[3] = b[2] self.L2a1 = spherogram.Link([a,b]) #Borromean Link (3) @@ -142,7 +142,7 @@ def testConnectedSum(self): self.assertEqual(Sum.alexander_poly(), k1.alexander_poly()*k2.alexander_poly()) self.assertEqual(Sum.signature(), k1.signature()+k2.signature()) self.assertEqual(Sum.determinant(), k1.determinant()*k2.determinant()) - repeat -=1 + repeat -= 1 def testSignature(self): self.assertEqual(abs(self.Tref.signature()), 2) @@ -165,7 +165,7 @@ def testCopy(self): self.assertEqual(k1.writhe(), k1_prime.writhe()) self.assertEqual(k1.signature(), k1_prime.signature()) self.assertEqual(k1.alexander_poly(), k1_prime.alexander_poly()) - repeat-=1 + repeat -= 1 repeat = 3 while repeat > 0: @@ -175,16 +175,16 @@ def testCopy(self): self.assertEqual(k2.writhe(), k2_prime.writhe()) self.assertEqual(k2.signature(), k2_prime.signature()) self.assertEqual(k2.alexander_poly(), k2_prime.alexander_poly()) - repeat-=1 + repeat -= 1 def testMirror(self): repeat = 3 while repeat > 0: k1 = self.random_knot() k1_prime = k1.mirror() - self.assert_(k1.signature() == -1*k1_prime.signature(), msg="knot signature failed for "+ repr(k1)) - self.assert_(k1.writhe() == -1*k1_prime.writhe(), msg="knot writhe failed for "+repr(k1)) - repeat-=1 + self.assert_(k1.signature() == -1*k1_prime.signature(), msg="knot signature failed for " + repr(k1)) + self.assert_(k1.writhe() == -1*k1_prime.writhe(), msg="knot writhe failed for " + repr(k1)) + repeat -= 1 repeat = 3 while repeat > 0: @@ -192,7 +192,7 @@ def testMirror(self): k2_prime = k2.mirror() self.assert_(k2.signature() == -1*k2_prime.signature(), msg="link signature failed for " + repr(k2)) self.assert_(k2.writhe() == -1*k2_prime.writhe(), msg="link writhe failed for " + repr(k2)) - repeat-=1 + repeat -= 1 def testDet(self): self.assertEqual(self.K3_1.determinant(), 3) @@ -240,13 +240,13 @@ def testBlackGraph(self): while repeat > 0: k1 = self.random_knot() self.assert_(k1.black_graph().is_planar()) - repeat-=1 + repeat -= 1 repeat = 3 while repeat > 0: k2 = self.random_link() self.assert_(k2.black_graph().is_planar()) - repeat-=1 + repeat -= 1 suite = unittest.TestLoader().loadTestsFromTestCase(TestLinkFunctions) diff --git a/dev/dev_malik/fast_jones_patch.py b/dev/dev_malik/fast_jones_patch.py index c8e088a..1a1f78b 100644 --- a/dev/dev_malik/fast_jones_patch.py +++ b/dev/dev_malik/fast_jones_patch.py @@ -80,6 +80,7 @@ def simplify_jones_expression(poly): # print(poly) return poly + def simplify_monomial(monomial): monomial = remove_squares(monomial) monomial = remove_loops(monomial) @@ -87,7 +88,7 @@ def simplify_monomial(monomial): strand_vars = monomial.free_symbols - set(sympy.symbols('A,B')) strand_labels = all_labels(strand_vars) - while len(set(strand_labels)) 0: - found_nontrivial=True + found_nontrivial = True if prime_decomp: cant_deconnect = False while cant_deconnect: ds = knot.deconnect_sum() knot = max(ds,key=len) - cant_deconnect = (len(ds)>1) + cant_deconnect = (len(ds) > 1) return knot """ @@ -190,7 +190,7 @@ def isosig(self, root=None, over_or_under=False): for strand,end in strands: isst = map(lambda cs: crossing_order.index(cs[0]),strand) if (want_root) and (not found_root) and (root_position in isst): - root_strand_indices = [i for i in range(len(isst)) if isst[i]==root_position] + root_strand_indices = [i for i in range(len(isst)) if isst[i] == root_position] for root_strand_index in root_strand_indices: if root_tuple == strand[root_strand_index]: found_root = True @@ -202,7 +202,7 @@ def isosig(self, root=None, over_or_under=False): for loop in loops: isl = map(lambda cs: crossing_order.index(cs[0]),loop) if (want_root) and (not found_root) and (root_position in isl): - root_loop_indices = [i for i in range(len(isl)) if isl[i]==root_position] + root_loop_indices = [i for i in range(len(isl)) if isl[i] == root_position] for root_loop_index in root_loop_indices: if root_tuple == loop[root_loop_index]: found_root = True @@ -273,11 +273,11 @@ def crossing_orientations(strands): for cs in strand: for seen_cs in css_seen: if cs[0] == seen_cs[0]: - orientation = (cs[1]-seen_cs[1])%4 + orientation = (cs[1]-seen_cs[1]) % 4 if orientation == 3: orientation = -1 - orientations[cs[0]]=orientation - over_or_under[cs[0]] = (cs[1]%2) + orientations[cs[0]] = orientation + over_or_under[cs[0]] = (cs[1] % 2) break css_seen.append(cs) #didn't find cs return orientations, over_or_under @@ -357,16 +357,16 @@ def all_cross_strands(self): if loop_cs[0] in seen_once: for seen_cs in cs_seen: if loop_cs[0] == seen_cs[0]: - orientation = (loop_cs[1]-seen_cs[1])%4 + orientation = (loop_cs[1]-seen_cs[1]) % 4 if orientation == 3: orientation = -1 - orientations[loop_cs[0]]=orientation - over_or_under[loop_cs[0]] = loop_cs[1]%2 + orientations[loop_cs[0]] = orientation + over_or_under[loop_cs[0]] = loop_cs[1] % 2 seen_once.remove(loop_cs[0]) break else: seen_once.add(loop_cs[0]) - while len(orientations)0: + if len(Uc) > 0: return Uc return None + #Returns first nontrivial 4-cycle found def get_four_cycle(G, start_vertex): adjacent = G.children(start_vertex) for v in adjacent: for w in adjacent: - if v==w: + if v == w: continue new_adj = G.children(v).intersection(G.children(w)) new_adj.remove(start_vertex) @@ -567,11 +568,11 @@ def trace_boundary_component(start_cs,full_boundary): print(cs) cs = cs.next_corner() i += 1 - if i>100: + if i > 100: raise Exception() cs = cs.rotate(1) boundary_comp.append(cs) - boundary_comp.pop(-1) #code aboves adds the start_cs twice + boundary_comp.pop(-1) # code aboves adds the start_cs twice return boundary_comp @@ -602,22 +603,22 @@ def tangle_neighborhood(link,crossing,radius,return_gluings=True,hull=False): print('all comps: '+str(comps)) comps.remove(largest_comp) #remove largest component for comp in comps: - print('crossings: ' +str(crossings)) - print('filling in comp:'+str(comp)) - print('adjacent: '+str(adjacent)) + print('crossings: ' + str(crossings)) + print('filling in comp:' + str(comp)) + print('adjacent: ' + str(adjacent)) c = comp.pop() cs = choice(c.crossing_strands()) print('cs: ' + str(cs)) exit_strand = meander(cs,sides)[1] #meander until you hit boundary exit_strand = exit_strand[0].crossing_strands()[exit_strand[1]] - print('exit_strand: ' +str(exit_strand)) + print('exit_strand: ' + str(exit_strand)) bound_comp = trace_boundary_component(exit_strand,adjacent) print('traced component: ' + str(bound_comp)) if exit_strand not in main_boundary_comp: for x in bound_comp: adjacent.remove(x) - print('updated adjacent: ' +str(adjacent)) + print('updated adjacent: ' + str(adjacent)) crossings.append(c) crossings.extend(list(comp)) @@ -846,13 +847,13 @@ def fill_in_crossings(link,sides): crossing_sides = {x[0]:sides[x] for x in sides} crossing_labels = map(lambda c: c.label,link.crossings) crossings_to_sort = set(crossing_labels)-set(x[0] for x in sides) - while len(crossings_to_sort)>0: + while len(crossings_to_sort) > 0: start_crossing = crossings_to_sort.pop() accumulated_crossings = [start_crossing] m,end_side = meander(crossing_strand_from_name(link,(start_crossing,randint(0,3))),sides) accumulated_crossings.extend(map(lambda x: x.label,m)) for c in accumulated_crossings: - crossing_sides[c]=end_side + crossing_sides[c] = end_side return crossing_sides @@ -948,12 +949,13 @@ def hamilton(G): conf_g.remove_node(path[-1]) confs.append((conf_g,conf_p)) for g,p in confs: - if len(p)==n: + if len(p) == n: return p else: - F.append((g,p)) + F.append((g, p)) return None + def hamiltonian_cycle(G): return edge_cycle(hamilton(G.to_networkx()),G) @@ -1079,7 +1081,7 @@ def all_nhd_vol_dists(link,radius,tolerance): double_volumes = map(lambda x: x.exterior().volume(),T1.all_circular_sums(T1)) double_volumes.sort() for i in range(len(double_volumes)): - if double_volumes[i]j: + if i > j: ordered_or_backward = 1 if height_perm[i] < height_perm[j]: # strand i passes under strand j if not visited_dict[a,b]: #if first time crossing has come up diff --git a/dev/dev_malik/random_knot.py b/dev/dev_malik/random_knot.py index daa62d8..2564eb6 100644 --- a/dev/dev_malik/random_knot.py +++ b/dev/dev_malik/random_knot.py @@ -8,7 +8,7 @@ def start_string(): crossings = [Crossing('0')] - crossings[0][2]=crossings[0][3] + crossings[0][2] = crossings[0][3] final_cs = crossings[0].crossing_strands()[0] loose_cs = crossings[0].crossing_strands()[1] return crossings, loose_cs, final_cs @@ -22,7 +22,7 @@ def random_knot(n, method='close_under', alternate=False, return Link(crossings) elif method == 'close_on_opportunity': crossings = [Crossing('0')] - crossings[0][2]=crossings[0][3] + crossings[0][2] = crossings[0][3] final_cs = crossings[0].crossing_strands()[0] loose_cs = crossings[0].crossing_strands()[1] i = 0 @@ -47,7 +47,7 @@ def random_knot(n, method='close_under', alternate=False, def random_open_string(n, alternate=False, bias=False): crossings = [Crossing('0')] - crossings[0][2]=crossings[0][3] + crossings[0][2] = crossings[0][3] final_cs = crossings[0].crossing_strands()[0] loose_cs = crossings[0].crossing_strands()[1] for i in range(n): @@ -96,7 +96,7 @@ def distance(crossings, loose_cs, final_cs): def volume_evolution(n): crossings = [Crossing('0')] - crossings[0][2]=crossings[0][3] + crossings[0][2] = crossings[0][3] final_cs = crossings[0].crossing_strands()[0] loose_cs = crossings[0].crossing_strands()[1] vols = [] @@ -125,7 +125,7 @@ def open_string_volume(crossings): def alexander_evolution(n): crossings = [Crossing('0')] - crossings[0][2]=crossings[0][3] + crossings[0][2] = crossings[0][3] final_cs = crossings[0].crossing_strands()[0] loose_cs = crossings[0].crossing_strands()[1] alex_polys = [] @@ -143,7 +143,7 @@ def alexander_evolution(n): def function_evolution(n, function, simplify='level', skip_first=0): crossings = [Crossing('0')] - crossings[0][2]=crossings[0][3] + crossings[0][2] = crossings[0][3] final_cs = crossings[0].crossing_strands()[0] loose_cs = crossings[0].crossing_strands()[1] values = [] @@ -199,7 +199,7 @@ def trivial_jones_search(n, simplify='global', start_turn_list=[1]): for i in range(n): K = open_string_evaluation(crossings, lambda K: K, simplify) if K: - if len(K)>17: + if len(K) > 17: p = K.jones_polynomial() if p.is_one(): trivial_jones.append(K) @@ -252,7 +252,7 @@ def open_string_alexander(crossings): def open_string_from_turn_list(turn_list): crossings = [Crossing('0')] - crossings[0][2]=crossings[0][3] + crossings[0][2] = crossings[0][3] final_cs = crossings[0].crossing_strands()[0] loose_cs = crossings[0].crossing_strands()[1] for i, turn in enumerate(turn_list): @@ -271,7 +271,7 @@ def open_string_from_turn_list(turn_list): # print('available after') # print(available) loose_cs = cross_strand(crossings, loose_cs, - strand_to_cross, str(i+1), (turn>0)) + strand_to_cross, str(i+1), (turn > 0)) # print('crossings after') # [c.info() for c in crossings] @@ -279,7 +279,7 @@ def open_string_from_turn_list(turn_list): def knot_tree(depth): crossings = [Crossing('0')] - crossings[0][2]=crossings[0][3] + crossings[0][2] = crossings[0][3] final_cs = crossings[0].crossing_strands()[0] loose_cs = crossings[0].crossing_strands()[1] tree = nx.Graph() @@ -391,9 +391,9 @@ def hard_unknot_search(turn_list, max_steps): print(turn_list) K = open_string_evaluation(crossings, lambda x: x, simplify=None) K.simplify() - if len(K)>3: + if len(K) > 3: K.simplify(mode='global') - if len(K)>3: + if len(K) > 3: if K.alexander_polynomial().is_one(): print(turn_list) unknots.append(tuple(turn_list)) diff --git a/dev/dev_malik/thompson/graphknotter.py b/dev/dev_malik/thompson/graphknotter.py index 0dec229..74ddd73 100644 --- a/dev/dev_malik/thompson/graphknotter.py +++ b/dev/dev_malik/thompson/graphknotter.py @@ -14,7 +14,7 @@ def faces(G): unseen_pairs = { (edge,vertex) for edge in G.edges for vertex in edge.incident_to()} face_list = [] - while len(unseen_pairs)>0: + while len(unseen_pairs) > 0: direction = unseen_pairs.pop() face = [direction] @@ -56,7 +56,7 @@ def link_diagram(G): # print(c,o) # print('cnext,u: ') # print(cnext,u) - c[o]=cnext[u] + c[o] = cnext[u] for edge in G.edges: #switch twisted edges c = crossing_dict[label(edge)] @@ -155,7 +155,7 @@ def face_two_coloring(G,faces): for f in faces: for other_face in colors: if share_edge(f,other_face): - colors[tuple(f)]=1-colors[other_face] + colors[tuple(f)] = 1 - colors[other_face] faces.remove(f) break return colors @@ -163,11 +163,13 @@ def face_two_coloring(G,faces): def share_edge(face1,face2): s1 = set([e for e,v in face1]) s2 = set([e for e,v in face2]) - return len(s1.intersection(s2))>0 + return len(s1.intersection(s2)) > 0 + def edge_to_str_pair(edge): return set(map(lambda s: s.strip(), str(edge).split('---'))) + def doubled_edge_to_str_pair(e): s1 = set([e[0].split()[0].split('\'')[1], e[0].split()[1].split('\'')[1]]) s2 = set([e[1].split()[0].split('\'')[1], e[1].split()[1].split('\'')[1]]) @@ -190,10 +192,10 @@ def identify_pair(link): if len(face) == 4: signs = [(cs.strand_index % 2) for cs in face] comps = [component_index(link,cs) for cs in face] - if sorted(signs) == [0,0,1,1] and len(set(comps))>1: + if sorted(signs) == [0, 0, 1, 1] and len(set(comps)) > 1: second_zero = 3 - list(reversed(signs)).index(0) - if comps[second_zero] != comps[(second_zero+2)%4]: - return face[second_zero],face[(second_zero+2)%4] + if comps[second_zero] != comps[(second_zero+2) % 4]: + return face[second_zero],face[(second_zero+2) % 4] raise Exception() @@ -217,24 +219,25 @@ def component_index(link,cs): def whitehead_double(link): D = double(link) cs1, cs2 = identify_pair(D) - T1 = simple_cut(D,cs1,cs2) + T1 = simple_cut(D, cs1, cs2) T2 = clasp() clear_orientations(T1) clear_orientations(T2) - L = T1.circular_sum(T2,1) + L = T1.circular_sum(T2, 1) L._rebuild() return L + def make_writhe_zero(link): writhe = link.writhe() - if writhe>0: + if writhe > 0: hand = 'left' else: hand = 'right' writhe = abs(writhe) for i in range(writhe): cs = random.choice(link.crossing_strands()) - reverse_type_I(link,cs, i, hand, False) + reverse_type_I(link, cs, i, hand, False) link._rebuild() print(link.writhe()) @@ -242,14 +245,17 @@ def make_writhe_zero(link): def clear_orientations(link): for c in link.crossings: c._clear() + + def clasp(): c, d = spherogram.Crossing('c'),spherogram.Crossing('d') c[0] = d[1] c[1] = d[0] - d2, d3= d.crossing_strands()[2],d.crossing_strands()[3] - c2, c3= c.crossing_strands()[2],c.crossing_strands()[3] + d2, d3 = d.crossing_strands()[2],d.crossing_strands()[3] + c2, c3 = c.crossing_strands()[2],c.crossing_strands()[3] return Tangle(2,[c,d],[d2, d3, c3, c2]) + def random_four_connect(link1,link2): from random import choice, sample css1 = sample(choice(link1.faces()),2) diff --git a/dev/dev_malik/thompson/thompson.py b/dev/dev_malik/thompson/thompson.py index 60bcb10..6ff6e76 100644 --- a/dev/dev_malik/thompson/thompson.py +++ b/dev/dev_malik/thompson/thompson.py @@ -3,6 +3,7 @@ from spherogram.graphs import FatGraph from random import randint + class DyadicRational: def __init__(self, numerator, denom_exp): self.numerator = numerator @@ -19,7 +20,7 @@ def reduce(self): def children(self): l = DyadicRational(2*self.numerator-1, self.denom_exp+1) r = DyadicRational(2*self.numerator+1, self.denom_exp+1) - return (l,r) + return (l, r) def parent(self): if self.numerator % 4 == 1: @@ -28,7 +29,7 @@ def parent(self): return DyadicRational(self.numerator-1,self.denom_exp) def __repr__(self): - return '%s/2^%s' %(self.numerator,self.denom_exp) + return '%s/2^%s' % (self.numerator,self.denom_exp) def __eq__(self,otherdyadic): if self.numerator == 0: diff --git a/dev/old/braidclosure.py b/dev/old/braidclosure.py index bc36828..40dd247 100644 --- a/dev/old/braidclosure.py +++ b/dev/old/braidclosure.py @@ -39,9 +39,9 @@ def braid_closure_via_crossings(braid): if (not foundrightstrand): crossings[i][0] = crossings[j][1] foundrightstrand = True - j = (j+1)%l + j = (j + 1) % l for i in range(l): - if braid[i]>0: + if braid[i] > 0: crossings[i].rotate_by_90() return Link(crossings) diff --git a/dev/old/seifert_patch.py b/dev/old/seifert_patch.py index 2d675df..9dd69b9 100644 --- a/dev/old/seifert_patch.py +++ b/dev/old/seifert_patch.py @@ -65,7 +65,7 @@ def seifert_tree(link): under_circle = n if over in circle: over_circle = n - if under_circle>0 and over_circle>0: + if under_circle > 0 and over_circle > 0: break if sign == -1: connect_head_to_tail(edges[under_circle],edges[over_circle]) @@ -258,8 +258,10 @@ def seifert_matrix(link, return_type_matrix=False): else: return matrix -def cyclic_permute(l,n): - return [l[(i+n)%len(l)] for i in range(len(l))] + +def cyclic_permute(l, n): + return [l[(i + n) % len(l)] for i in range(len(l))] + def straighten_arrows(arrows): totally_straightened = False @@ -267,22 +269,22 @@ def straighten_arrows(arrows): totally_straightened = True for arrow in arrows: tail, head = arrow[0], arrow[1] - if tail < head: #need to move tail down + if tail < head: # need to move tail down diff = head-tail - same_start_strand = [x for x in arrows if x[2] == arrow[2] and x[0]>=tail] + same_start_strand = [x for x in arrows if x[2] == arrow[2] and x[0] >= tail] for other_arrow in same_start_strand: other_arrow[0] += diff - one_strand_behind = [x for x in arrows if x[2] == arrow[2]-1 and x[1]>=tail] + one_strand_behind = [x for x in arrows if x[2] == arrow[2]-1 and x[1] >= tail] for other_arrow in one_strand_behind: other_arrow[1] += diff totally_straightened = False elif head < tail: #need to move head down diff = tail-head - same_end_strand = [x for x in arrows if x[2] == arrow[2] and x[1]>=head] + same_end_strand = [x for x in arrows if x[2] == arrow[2] and x[1] >= head] for other_arrow in same_end_strand: other_arrow[1] += diff - one_strand_ahead = [x for x in arrows if x[2] == arrow[2]+1 and x[0]>=head] + one_strand_ahead = [x for x in arrows if x[2] == arrow[2]+1 and x[0] >= head] for other_arrow in one_strand_ahead: other_arrow[0] += diff totally_straightened = False diff --git a/dev/orthogonal/orthogonal.py b/dev/orthogonal/orthogonal.py index 4a8a922..7373faa 100644 --- a/dev/orthogonal/orthogonal.py +++ b/dev/orthogonal/orthogonal.py @@ -92,9 +92,9 @@ def topological_numbering(G): above = len([e for e in G.outgoing(v) if e.dummy is False]) if above != below: if above > below: - new_pos = min( n[e.head] for e in G.outgoing(v) ) - 1 + new_pos = min(n[e.head] for e in G.outgoing(v)) - 1 else: - new_pos = max( n[e.tail] for e in G.incoming(v) ) + 1 + new_pos = max(n[e.tail] for e in G.incoming(v)) + 1 if new_pos != n[v]: n[v] = new_pos success = True @@ -191,10 +191,10 @@ def saturate_face( face_info ): break for i in range(len(face_info) - 2): x, y, z = face_info[i:i+3] - if x.turn == -1 and y.turn== z.turn == 1: - a,b = (x, z) if x.kind == 'sink' else (z, x) + if x.turn == -1 and y.turn == z.turn == 1: + a, b = (x, z) if x.kind == 'sink' else (z, x) remaining = face_info[:i] + [LabeledFaceVertex(z.index, z.kind, 1)] + face_info[i+3:] - return [ (a.index, b.index) ] + saturate_face(remaining) + return [(a.index, b.index)] + saturate_face(remaining) return [] new_edges = saturate_face(self.switches(swap_hor_edges)) @@ -213,7 +213,7 @@ def saturate_face( face_info ): break for i in range(len(face_info) - 2): x, y, z = face_info[i:i+3] - if x.turn == -1 and y.turn== z.turn == 1: + if x.turn == -1 and y.turn == z.turn == 1: a,b = (x, z) if x.kind == 'sink' else (z, x) remaining = face_info[:i] + [LabeledFaceVertex(z.index, z.kind, 1)] + face_info[i+3:] return [ (a.index, b.index) ] + saturate_face(remaining) @@ -273,10 +273,10 @@ def _build_faces(self): def _make_turn_regular(self): dummy = set() regular = [F for F in self.faces if F.is_turn_regular()] - irregular =[F for F in self.faces if not F.is_turn_regular()] + irregular = [F for F in self.faces if not F.is_turn_regular()] while len(irregular): F = irregular.pop() - i,j = F.kitty_corner() + i, j = F.kitty_corner() v0, v1 = F[i][1], F[j][1] kind = random.choice( ('vertical', 'horizontal')) if len([e for e in self.incoming(v0) if e.kind == kind]): @@ -359,9 +359,9 @@ def show(self, unit=10, labels=True): class Face(CyclicList): def __init__(self, link, crossing_strands, exterior=False): list.__init__(self, crossing_strands) - self.edges = dict( (c.oriented(),c) for c in crossing_strands) + self.edges = {c.oriented(): c for c in crossing_strands} self.exterior = exterior - self.turns =[1 for e in self] + self.turns = [1 for e in self] def edge_of_intersection(self, other): """ @@ -623,7 +623,7 @@ def load_from_spherogram(self, link, spacing=None, adjust_plink_size=True): size, vertices, arrows, crossings = OrthogonalLinkDiagram(link).plink_data(spacing, width) self.clear() self.clear_text() - for (x, y) in vertices: + for x, y in vertices: self.Vertices.append(Vertex(x, y, self.canvas)) for u, v in arrows: U, V = self.Vertices[u], self.Vertices[v] @@ -635,7 +635,7 @@ def load_from_spherogram(self, link, spacing=None, adjust_plink_size=True): self.create_colors() self.goto_start_state() if adjust_plink_size and size: - self.window.geometry('%sx%s'% (size[0], size[1] + 25)) + self.window.geometry('%sx%s' % (size[0], size[1] + 25)) plink.LinkEditor.load_from_spherogram = load_from_spherogram diff --git a/dev/signature.py b/dev/signature.py index c095fee..8caeb32 100644 --- a/dev/signature.py +++ b/dev/signature.py @@ -37,6 +37,7 @@ def compact_form(p): assert p == y**(p.degree() // 2) * g(y + 1/y) return g + def roots_on_unit_circle(poly, prec=53): """ For a palindromic polynomial p(x) of even degree, return all the @@ -48,7 +49,7 @@ def roots_on_unit_circle(poly, prec=53): assert is_palindromic(poly) and poly.degree() % 2 == 0 assert poly.parent().is_exact() # Deal with these corner cases at some point if needed. - assert poly(1) != 0 and poly(-1) !=0 + assert poly(1) != 0 and poly(-1) != 0 ans = [] RR = RealField(prec) @@ -61,6 +62,7 @@ def roots_on_unit_circle(poly, prec=53): ans += [ (arg, e) for arg in args] return sorted(ans) + def alexander_poly_from_seifert(V): R = PolynomialRing(ZZ, 't') t = R.gen() diff --git a/dev/tangle_patch.py b/dev/tangle_patch.py index 78a1c6f..7307932 100644 --- a/dev/tangle_patch.py +++ b/dev/tangle_patch.py @@ -37,7 +37,7 @@ def add_random_crossing(self,label): old_position = randint(0,len(adj)-1) old_crossing, old_strand = adj.pop(old_position) new_strand = randint(0,3) - old_crossing[old_strand]=new_crossing[new_strand] + old_crossing[old_strand] = new_crossing[new_strand] for i in range(1,4): adj.insert(old_position,(new_crossing,(new_strand-i)%4)) adj[len(adj)/2:] = reversed(adj[len(adj)/2:]) @@ -153,7 +153,7 @@ def all_four_cycles_at_vertex(G, start_vertex): four_cycles = set() for v in adjacent: for w in adjacent: - if v==w: + if v == w: continue new_adj = G.children(v).intersection(G.children(w)) new_adj.remove(start_vertex) @@ -170,10 +170,11 @@ def all_four_cycles_at_vertex(G, start_vertex): return four_cycles + def unknot_search(num_attempts, backtrack_height, num_mutations): c = spherogram.Crossing(0) - c[0]=c[1] - c[2]=c[3] + c[0] = c[1] + c[2] = c[3] U = spherogram.Link([c]) for i in range(num_attempts): print(i) @@ -182,7 +183,7 @@ def unknot_search(num_attempts, backtrack_height, num_mutations): for i in range(num_mutations): Uc = random_mutate(Uc) Uc.simplify(mode='level') - if len(Uc)>0: + if len(Uc) > 0: return Uc return None @@ -194,7 +195,7 @@ def get_four_cycle(G, start_vertex): adjacent = G.children(start_vertex) for v in adjacent: for w in adjacent: - if v==w: + if v == w: continue new_adj = G.children(v).intersection(G.children(w)) new_adj.remove(start_vertex) @@ -330,22 +331,22 @@ def tangle_neighborhood(link,crossing,radius,return_gluings=True,hull=False): print('all comps: '+str(comps)) comps.remove(largest_comp) #remove largest component for comp in comps: - print('crossings: ' +str(crossings)) - print('filling in comp:'+str(comp)) - print('adjacent: '+str(adjacent)) + print('crossings: ' + str(crossings)) + print('filling in comp:' + str(comp)) + print('adjacent: ' + str(adjacent)) c = comp.pop() cs = choice(c.crossing_strands()) print('cs: ' + str(cs)) exit_strand = meander(cs,sides)[1] #meander until you hit boundary exit_strand = exit_strand[0].crossing_strands()[exit_strand[1]] - print('exit_strand: ' +str(exit_strand)) + print('exit_strand: ' + str(exit_strand)) bound_comp = trace_boundary_component(exit_strand,adjacent) print('traced component: ' + str(bound_comp)) if exit_strand not in main_boundary_comp: for x in bound_comp: adjacent.remove(x) - print('updated adjacent: ' +str(adjacent)) + print('updated adjacent: ' + str(adjacent)) crossings.append(c) crossings.extend(list(comp)) @@ -531,7 +532,7 @@ def tangle_cut(link, cycle): flip(side1) return Tangle(n/2,crossings0,side0),Tangle(n/2,crossings1,side1) -def fill_in_crossings(link,sides): +def fill_in_crossings(link, sides): """ Given boundary as above , fill in crossings on either side from sides. Returns a dictionary with the side (0 or 1) of each crossing. @@ -540,17 +541,17 @@ def fill_in_crossings(link,sides): crossing_sides = dict([(x[0], sides[x]) for x in sides]) crossing_labels = map(lambda c: c.label,link.crossings) crossings_to_sort = set(crossing_labels)-set(x[0] for x in sides) - while len(crossings_to_sort)>0: + while crossings_to_sort: start_crossing = crossings_to_sort.pop() accumulated_crossings = [start_crossing] m,end_side = meander(crossing_strand_from_name(link,(start_crossing,randint(0,3))),sides) accumulated_crossings.extend(map(lambda x: x.label,m)) for c in accumulated_crossings: - crossing_sides[c]=end_side + crossing_sides[c] = end_side return crossing_sides -def meander(cs,sides): +def meander(cs, sides): """ Wander randomly starting at crossing strand cs until you hit a boundary strand in sides.Assumes the crossing of cs is not on the side.