Exemplo n.º 1
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.º 2
0
def reed_muller(r, m, puncture=False):
    "Build Reed-Muller code"

    assert 0<=r<=m, "r=%s, m=%d"%(r, m)

    n = 2**m # length

    one = array2([1]*n)
    basis = [one]

    vs = [[] for i in range(m)]
    for i in range(2**m):
        for j in range(m):
            vs[j].append(i%2)
            i >>= 1
        assert i==0

    vs = [array2(v) for v in vs]

    for k in range(r):
        for items in choose(vs, k+1):
            v = one
            #print(items)
            for u in items:
                v = v*u
            basis.append(v)
        
    G = numpy.array(basis)

    code = Code(G, d=2**(m-r), desc="reed_muller(%d, %d)"%(r, m))

    if puncture:
        code = code.puncture(0)

    return code
Exemplo n.º 3
0
 def cube(cls):
     "the verts, edges and faces of a cube"
     verts = []
     edges = []
     faces = []
     incidence = []
     for i in [0, 1]:
         for j in [0, 1]:
             for k in [0, 1]:
                 verts.append((i, j, k))
     for edge in choose(verts, 2):
         i, j = edge
         if sum([abs(i[ii] - j[ii]) for ii in range(3)]) == 1:
             e = (i, j)
             edges.append(e)
             incidence.append((i, e))
             incidence.append((j, e))
     assert len(edges) == 12
     assert len(incidence) == 24
     for idx in range(3):
         for face in choose(verts, 4):
             r = face[0][idx]
             for v in face:
                 if v[idx] != r:
                     break
             else:
                 faces.append(face)
                 for v in face:
                     incidence.append((v, face))
                 for edge in choose(face, 2):
                     if edge in edges:
                         incidence.append((edge, face))
     assert len(faces) == 6
     #for (i,j) in incidence[24:]:
     #    print i, '---', j
     assert len(incidence) == 24 + 6 * 4 + 6 * 4
     tpmap = {}
     for v in verts:
         tpmap[v] = 'v'
     for e in edges:
         tpmap[e] = 'e'
     for f in faces:
         tpmap[f] = 'f'
     g = Geometry(incidence, tpmap)
     return g
Exemplo n.º 4
0
def test_phi_3():
    names = """
        x_000 x_001 x_010 x_011 x_100 x_101 x_110 x_111
    """.strip().split()
    count = 0
    for ns in choose(names, 4):
        x = sum(eval("0b"+n[2:]) for n in ns) % 2
        if x==0:  # <------ subscripts sum to zero
            print(' '.join(ns), "==", x)
            count += 1
    print("count:", count)
Exemplo n.º 5
0
def test_plucker():
    ring = Q
    zero = Poly({}, ring)
    one = Poly({():1}, ring)

    rows, cols = argv.get("rows", 2), argv.get("cols", 4)
    U = numpy.empty((rows, cols), dtype=object)
    for i in range(rows):
      for j in range(cols):
        U[i, j] = Poly("x[%d,%d]"%(i, j), ring)

    print(U)
    COLS = list(range(cols))
    w = {} # the plucker coordinates
    for idxs in choose(COLS, rows):
        V = U[:, idxs]
        #print(V)
        a = determinant(V)
        w[idxs] = a
        #print(idxs, a)

    if (rows, cols) == (2, 4):
        assert w[0,1]*w[2,3]-w[0,2]*w[1,3]+w[0,3]*w[1,2] == 0

    for idxs in choose(COLS, rows-1):
      for jdxs in choose(COLS, rows+1):
        if len(idxs) and idxs[-1] >= jdxs[0]:
            continue
        #print(idxs, jdxs)
        sign = ring.one
        rel = ring.zero
        for l in range(rows+1):
            ldxs = idxs+(jdxs[l],)
            rdxs = jdxs[:l] + jdxs[l+1:]
            rel += sign*w[ldxs]*w[rdxs]
            sign *= -1
        assert rel==0
