def test_coset_enumeration(self): g1 = Permutation.read_cycle_form([[1,2,3,4]], 4) g2 = Permutation.read_cycle_form([[1,2]], 4) h1 = Permutation.read_cycle_form([[1,2,3]], 4) h2 = Permutation.read_cycle_form([[1,2]], 4) G = PermGroup([g1, g2]) H = PermGroup([h1, h2]) cosets = G._left_cosets(H) total = 0 elements = [] for coset in cosets: temp_eles = coset._list_elements() elements += temp_eles self.assertEqual(len(temp_eles),len(H)) total += len(temp_eles) self.assertEqual(len(G), total) self.assertEqual(sorted(G._list_elements()), sorted(elements)) cosets = G._right_cosets(H) total = 0 elements = [] for coset in cosets: temp_eles = coset._list_elements() elements += temp_eles self.assertEqual(len(temp_eles),len(H)) total += len(temp_eles) self.assertEqual(len(G), total) self.assertEqual(sorted(G._list_elements()), sorted(elements))
def test_len(self): s1 = Permutation.read_cycle_form([[1,2,3,4]], 4) s2 = Permutation.read_cycle_form([[1,2]], 4) G = PermGroup([s1, s2]) self.assertEqual(len(G), len(G._list_elements())) self.assertEqual(len(G), 24) self.assertTrue(Permutation.read_cycle_form([[3,4]], 4) in G._list_elements())
def test_klien4(self): a = Permutation.read_cycle_form([[1,2],[3,4]],4) b = Permutation.read_cycle_form([[1,3],[2,4]],4) G = PermGroup([a,b]) oG = OrbitGraph(G,(1,2)) self.assertTrue(tuple(sorted(oG.edges)) == ((1,2),(2,1),(3,4),(4,3))) self.assertTrue(oG.ad_list == [[2],[1],[4],[3]])
def with_tet_label(self): si = str(self.snappy_tetrahedron.Index)+'|' op = self.cut_ribbon_graph.opposite new_op = Permutation({si+label : si+op[label] for label in op}) next = self.cut_ribbon_graph.next new_next = Permutation({si+label : si+next[label] for label in next}) return RibbonGraph([new_op,new_next])
def write(P): # --------------- symmetry factor ---------- if order == 4: a1, a2, a3, a4, a5, a6, a7, a8 = P.permutation pm = Permutation(a1 + 1, a2 + 1, a3 + 1, a4 + 1, a5 + 1, a6 + 1, a7 + 1, a8 + 1) elif order == 3: a1, a2, a3, a4, a5, a6 = P.permutation pm = Permutation(a1 + 1, a2 + 1, a3 + 1, a4 + 1, a5 + 1, a6 + 1) sign = pm.sign P.sym_factor = -1 * sign * abs(P.sym_factor) # --------------------------------------------- with open("DiagPolar{0}.new.txt".format(order), 'a') as f: f.write("# Topology [" + str(P.name) + "]\n") for i in P.permutation: f.write('{0:3d}'.format(i)) f.write('\n') f.write("# Symmetry Factor \n {} \n".format(P.sym_factor)) f.write("# Loop Bases" + '\n') len_i, len_j = P.loop_bases.shape for i in range(len_i): for j in range(len_j): f.write('{0:3d}'.format(P.loop_bases[i, j])) f.write('\n') f.write("# Ver Loop Bases" + '\n') len_i, len_j = P.ver_bases.shape for i in range(len_i): for j in range(len_j): f.write('{0:3d}'.format(P.ver_bases[i, j])) f.write('\n') f.write('\n')
def test_isvalid(self): fg = FatgraphB([ (1, 2, 3, 4), ], [(1, 2), (3, 4)], [(1, 4), (2, 3)]) assert fg.isvalid() fg._is = Permutation.from_cycles((1, 2), (3, 4)) assert not fg.isvalid() fgb = FatgraphB([ tuple(range(1, 9)), ], [(1, 8), (2, 3), (4, 7), (5, 6)], [(2, 7), (3, 6), (4, 5), (1, 8)]) assert not fgb.isvalid() fgb = FatgraphB([(1, 2, 3, 4), (5, 6, 7, 8)], [(1, 8), (2, 7), (3, 5), (4, 6)], [(1, 2), (3, 4), (5, 6), (7, 8)]) assert not fgb.isvalid() fgb._is = Permutation.from_cycles((1, 4), (2, 3), (5, 6), (7, 8)) assert fgb.isvalid() fg = FatgraphB([ (1, 2, 3, 4, 5, 6), ], [(3, 6), (4, 5)], [(1, 4), (2, 3), (5, 6)]) assert fg.isvalid() fg = FatgraphB([ (1, 2, 3, 4, 5, 6), ], [(3, 6), (4, 5)], [(1, 2), (6, 3), (5, 4)]) assert not fg.isvalid() fg = FatgraphB([ (1, 2, 3, 4, 5, 6), ], [(1, 4), (3, 5)], [(1, 6), (2, 5), (3, 4)]) assert fg.isvalid()
def main(): UPPER_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" testAlphabet = Alphabet(UPPER_STRING) permutation1 = Permutation( "(AELTPHQXRU) (BKNW) (CMOY) (DFG) (IV) (JZ) (S)", testAlphabet) permutation2 = Permutation( "(FIXVYOMW) (CDKLHUP) (ESZ) (BJ) (GR) (NT) (A) (Q)", testAlphabet) permutation3 = Permutation("(ABDHPEJT) (CFLVMZOYQIRWUKXSG) (N)", testAlphabet) permutation4 = Permutation("(AEPLIYWCOXMRFZBSTGJQNH) (DV) (KU)", testAlphabet) permutation5 = Permutation( "(AE) (BN) (CK) (DQ) (FU) (GY) (HW) (IJ) (LO) (MP) (RX) (SZ) (TV)", testAlphabet) rotor1 = Rotor("I", permutation1, "TG") rotor2 = Rotor("II", permutation2, "A") rotor3 = Rotor("III", permutation3, "B") rotor4 = Rotor("IV", permutation4, "XO") reflector = Reflector("A", permutation5) rotors = [reflector, rotor4, rotor3, rotor2, rotor1] machine = Machine(testAlphabet, 5, 6, rotors) machine.insertRotors(["A", "IV", "III", "II", "I"]) machine.setRotors("AAAA") message = input("What to convert:") print(machine.convertMsg(message))
def doGenerate(): variables.head = handleList(variables.head) variables.tail = handleList(variables.tail) variables.name.append("") variables.birthday.append("") variables.extinfo.append("") variables.dict_max.append("") variables.tail.append("") variables.head.append("") variables.year.append("") # variables.lett.append("") variables.email.append("") variables.qq.append("") variables.phoneNumer.append("") f = open('./result.txt', 'w') for birstdayItem in variables.birthday: for nameItem in variables.name: for extinfoItem in variables.extinfo: for dictMaxItem in variables.dict_max: for yearItem in variables.year: for phoneItem in variables.phoneNumer: for qqItem in variables.qq: for emailItem in variables.email: preS = birstdayItem + nameItem + extinfoItem\ + dictMaxItem + yearItem + phoneItem\ + qqItem + emailItem if len(preS) < variables.minLen or len( preS) > variables.maxLen: continue obj = Permutation() obj.words = [birstdayItem, nameItem, extinfoItem, \ dictMaxItem, \ yearItem, phoneItem, qqItem, \ emailItem] obj.words = handleList(obj.words) rst = obj.permutationList() for s in rst: if len(s) < variables.minLen or len( s) > variables.maxLen: break for tailItem in variables.tail: for headItem in variables.head: s2 = headItem + s + tailItem if len( s2 ) >= variables.minLen and len( s2 ) <= variables.maxLen: print s2 if const.isRelease: f.write(s2) f.write('\n') f.close() print "Done!"
def test_schreier_graph_construction(self): s1 = Permutation.read_cycle_form([[2,3]], 4) s2 = Permutation.read_cycle_form([[1,2,4]], 4) gens = [s1,s2] identity = Permutation([1,2,3,4]) s_g = _schreier_graph(2, gens, identity) self.assertEqual(s_g, [s2,identity,s1,s2])
def _permutations_from_PD(self, PD): edge_dict = {} for n, v in enumerate(PD): for m, label in enumerate(v): if label in edge_dict: edge_dict[label].append((n, m)) else: edge_dict[label] = [(n, m)] positions = [] for l in edge_dict.values(): positions.extend(l) opposite_list = [[ positions.index(pair) + 1 for pair in edge_dict[label] ] for label in edge_dict] next_list = [] for n, v in enumerate(PD): cycle = [] for m, label in enumerate(v): cycle.append(positions.index((n, m)) + 1) next_list.append(cycle) opposite = Permutation(dictionary={}, cycles=opposite_list) next = Permutation(dictionary={}, cycles=next_list) return opposite, next
def test_identity(self): a = Permutation([2, 3, 1, 4]) c = Permutation([]) self.assertEqual(a ** -1 * a, Permutation.read_cycle_form([], 4)) self.assertEqual(a * a ** -1, Permutation.read_cycle_form([], 4)) self.assertEqual((a * a ** -1)._func, (1, 2, 3, 4)) self.assertEqual(c._func, ())
def write(P, filename): order = len(P.permutation) / 2 # --------------- symmetry factor ---------- if order == 6: a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12 = P.permutation pm = Permutation(a1 + 1, a2 + 1, a3 + 1, a4 + 1, a5 + 1, a6 + 1, a7 + 1, a8 + 1, a9 + 1, a10 + 1, a11 + 1, a12 + 1) elif order == 5: a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 = P.permutation pm = Permutation(a1 + 1, a2 + 1, a3 + 1, a4 + 1, a5 + 1, a6 + 1, a7 + 1, a8 + 1, a9 + 1, a10 + 1) elif order == 4: a1, a2, a3, a4, a5, a6, a7, a8 = P.permutation pm = Permutation(a1 + 1, a2 + 1, a3 + 1, a4 + 1, a5 + 1, a6 + 1, a7 + 1, a8 + 1) elif order == 3: a1, a2, a3, a4, a5, a6 = P.permutation pm = Permutation(a1 + 1, a2 + 1, a3 + 1, a4 + 1, a5 + 1, a6 + 1) elif order == 2: a1, a2, a3, a4 = P.permutation pm = Permutation(a1 + 1, a2 + 1, a3 + 1, a4 + 1) sign = pm.sign P.sym_factor = sign * abs(P.sym_factor) # --------------------------------------------- with open(filename, 'a') as f: f.write("# Topology [" + str(P.name) + "]\n") for i in P.permutation: f.write('{0:3d}'.format(i)) f.write('\n') f.write('# Propagator Type \n') for i in P.permutation: f.write('{0:3d}'.format(0)) f.write('\n') f.write("# Symmetry Factor \n {} \n".format(P.sym_factor)) f.write("# Loop Bases" + '\n') len_i, len_j = P.loop_bases.shape for i in range(len_i): for j in range(len_j): f.write('{0:3d}'.format(P.loop_bases[i, j])) f.write('\n') f.write("# Ver Loop Bases" + '\n') len_i, len_j = P.ver_bases.shape for i in range(len_i): for j in range(len_j): f.write('{0:3d}'.format(P.ver_bases[i, j])) f.write('\n') f.write('# Interaction Type \n') for i in range(order + 1): f.write('{0:3d}'.format(0)) f.write('\n') f.write('# SpinFactor \n') for i in range(2**(order - 1)): f.write('{0:3d}'.format(1)) f.write('\n') f.write('\n')
def test_cycle_form_reading(self): a = Permutation([2, 3, 1, 4]) b = Permutation.read_cycle_form([], 4) e = Permutation([1, 2, 3, 4]) self.assertEqual(b, e) self.assertEqual(a, Permutation.read_cycle_form([[1, 2, 3]], 4)) self.assertEqual(a, Permutation.read_cycle_form([[2, 3, 1]], 4)) self.assertEqual(a, Permutation.read_cycle_form([[3, 1, 2]], 4))
def test_coset_reps(self): s1 = Permutation.read_cycle_form([[2,3]], 4) s2 = Permutation.read_cycle_form([[1,2,4]], 4) gens = [s1,s2] identity = Permutation([1,2,3,4]) s_g = _schreier_graph(2, gens, identity) cosets = _coset_reps(s_g, identity) self.assertEqual(cosets, [Permutation.read_cycle_form([[1,4,2]], 4),identity,s1,s2])
def test_equality(self): p = Permutation.from_list([1, 0, 2]) q = Permutation.from_cycle((0, 1), 3) self.assertEqual(p, q) self.assertTrue(p == q) q = Permutation.from_cycle((0, 1), 2) self.assertNotEqual(p, q) self.assertTrue(p != q)
def disk_with_handles(pairing): s = set() for x, y in pairing: s.add(x) s.add(y) l = sorted(s) next = Permutation(cycles=[l]) opposite = Permutation(cycles=pairing) return RibbonGraph([opposite, next])
def trefoil(): next = Permutation(cycles=[('a', 'b', 'c', 'd'), ('e', 'f', 'g', 'h'), ('i', 'j', 'k', 'l')]) opposite = Permutation( cycles=[('a', 'f'), ('b', 'e'), ('c', 'l'), ('d', 'k'), ('i', 'h'), ('g', 'j')]) return RibbonGraph([opposite, next])
def test_generator_with_custom_start(self): results = [] for p in permutations(3, Permutation.from_list([1, 2, 0])): results.append(p) self.assertEqual(len(results), 3) self.assertEqual(results[0], Permutation.from_list([1, 2, 0])) self.assertEqual(results[1], Permutation.from_list([2, 0, 1])) self.assertEqual(results[2], Permutation.from_list([2, 1, 0]))
def test_base_image_member(self): h1 = Permutation.read_cycle_form([[3,4,5,6,7]], 7) h2 = Permutation.read_cycle_form([[3,4]], 7) H = PermGroup.fixed_base_group([h1, h2], [3,4,5,6]) self.assertEqual(H.base, [3,4,5,6]) image1 = [1,2,3,4] image2 = [5,3,4,6] self.assertTrue(H.base_image_member(image1) is None) self.assertEqual(H.base_image_member(image2), Permutation.read_cycle_form([[3,5,4]],7))
def relabeled(self): labels = list(self.labels()) indices = {l: i for i, l in enumerate(labels)} new_op = Permutation( {i: indices[self.opposite[labels[i]]] for i in range(len(labels))}) new_next = Permutation( {i: indices[self.next[labels[i]]] for i in range(len(labels))}) return RibbonGraph([new_op, new_next])
def test_default_construction(self): p = Permutation(3) self.assertEqual(p.dim, 3) self.assertEqual(p, Permutation.from_list([0, 1, 2])) for i in range(p.dim): self.assertEqual(p[i], i) q = Permutation(1) self.assertEqual(q.dim, 1) self.assertEqual(q, Permutation.from_list([0]))
def __init__(self, vertices, edges): # Check the arguments define a valid fatgraph self.checkvalidity(vertices, edges) self.vertices = vertices self.edges = edges self._vs = Permutation.from_cycles(*vertices) self._es = Permutation.from_cycles(*edges) vs = [i for j in vertices for i in j] es = [i for j in edges for i in j] self.unpaired = set(vs) - set(es)
def afterGenerateNameList(f, l): nameList = [] nameList.append(f) nameList.append(l) obj = Permutation() obj.words = nameList rst = obj.permutationList() if rst: variables.name.extend(rst) #字符串去重 variables.name = list(set(variables.name))
def doGenerate(): variables.head = handleList(variables.head) variables.tail = handleList(variables.tail) variables.name.append("") variables.birthday.append("") variables.extinfo.append("") variables.dict_max.append("") variables.tail.append("") variables.head.append("") variables.year.append("") # variables.lett.append("") variables.email.append("") variables.qq.append("") variables.phoneNumer.append("") f = open('./result.txt', 'w') for birstdayItem in variables.birthday: for nameItem in variables.name: for extinfoItem in variables.extinfo: for dictMaxItem in variables.dict_max: for yearItem in variables.year: for phoneItem in variables.phoneNumer: for qqItem in variables.qq: for emailItem in variables.email: preS = birstdayItem + nameItem + extinfoItem\ + dictMaxItem + yearItem + phoneItem\ + qqItem + emailItem if len(preS) < variables.minLen or len(preS) > variables.maxLen: continue obj = Permutation() obj.words = [birstdayItem, nameItem, extinfoItem, \ dictMaxItem, \ yearItem, phoneItem, qqItem, \ emailItem] obj.words = handleList(obj.words) rst = obj.permutationList() for s in rst: if len(s) < variables.minLen or len(s) > variables.maxLen: break for tailItem in variables.tail: for headItem in variables.head: s2 = headItem + s + tailItem if len(s2) >= variables.minLen and len(s2) <= variables.maxLen: print s2 if const.isRelease: f.write(s2); f.write('\n') f.close() print "Done!"
def test_fixed_base_group(self): g1 = Permutation.read_cycle_form([[1,2,3,4,5,6]], 6) g2 = Permutation.read_cycle_form([[1,2]], 6) h1 = Permutation.read_cycle_form([[3,4,5,6]], 6) h2 = Permutation.read_cycle_form([[3,4]], 6) G = PermGroup.fixed_base_group([g1, g2], [5,4]) H = PermGroup.fixed_base_group([h1, h2], [1,2,3,4,5,6]) N = PermGroup.fixed_base_group([g1,h1], []) self.assertEqual(G.base[:2], [5,4]) self.assertEqual(H.base[:4], [1,2,3,4]) self.contains_membership_test(G, H)
def test_associativity(self): x = Permutation.from_string('(0, 1)(2, 3)', 4) y = Permutation.from_string('(1, 2)', 4) z = Permutation.from_string('(0, 3)', 4) l = (x * y) * z r = x * (y * z) n = x * y * z self.assertEqual(l, r) self.assertEqual(l, n) self.assertEqual(r, n)
def factoring(self, perm): step = dict() for g in self.gen: step[g] = 0 perm = Permutation(perm=perm) for i in range(len(self.base)): while perm[self.base[i]] != self.base[i]: step[(self.prev_tree_list[i][perm[self.base[i]]])] += 1 perm = ~(self.prev_tree_list[i][perm[self.base[i]]]) * perm if perm != Permutation(size=len(perm)): return None return step
def main(): # Test 3-inflatable permutation of length 17 tau = Permutation('2cbdf8419hea3576g') print('Densities in inflation of', tau) for pi in Permutation.of_length(3): print(pi, inflated_density(pi, tau)) # Incorrect example from Cooper & Petrarca tau = Permutation('472951836') print('Densities in inflation of', tau) for pi in Permutation.of_length(3): print(pi, inflated_density(pi, tau))
def __init__(self, mcomplex, meridian_info=None): self.tetrahedra = [Tetrahedron(tet) for tet in mcomplex.Tetrahedra] self.ribbon_graph = RibbonGraph([Permutation(), Permutation()]) for tet in self.tetrahedra: self.ribbon_graph = self.ribbon_graph.union(tet.with_tet_label()) if meridian_info: self.add_meridian() self.glue_boundary_faces() self.classify_lace_components()
def test_orbit(self): g1 = Permutation.read_cycle_form([[1,2,3,4]], 4) g2 = Permutation.read_cycle_form([[1,2]], 4) base = [3,4,1] reverse_priority = [3,4,1,2] G = PermGroup.fixed_base_group([g1, g2], base) self.assertEqual(G.orbit(1), [1,2,3,4]) self.assertEqual(G.orbit(1, stab_level = 0), [1,2,3,4]) self.assertEqual(G.orbit(1, stab_level = 1), [1,2,4]) self.assertEqual(G.orbit(1, stab_level = 2), [1,2]) self.assertEqual(G.orbit(1, stab_level = 3), [1]) self.assertEqual(G.orbit(1, key = lambda x : reverse_priority[x - 1]), [3,4,1,2])
def test_schreier_generators(self): s1 = Permutation.read_cycle_form([[2,3]], 4) s2 = Permutation.read_cycle_form([[1,2,4]], 4) gens = [s1,s2] identity = Permutation([1,2,3,4]) s_g = _schreier_graph(2, gens, identity) cosets = _coset_reps(s_g, identity) s_gen = _schreier_generators(2, cosets, gens, identity) gen_1 = Permutation.read_cycle_form([[3,4]], 4) gen_2 = Permutation.read_cycle_form([[1,3,4]], 4) gen_3 = Permutation.read_cycle_form([[1,3]], 4) self.assertEqual(s_gen, [gen_1, gen_2, gen_3])
def test_construction_from_cycles(self): tests = [(([(0, )], 1), [0]), (([(0, 1), (2, 3)], 4), [1, 0, 3, 2]), (([(0, 1)], 4), [1, 0, 2, 3]), (([(0, 1), (1, 2)], 4), [1, 2, 0, 3]), (([(0, 1), (2, 3), (4, 5)], 6), [1, 0, 3, 2, 5, 4]), (([(0, 1, 2), (2, 3)], 4), [1, 2, 3, 0])] for test in tests: cycles = test[0][0] dim = test[0][1] expected = test[1] self.assertEqual(Permutation.from_cycles(cycles, dim), Permutation.from_list(expected))
def test_read_partitions(self): a = Partition([[1], [2], [3], [4], [5]]) b = Partition([[2], [3], [4], [5], [1]]) c = Partition([[2], [1], [4], [3], [5]]) perm1 = Permutation([2, 3, 4, 5, 1]) perm1alt = Permutation.read_partitions(a, b) perm2 = Permutation([2, 1, 4, 3, 5]) perm2alt = Permutation.read_partitions(a, c) perm3 = Permutation([5, 2, 1, 4, 3]) perm3alt = Permutation.read_partitions(b, c) self.assertEqual(perm1, perm1alt) self.assertEqual(perm2, perm2alt) self.assertEqual(perm3, perm3alt)
def test_action(self): X = [0, 1, 2, 3, 4, 5] p = Permutation(6) p.act_on(X) self.assertEqual(X, [0, 1, 2, 3, 4, 5]) p = Permutation.from_cycle((3, 0), 4) p.act_on(X) self.assertEqual(X, [3, 1, 2, 0, 4, 5]) X = [0, 1, 2, 3, 4, 5] p = Permutation.from_cycle((1, 2, 3), 6) p.act_on(X) self.assertEqual(X, [0, 3, 1, 2, 4, 5])
def cube(): next = Permutation( cycles=[(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12), (13, 14, 15), (16, 17, 18), (19, 20, 21), (22, 23, 24)]) opposite = Permutation(cycles=[(1, 5), (4, 8), (7, 11), (2, 10), ( 13, 18), (16, 21), (19, 24), (22, 15), (3, 14), (6, 17), (9, 20), (12, 23)]) return RibbonGraph([opposite, next])
def build_schreier_tree(b: int, gen): n = len(next(iter(gen))) def dfs(b, gen, orbit, prev): for g in gen: if g[b] not in orbit: orbit[g[b]] = g * orbit[b] prev[g[b]] = g dfs(g[b], gen, orbit, prev) orbit = {b: Permutation(n)} prev = {b: Permutation(n)} dfs(b, gen, orbit, prev) return orbit, prev
def test_construction_from_invalid_string(self): tests = [('(,)', 4), ('( ,)', 4), ('( ', 4), ('( ,', 4), ('(1, )', 4), ('(0,,1)', 4), ('(0, ,1)', 4), ('(0, ,1)', 4), ('0, 1, 2)', 3), ('(0, 1(2, 3)', 4), ('(0, 1,(2, 3)', 4), ('1 (0, 1)(2, 3) ', 4), (' 1 (0, 1 ) ( 2, 3) ', 4), (' (0, 1 ) ( 2, 3 ', 4), (' (0, 1 ) ( 2, 3', 4), (' (0, 1 ) ( 2, 3,', 4)] for test in tests: with self.subTest(test=test[0]): with self.assertRaises(ValueError): cycles_string = test[0] dim = test[1] Permutation.from_string(cycles_string, dim)
def test_initialisation(self): s1 = Permutation.read_cycle_form([[2,3,5,7],[9, 10]], 10) s2 = Permutation.read_cycle_form([[1,2,4,8],[9, 10]], 10) G = PermGroup([s1, s2]) base_gen_pairs = list(zip(['_']+G.base, G.chain_generators)) prev = base_gen_pairs[0][1] base_eles = [] for base_ele, gens in base_gen_pairs[1:-1]: base_eles.append(base_ele) for g in gens: for b in base_eles: self.assertEqual(b, b**g) temp = PermGroup(gens) self.assertTrue(len([g for g in prev if g not in temp])>0) prev = gens
def test_coset(self): g1 = Permutation.read_cycle_form([[1,2,3,4]], 4) g2 = Permutation.read_cycle_form([[1,2]], 4) h1 = Permutation.read_cycle_form([[1,2,3]], 4) h2 = Permutation.read_cycle_form([[1,2]], 4) G = PermGroup([g1, g2]) H = PermGroup([h1, h2]) coset = sorted((H*g1)._list_elements()) for c in coset: self.assertEqual(sorted((H*c)._list_elements()), coset) self.assertEqual(len(H), len(coset)) coset = sorted((g1*H)._list_elements()) for c in coset: self.assertEqual(sorted((c*H)._list_elements()), coset) self.assertEqual(len(H), len(coset))
def test_construction_from_cycle(self): # mapping from (cycle, dim) -> list representation tests = { ((), 1): [0], ((0, ), 1): [0], ((1, ), 4): [0, 1, 2, 3], ((1, 2), 4): [0, 2, 1, 3], ((0, 3), 4): [3, 1, 2, 0], ((0, 1, 2), 4): [1, 2, 0, 3], ((0, 3, 1), 4): [3, 0, 2, 1] } for test, result in tests.items(): p = Permutation.from_cycle(test[0], test[1]) self.assertEqual(p, Permutation.from_list(result))
def compute_det(self): if self.is_det_computed: return self.det permutations = Permutation.get_permutations(self.n, list(range(self.n))) value = 0 for permutation in permutations: x = 1 for i in range(self.n): x *= self.matrix[i][permutation[i]] value += Permutation(permutation).get_permutation_sign() * x if self.n == 0: value = 1 self.is_det_computed = True self.det = value return value
def connect_edges(self, pairing): """ Given a list of pairs of half-edge labels which are currently disconnected (fixed points of self.opposite), connect the half-edges up. If one of the labels is already connected, it raises an exception. """ connecting_permutation = Permutation(cycles=pairing) all_labels = connecting_permutation.labels() for label in connecting_permutation.labels(): if self.opposite[label] != label: raise Exception( "Trying to connect already connected half edge") new_op = self.opposite.restricted_to(self.opposite.labels() - all_labels) return RibbonGraph([new_op * connecting_permutation, self.next])
def snappy_tetrahedron_to_edge_pairing(self): tet = self.tetrahedron gluing = tet.Gluing neighbor = tet.Neighbor directed_edge_choices = ['32','23','10','01'] corresponding_left_faces = [7, 11, 13, 14] #[F3, F2, F1, F0] permutations = [Permutation(gluing[i].dict) for i in corresponding_left_faces] corresponding_neighbors = [neighbor[i].Index for i in corresponding_left_faces] pairing = [] for e, perm, index in zip(directed_edge_choices,permutations, corresponding_neighbors): print(tet.Index, index) print(perm) vs = self.boundary_edge_from_directed_edge(e) new_e = ''.join(reversed([str(perm[int(v)]) for v in e])) new_vs = self.boundary_edge_from_directed_edge(new_e) # new_vs = ''.join([new_vs[2], new_vs[3], new_vs[0], new_vs[1]]) print(e,new_e) print(vs, new_vs) print(self.ribbon_graph.face(vs), self.ribbon_graph.face(new_vs)) pairing.append( (str(tet.Index)+vs, str(index)+new_vs) ) print('\n\n') return pairing
def subgroup(self): #Needs to be done in this order. self.initialise_partition_stacks() self.initialise_r_base() self.initialise_search_tree() gens = [] while self._cur_height > -1: #alt_1 rule out. backtrack_index = self.tree_modifiers.exclude_backtrack_index(self.left, self.right, self._cur_position, self._cur_height) if backtrack_index is not None: self.backtrack(backtrack_index) #alt_2 discrete check. elif self.right.discrete(): perm = Permutation.read_partitions(self.left[self._cur_height], self.right[self._cur_height]) if not perm.trivial() and self.tree_modifiers.property_check(perm): gens.append(perm) backtrack_index = self.tree_modifiers.leaf_pass_backtrack_index(self.left,self.right,self._cur_position) else: backtrack_index = self.tree_modifiers.leaf_fail_backtrack_index(self.left,self.right,self._cur_position) self.backtrack(backtrack_index) #alt_3 function to extend. elif self._r_base[self._cur_height] is not None: func = self._r_base[self._cur_height] func(self.right) self._cur_height += 1 #alt_4 special level. else: self._extend_right() return gens
def add_new_vertices(self, vertex_labels, vertex_size): """ Add a new vertex for each label in vertex_labels, with each vertex having size vertex_size. The half edges around the vertex corresponding to l will be labeled (l,0),(l,1),...(l,vertex_size-1). They are not yet connected to any other vertices. """ new_op = self.opposite * Permutation({(l, i): (l, i) for l in vertex_labels for i in range(vertex_size)}) new_next = self.next * Permutation( cycles=[[(l, i) for i in range(vertex_size)] for l in vertex_labels]) return RibbonGraph([new_op, new_next])
def test_change_base_redundancy(self): cf = lambda x: Permutation.read_cycle_form(x,6) a = cf([[1,2,3,4],[5,6]]) b = cf([[1,2]]) G = PermGroup([a,b]) G.change_base([5,6,1,2,3,4]) self.assertEqual(G.base[:4],[5,6,1,2])
def LU_decomp(A): """ returns (LU, P, signum) This function factorizes the square matrix A into the LU decomposition PA = LU. On output the diagonal and upper triangular part of the return matrix contain the matrix U. The lower triangular part of the input matrix (excluding the diagonal) contains L. The diagonal elements of L are unity, and are not stored. The permutation matrix P is encoded in the permutation p. The j-th column su of the matrix P is given by the k-th column of the identity matrix, where k = p_j the j-th element of the permutation vector. The sign of the permutation is given by signum. It has the value (-1)^n, where n is the number of interchanges in the permutation. The algorithm used in the decomposition is Gaussian Elimination with partial pivoting (Golub & Van Loan, Matrix Computations, Algorithm 3.4.1). """ p = Permutation(A.shape[1]) code = get_typecode(A) An = array_typed_copy(A) if code == Complex: # Now all error flags are turned into python exceptions. So no # unpack necessary any longer. signum = _gslwrap.gsl_linalg_complex_LU_decomp(An, p) elif code == Float: signum = _gslwrap.gsl_linalg_LU_decomp(An, p) else: print code, Float, Complex raise TypeError, "LU must be of type Float or Complex" return (An, p, signum)
def test_order(self): cf = lambda x : Permutation.read_cycle_form(x,6) g1 = cf([[1,4,5,2,3]]) g2 = cf([[1,2],[3,4]]) g3 = cf([[1,2],[6,5]]) G = PermGroup([g1,g2,g3]) self.assertEqual(G.order(), 360)
def test_cycle_notation(self): a = Permutation([1, 2, 3, 4, 5, 6]) b = Permutation([1, 3, 2, 5, 6, 4]) c = Permutation([2, 3, 4, 5, 6, 1]) d = Permutation([6, 4, 5, 2, 3, 1]) self.assertEqual(a.cycle_notation(), []) self.assertEqual(b.cycle_notation(), [[2, 3], [4, 5, 6]]) self.assertEqual(c.cycle_notation(), [[1, 2, 3, 4, 5, 6]]) self.assertEqual(d.cycle_notation(), [[1, 6], [2, 4], [3, 5]])
def test_pow(self): perm = Permutation.read_cycle_form([[1,2],[5,3]], 5) a = Partition([[1,2,3], [4,5]]) b = a ** perm c = Partition([[1,5,2], [4,3]]) d = b ** perm self.assertEqual(a,d) self.assertEqual(b,c) self.assertNotEqual(a,c) self.assertNotEqual(b,d)
def test_contains(self): cf = lambda x:Permutation.read_cycle_form(x, 6) g1 = cf([[1,2,3,4,5,6]]) g2 = cf([[1,2]]) h1 = cf([[3,4,5,6]]) h2 = cf([[3,4]]) a1 = cf([[3,1],[6,4]]) a2 = cf([[1,6,4]]) G = PermGroup([g1, g2]) H = PermGroup([h1, h2]) self.contains_membership_test(G, H) S6 = PermGroup.fixed_base_group([g1,g2], [5,2,1,3,4,6]) A4 = PermGroup.fixed_base_group([a1,a2], [3,1,2,5,6,4]) self.contains_membership_test(S6, A4)
def test_identity_modifiers(self): cf = lambda x:Permutation.read_cycle_form(x,5) fam = RefinementUnion([IdentityFamily()]) prop = CosetPropertyUnion([IdentityProperty()]) log = LeonLoggerUnion([LeonCounter()]) union = ModifierUnion([fam,prop,log]) a = PartitionStack([0,0,0,0,0],[-1,-1,-1,-1,-1]) b = PartitionStack([0,1,1,1,0],[-1,0,0,0,-1]) funcs = union.extension_functions(a) self.assertEqual(funcs,None) index = union.exclude_backtrack_index(a,b, None, None) self.assertEqual(index,None) check = union.property_check(None) self.assertTrue(check) pos_leaf = union.leaf_fail_backtrack_index(a,b,None) neg_leaf = union.leaf_pass_backtrack_index(a,b,None) self.assertEqual(pos_leaf, None) self.assertEqual(neg_leaf, None)
def find_partition_backtrack_subgroup(self): #Assume the refinement families are symmetric. generators = [] self.functions = self.preprocess() self.extension_indices = [0] * len(self.functions) while self.function_index > 0: #Alternative 3-4 self.extend_right() #Alternatice 1: premature rule out. if self.alt1(): self.backtrack() #Alternative 2: discrete check. elif self.right.discrete(): perm = Permutation.read_partitions(left[func_index], right[func_index]) if not perm.trivial() and self.prop_check(perm): generators.append(perm) self.backtrack() return generators
def find_partition_backtrack_coset(self): #Assume the refinement families are LR-symmetric. r_base = self._r_base() count = 0 while self._cur_height > -1: count += 1 #a = self._cur_position #b = self.right[self._cur_height] #c = self.left[self._cur_height] #if self.size == 13: #print("\n{}:{}\n{} -> {}".format(self._cur_height,a,c,b)) #Alternatice 1: premature rule out. if self.alt1(): #print("alt_1") self.backtrack(self._cur_height - 1) #Alternative 2: discrete check. elif self.right.discrete(): #print("alt_2") perm = Permutation.read_partitions(self.left[self._cur_height], self.right[self._cur_height]) if self.group_property.check(perm): return perm #self.backtrack() self.backtrack(self._cur_position.min_changed_index()) #Alternative 3: not a special level elif r_base[self._cur_height] is not None: #print("alt_3") r_base[self._cur_height](None, self.right) self._cur_height += 1 #Alternative 4: is a special level else: #print("alt_4") self.extend_right() print(count) return generators
def test_non_trivial_modifiers(self): cf = lambda x: Permutation.read_cycle_form(x,13) a = cf([[2,3],[4,6],[5,8],[9,11]]) b = cf([[1,2,4,7,9,3,5,6,8,10,11,12,13]]) G = PermGroup([a,b]) part_stab = Partition([[3,2],[6,4],[5,1,7,8,9,10,11,12,13]]) fam = RefinementUnion([PartitionStabaliserFamily(part_stab), SubgroupFamily(G)]) prop = CosetPropertyUnion([PartitionStabaliserProperty(part_stab), SubgroupProperty(G)]) log = LeonLoggerUnion([LeonCounter()]) cons = ModifierUnion([PartitionStackConstraint()]) union = ModifierUnion([fam,prop,log,cons]) left = PartitionStack([0]*13,[-1]*13) right = PartitionStack.deep_copy(left) funcs = union.extension_functions(left) self.assertFalse(funcs is None) funcs[0](left) funcs[1](right) index = union.exclude_backtrack_index(left,right, None, 1) self.assertEqual(index,None) index = union.exclude_backtrack_index(left,right, None, 2) self.assertEqual(index,1) self.assertTrue(union.property_check(a)) self.assertFalse(union.property_check(b)) pos_leaf = union.leaf_fail_backtrack_index(a,b,None) neg_leaf = union.leaf_pass_backtrack_index(a,b,None) self.assertEqual(pos_leaf, None) self.assertEqual(neg_leaf, None)
def get_gens(big_rectangle, small_rectangle): (W, H) = big_rectangle (w, h) = small_rectangle (w, h) = (w-1, h-1) grid = [list(range(s, s + W)) for s in range(1, H*W+1, W)] gens = [] for row in range(H-h): for col in range(W-w): gen = [] #top row for index in range(col, col + w): gen.append(grid[row][index]) #right col for index in range(row, row + h): gen.append(grid[index][col + w]) #bottom row for index in range(col + w, col, -1): gen.append(grid[row+h][index]) #left col for index in range(row + h, row, -1): gen.append(grid[index][col]) gens.append(gen) return [Permutation.read_cycle_form([gen],W*H) for gen in gens]
def __mul__(self, other): if type(other) is Transformation: return Transformation(self.orientation*other.orientation, self.inverted ^ other.inverted, (other[x] for x in self)) return Permutation.__mul__(self, other)
def __init__(self, orientation, inverted, tup): assert inverted == 0 or inverted == 1 self.inverted = inverted Permutation.__init__(self, orientation, tup)
def __new__(cls, orientation, inverted, tup): return Permutation.__new__(cls, orientation, tup)