Exemplo n.º 1
0
def find_orbits():
    n = argv.get("n", 3)
    m = argv.get("m", 2)
    rows = list(range(m))
    cols = list(range(n))
    orbits = {}
    found = set()
    for H in numpy.ndindex((2,)*n*m):
        H = array2(H)
        H.shape = (m, n)
        s = H.tostring()
        if s in orbits:
            continue
        if rank(H)<m:
            continue
        found.add(s)
        for rperm in all_perms(rows):
          for cperm in all_perms(cols):
            #print(rperm, cperm)
            J = H[rperm, :]
            J = J[:, cperm]
            #print(J)
            J = J.copy()
            s = J.tostring()
            #assert s not in orbits
            orbits[s] = H
    for s in found:
        H = numpy.fromstring(s, dtype=H.dtype)
        print(H)
    print(len(found))
Exemplo n.º 2
0
def all_rects_shape(els, m, n):
    # A rect of shape (m,n)
    # is a tuple of length m, of length n tuple's.
    assert m * n == len(els), (m, n, len(els))
    els = tuple(els)
    if m == 1:
        row = els
        yield (row, )
        return
    if n == 1:
        col = tuple((e, ) for e in els)
        yield col
        return

    found = set()
    top = els[0]
    rest = els[1:]
    for row in choose(rest, n - 1):
        rest1 = [e for e in rest if e not in row]
        for col in choose(rest1, m - 1):
            rest2 = [e for e in rest1 if e not in col]
            for sub in all_perms(rest2):
                rect = [(top, ) + row]
                k = 0
                for i in col:
                    row1 = (i, ) + sub[k:k + n - 1]
                    k += n - 1
                    rect.append(row1)
                rect = tuple(rect)
                assert rect not in found
                found.add(rect)
                yield rect
Exemplo n.º 3
0
def all_cycles(els):
    els = tuple(els)
    n = len(els)
    if n < 3:
        yield els
        return
    if n == 3:
        yield els
        yield (els[0], els[2], els[1])
        return
    for perm in all_perms(els[1:]):
        yield (els[0], ) + perm
Exemplo n.º 4
0
def all_obinary_trees(els):
    if not els:
        return
    els = list(els)
    if len(els) == 1:
        yield els[0]
    #els = [set(e) for e in els]
    found = set()
    n = len(els)
    for perm in all_perms(els):
        for i in range(n):
            for j in range(i + 1, n):
                a, b = perm[i], perm[j]
                pair = (a, b)
                rest = list(perm)
                rest.pop(j)
                rest.pop(i)
                rest.insert(0, pair)
                for tree in all_obinary_trees(rest):
                    if tree not in found:
                        yield tree
                        found.add(tree)
Exemplo n.º 5
0
 def weak_eq(self, other):
     #print("weak_eq:")
     #print("\t%s"%(self,))
     #print("\t%s"%(other,))
     if len(self.uniqsrc) != len(other.uniqsrc):
         return
     assert other.tgt is not None
     subs = self.tgt.specialize(other.tgt, iso=True)
     if subs is None:
         return
     n = len(self.uniqsrc)
     items = list(range(n))
     #print("weak_eq", subs)
     for perm in all_perms(items):  # XXX slow & stupid, fix me
         #print("\t", perm)
         _subs = dict(subs)
         for i, j in enumerate(perm):
             lhs, rhs = self.uniqsrc[i], other.uniqsrc[j]
             _subs = lhs.specialize(rhs, _subs, iso=True)
             #print("weak_eq: specialize", lhs, rhs, _subs)
             if _subs is None:
                 break
         else:
             return _subs
Exemplo n.º 6
0
                    yield s, t


# ----------------------------------------------------

Zero = Species(lambda items: Set([]), "Zero")
One = Species(lambda items: (Set([items])
                             if len(items) == 0 else empty), "One")
X = Species(lambda items: (Set([items]) if len(items) == 1 else empty), "X")
E = Species(lambda items: Set([items]), "E")
E_plus = Species(lambda items: (Set([items])
                                if len(items) > 0 else empty), "E_plus")
E_2 = Species(lambda items: (Set([items])
                             if len(items) == 2 else empty), "E_2")
Point = Species(lambda items: Set.promote(items), "Point")
List = Species(lambda items: Set(all_perms(items)), "List")
Perm = List
Cycle = Species(lambda items: Set(all_cycles(items)))
Der = Species(lambda items: Set(all_ders(items)), "Der")
Par = Species(lambda items: Set(items.all_partitions()), "Par")
BinaryTree = Species(all_binary_trees, "BinaryTree")
OrderedBinaryTree = Species(all_obinary_trees, "OrderedBinaryTree")
Pow = Species(all_subsets, "Pow")
Tree = Species(lambda items: Set(items.all_trees()), "Tree")


def all_handshakes(items):
    items = list(items)
    items.sort()
    n = len(items)
    if n < 2: