def has_inssch(self): types = [ PermSet([Permutation(321), Permutation(2143), Permutation(2413)]), PermSet([Permutation(123), Permutation(3142), Permutation(3412)]), PermSet([Permutation(132), Permutation(312)]), PermSet([Permutation(213), Permutation(231)]) ] return all([ any([all([b.avoids(p) for p in B]) for b in self._basis]) for B in types ])
def is_identity(perm): """Checks if the permutation is the identity. >>> p = Permutation.random(10) >>> (p * p.inverse()).is_identity() True """ return perm == Permutation.identity(len(perm))
def threepats(perm): p = list(perm) n = perm.__len__() patnums = {'123': 0, '132': 0, '213': 0, '231': 0, '312': 0, '321': 0} for i in range(n - 2): for j in range(i + 1, n - 1): for k in range(j + 1, n): patnums[''.join( map(lambda x: str(x + 1), Permutation([p[i], p[j], p[k]])))] += 1 return patnums
def __init__(self, basis, length=8, verbose=0): list.__init__(self, [PermSet() for i in range(0, length + 1)]) self.length = length temp_basis = [] for P in basis: temp_basis.append(Permutation(P)) basis = temp_basis self.basis = basis if length >= 1: self[1].add(Permutation([1])) for n in range(2, length + 1): k = 0 outof = len(self[n - 1]) for P in self[n - 1]: k += 1 if verbose > 0 and k % verbose == 0: # print '\t\t\t\tRight Extensions:',k,'/',outof,'\t( length',n,')' print( '\t\t\t\tRight Extenstions: {}/{}\t( length {}'.format( k, outof, n)) insertion_locations = P.insertion_locations add_this_time = [] for Q in P.right_extensions(): is_good = True for B in basis: if B.involved_in(Q, last_require=2): is_good = False insertion_locations[Q[-1]] = 0 # break if is_good: add_this_time.append(Q) for Q in add_this_time: # print Q,'is good' # print '\tchanging IL from ',Q.insertion_locations,'to',(insertion_locations[:Q[-1]+1]+ insertion_locations[Q[-1]:]) Q.insertion_locations = insertion_locations[:Q[ -1] + 1] + insertion_locations[Q[-1]:] self[n].add(Q)
def all(cls, length): """Builds the set of all permutations of a given length. Parameters: ----------- length : int the length of the permutations Examples -------- >>> p = Permutation(12); q = Permutation(21) >>> PermSet.all(2) == PermSet([p, q]) True """ return PermSet(Permutation.listall(length))
def othercycles(perm): # builds a permutation induced by the black and gray edges separately, and # counts the number of cycles in their product p = list(perm) n = perm.__len__() q = [0] + [p[i] + 1 for i in range(n)] grayperm = [n] + range(n) blackperm = [0 for i in range(n + 1)] for i in range(n + 1): ind = q.index(i) blackperm[i] = q[ind - 1] newperm = [] for i in range(n + 1): k = blackperm[i] j = grayperm[k] newperm.append(j) return Permutation(newperm).numcycles()
def fourpats(perm): p = list(perm) n = perm.__len__() patnums = { '1234': 0, '1243': 0, '1324': 0, '1342': 0, '1423': 0, '1432': 0, '2134': 0, '2143': 0, '2314': 0, '2341': 0, '2413': 0, '2431': 0, '3124': 0, '3142': 0, '3214': 0, '3241': 0, '3412': 0, '3421': 0, '4123': 0, '4132': 0, '4213': 0, '4231': 0, '4312': 0, '4321': 0 } for i in range(n - 3): for j in range(i + 1, n - 2): for k in range(j + 1, n - 1): for m in range(k + 1, n): patnums[''.join( map(lambda x: str(x + 1), list(Permutation([p[i], p[j], p[k], p[m]]))))] += 1 return patnums
def right_juxtaposition(self, C, generate_perms=True): A = permset.PermSet() max_length = max([len(P) for P in self.basis]) + max( [len(P) for P in C.basis]) for n in range(2, max_length + 1): for i in range(0, factorial(n)): P = Permutation(i, n) for Q in self.basis: for R in C.basis: if len(Q) + len(R) == n: if (Q == Permutation(P[0:len(Q)]) and R == Permutation(P[len(Q):n])): A.add(P) elif len(Q) + len(R) - 1 == n: if (Q == Permutation(P[0:len(Q)]) and Permutation(R) == Permutation( P[len(Q) - 1:n])): A.add(P) return AvClass(list(A.minimal_elements()), length=(8 if generate_perms else 0))
def ltrmax(perm): return [len(perm) - i - 1 for i in Permutation(perm[::-1]).rtlmax()][::-1]