Exemplo n.º 6
0
def is_morthogonal(G, m):
    k = len(G)
    if m==1:
        for v in G:
            if v.sum()%2 != 0:
                return False
        return True
    if m>2 and not is_morthogonal(G, m-1):
        return False
    items = list(range(k))
    for idxs in choose(items, m):
        v = G[idxs[0]]
        for idx in idxs[1:]:
            v = v * G[idx]
        if v.sum()%2 != 0:
            return False
    return True
Exemplo n.º 7
0
 def get_diagram(self):
     diagram = []
     #        print("get_diagram", self.types)
     for (i, j) in choose(self.types, 2):
         #            print("\tget_diagram", (i, j))
         tps = [k for k in self.types if k != i and k != j]
         assert len(tps) < 20
         #print "flag_type:", tps
         for flag in self.flags_type(tps):
             h = self.residue(flag)
             assert h.types == [i, j] or h.types == [j, i]
             if h.is_digon():
                 break
         else:
             diagram.append((i, j))
     diagram.sort()
     return diagram
Exemplo n.º 8
0
def strong_morthogonal(G, m):
    k = len(G)
    assert m>=1
    if m==1:
        for v in G:
            if v.sum()%2 != 0:
                return False
        return True
    if not strong_morthogonal(G, m-1):
        return False
    items = list(range(k))
    for idxs in choose(items, m):
        v = G[idxs[0]]
        for idx in idxs[1:]:
            v = v * G[idx]
        if v.sum()%2 != 0:
            return False
    return True
Exemplo n.º 9
0
def get_phi_3(w3, w2=None):
    vs = dict(globals())
    ns = dict((k, 0) for k in globals().keys())
    items = []
    names = "x_000 x_001 x_010 x_011 x_100 x_101 x_110 x_111".split()
    count = 0
    for _ns1 in choose(names, 4):
        ns1 = dict((k, vs[k]) for k in _ns1)
        ns2 = dict(ns)
        ns2.update(ns1)
        w = w3.substitute(ns2)
        items.append(w)
        #print(' '.join(_ns1), w.otherstr())
#        if w2 is not None and w.otherstr() == w2.otherstr():
#            x = sum(eval("0b"+n[2:]) for n in _ns1) % 2
#            print(' '.join(_ns1), w.otherstr())
#            assert x==0, x
#            #print(w.otherstr(), w2.otherstr())
#            count += 1
#    print("count:", count)
    return items
Exemplo n.º 10
0
def get_phi_4(w4, w3=None):
    vs = dict(globals())
    ns = dict((k, 0) for k in globals().keys())
    items = []
    names = """
        x_0000 x_0001 x_0010 x_0011 x_0100 x_0101 x_0110 x_0111
        x_1000 x_1001 x_1010 x_1011 x_1100 x_1101 x_1110 x_1111
    """.strip().split()
    count = 0
    for _ns1 in choose(names, 8):
        ns1 = dict((k, vs[k]) for k in _ns1)
        ns2 = dict(ns)
        ns2.update(ns1)
        w = w4.substitute(ns2)
        items.append(w)
#        #print(' '.join(_ns1), w.otherstr())
#        if w3 is not None and w.otherstr() == w3.otherstr():
#            x = sum(eval("0b"+n[2:]) for n in _ns1) % 2
#            print(' '.join(_ns1), "==", x)
#            count += 1
#    print("count:", count)
    return items
Exemplo n.º 11
0
def test():

    for m in range(2, 7):
      for r in range(0, m+1):
        code = reed_muller(r, m)
        assert code.n == 2**m
        k = 1
        for i in range(1, r+1):
            k += len(list(choose(list(range(m)), i)))
        assert code.k == k
        if code.k < 12:
            assert code.get_distance() == 2**(m-r)

        if 0<=r<=m-1:
            dual = code.get_dual()
            code1 = reed_muller(m-r-1, m)
            #print(m-r-1 == r, dual.eq(code), code)
            assert code1.eq(dual)

    test_hamming()

    print("OK")
Exemplo n.º 12
0
def test():

    n = argv.get("n", 3)
    items = list(range(1, n + 1))
    tgt = src = items

    I = Func(tgt, src, dict((i, i) for i in items), "")
    up = Func(tgt, src, dict((i, min(i + 1, n)) for i in items), "u")
    dn = Func(tgt, src, dict((i, max(i - 1, 1)) for i in items), "d")

    M = Monoid.generate([I, up, dn], maxsize=12)
    assert len(M) == 8

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

    X = [0, 1, 2]

    M = [
        Func(X, X, {
            0: 0,
            1: 1,
            2: 2
        }, 'I'),
        Func(X, X, {
            0: 0,
            1: 0,
            2: 1
        }, 'a'),
        Func(X, X, {
            0: 0,
            1: 0,
            2: 2
        }, 'b'),
        Func(X, X, {
            0: 0,
            1: 1,
            2: 1
        }, 'c'),
        Func(X, X, {
            0: 1,
            1: 1,
            2: 2
        }, 'd'),
        Func(X, X, {
            0: 0,
            1: 2,
            2: 2
        }, 'e'),
        Func(X, X, {
            0: 1,
            1: 2,
            2: 2
        }, 'f'),
        Func(X, X, {
            0: 0,
            1: 0,
            2: 0
        }, 'g'),
        Func(X, X, {
            0: 1,
            1: 1,
            2: 1
        }, 'h'),
        Func(X, X, {
            0: 2,
            1: 2,
            2: 2
        }, 'i'),
    ]

    N = Monoid.generate(M, maxsize=100)
    assert len(N) == len(M)

    I, a, b, c, d, e, f, g, h, i = M
    assert repr(b * a) == "Func([0, 1, 2], [0, 1, 2], {0:0, 1:0, 2:0})"
    assert b * a == g
    assert a * b == a

    for gen in choose(M[1:], 3):
        N = Monoid.generate(gen)
        if len(N) == len(M) - 1:
            print([str(g) for g in gen], end=" ")
            print(len(N))

    gens = [a, b, f]
    show_table(M, M)
Exemplo n.º 13
0
def test_plucker_flag():
    ring = Q
    zero = Poly({}, ring)
    one = Poly({():1}, ring)

    n = argv.get("n", 4)
    U = numpy.empty((n, n), dtype=object)
    for i in range(n):
      for j in range(n):
        U[i, j] = Poly("x[%d,%d]"%(i, j), ring)

    print(U)

    N = list(range(n))
    w = {} # the plucker coordinates
    for k in range(1, n):
      for idxs in choose(N, k):
        V = U[:k, idxs]
        #print(V)
        a = determinant(V)
        if k==1:
            w[idxs[0]] = a
        else:
            w[idxs] = a
        #print(idxs, a)

    assert n==4
    p1 = w[0]
    p2 = w[1]
    p3 = w[2]
    p4 = w[3]
    p12 = w[0,1]
    p13 = w[0,2]
    p14 = w[0,3]
    p23 = w[1,2]
    p24 = w[1,3]
    p34 = w[2,3]
    p123 = w[0,1,2]
    p124 = w[0,1,3]
    p134 = w[0,2,3]
    p234 = w[1,2,3]

    for rel in [
        p23*p1 - p13*p2 + p12*p3,       p24*p1 - p14*p2 + p12*p4,
        p34*p1 - p14*p3 + p13*p4,       p34*p2 - p24*p3 + p23*p4,
        p14*p23 - p13*p24 + p12*p34,    p234*p1 - p134*p2 + p124*p3 - p123*p4,
        p134*p12 - p124*p13 + p123*p14, p234*p12 - p124*p23 + p123*p24,
        p234*p13 - p134*p23 + p123*p34, p234*p14 - p134*p24 + p124*p34,
    ]:
        assert rel == 0

    return

    for idxs in choose(N, rows-1):
      for jdxs in choose(N, rows+1):
        if len(idxs) and idxs[-1] >= jdxs[0]:
            continue
        print(idxs, jdxs)
        sign = ring.one
        rel = ring.zero
        for l in range(rows+1):
            ldxs = idxs+(jdxs[l],)
            rdxs = jdxs[:l] + jdxs[l+1:]
            rel += sign*w[ldxs]*w[rdxs]
            sign *= -1
        print(rel)