def test_main(): for ring in [element.Q, element.FiniteField(2), element.FiniteField(7)]: #for ring in [element.FiniteField(2)]: cx = Assembly.build_tetrahedron(ring) chain = cx.get_chain() #chain.dump() test_chain(chain) M = parse(""" 1.1..1 11.1.. .11.1. ...111 """) chain = Chain.from_array(M, ring) test_chain(chain) #cx = Assembly.build_torus(ring, 2, 2) cx = Assembly.build_surface(ring, (0, 0), (2, 2)) chain = cx.get_chain() test_chain(chain)
critical = flow.get_critical() for item in rows + cols: if item in critical: x, y = layout[item] cvs.stroke(path.circle(x, y, r), st_thick + [red]) for key in flow.get_pairs(): if key not in layout: continue x, y = layout[key] cvs.stroke(path.circle(x, y, r), st_thick + [orange]) # --------------------------------------------------------- ring = element.FiniteField(2) if 0: # ------------------------------------ M = parse(""" 11..... 1.1.... .1.11.. ..11.1. ....1.1 .....11 """) #print(M) M = numpy.array([[1, 1, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 1, 0, 1, 0],
#print(lhs) #print(rhs) assert (2 * f) == f + f g = f.kernel() gf = dot(f, g) assert gf == gf.hom.zero_vector() g = f.cokernel() fg = dot(g, f) assert fg == fg.hom.zero_vector() # ----------------------------------------- A = Space(2, ring) f = Map.from_array([[1, 1], [1, 1]], Hom(A, A)) g = f.image() #print(g) assert f.trace() == 2 if __name__ == "__main__": test_over_ring(element.Z) test_over_ring(element.Q) test_over_ring(element.FiniteField(5)) print("OK")
def test_chainmap(): p = argv.get("p", 2) ring = element.FiniteField(p) #ring = element.Q one = ring.one space = Space(ring) def mk_ising(m, n): U = Space(ring, m, 0, "U") V = Space(ring, n, 1, "V") A = Lin(U, V) # U <--- V for i in range(m): A[i, i] = one if i + 1 < n: A[i, i + 1] = -one return A A = mk_ising(3, 4) c = Chain([A]) f = c.identity() zero = ChainMap(c, c) assert f != zero assert f == f assert f != 2 * f assert f + f == 2 * f assert f - f == zero B = mk_ising(2, 2) d = Chain([B]) fn = Lin(A.src, B.src) for i in range(len(B.src)): fn[i, i] = one fm = Lin(A.tgt, B.tgt) for i in range(len(B.tgt)): fm[i, i] = one f = ChainMap(c, d, [fn, fm]) # ----------------------------------- m, n = 8, 10 V1 = Space(ring, n, 1, "V1") V0 = Space(ring, m, 0, "V0") A = Lin.rand(V0, V1) # V0 <--- V1 c = Chain([A]) U0 = Space(ring, m, 0, "U0") f0 = Lin.rand(V0, U0) f1, B = A.pullback(f0) d = Chain([B]) f = ChainMap(c, d, [f1, f0]) # ----------------------------------- # construct a chain map (and a chain) from a # pullback of a grade zero map. m, n, p = 5, 6, 1 V1 = Space(ring, n, 1, "V1") V0 = Space(ring, m, 0, "V0") #A = Lin.rand(V0, V1) # V0 <--- V1 A = Lin(V0, V1) for i in range(m): A[i, i] = one A[i, (i + 1)] = one a = Chain([A]) #print("A:") #print(A) U0 = Space(ring, p, 0, "U0") f0 = Lin(V0, U0) for i in range(p): f0[i, i] = one f1, B = A.pullback(f0) b = Chain([B]) #print("B:") #print(B) f = ChainMap(a, b, [f1, f0]) #print(f0) #print(f1) g = ChainMap(a, b) h = f.coequalizer(g) #print(h) #print(h[0]) #print(h[1]) c = h.tgt C = c[0] #print(C.shape, C.rank()) #print(C) # ----------------------------------- # construct a 'puncture' of a repitition code # as a cokernel m, n, p = 5, 6, 1 V1 = Space(ring, n, 1, "V1") V0 = Space(ring, m, 0, "V0") #A = Lin.rand(V0, V1) # V0 <--- V1 A = Lin(V0, V1) for i in range(m): A[i, i] = one A[i, (i + 1)] = one a = Chain([A]) U1 = Space(ring, 1, 1, "U1") U0 = Space(ring, 2, 0, "U0") B = Lin(U0, U1) B[0, 0] = one B[1, 0] = one b = Chain([B]) offset = 2 # where to puncture f0 = Lin(V0, U0) f0[0 + offset, 0] = one f0[1 + offset, 1] = one f1 = Lin(V1, U1) f1[offset + 1, 0] = one f = ChainMap(a, b, [f1, f0]) h = f.cokernel() c = h.tgt # ----------------------------------- # Here we puncture a bit from a parity check matrix # by using a cokernel. This deletes that column, # as well as all the rows with support therein. m, n, p = 5, 6, 1 V1 = Space(ring, n, 1, "V1") V0 = Space(ring, m, 0, "V0") A = Lin.rand(V0, V1) # V0 <--- V1 a = Chain([A]) #print(A) col = 0 rows = [] for i in range(V0.n): if A[i, col]: rows.append(i) U1 = Space(ring, 1, 1, "U1") U0 = Space(ring, len(rows), 0, "U0") B = Lin(U0, U1) for i in range(U0.n): B[i, 0] = one b = Chain([B]) #print(B) f0 = Lin(V0, U0) for i, row in enumerate(rows): f0[row, i] = one #print(f0) f1 = Lin(V1, U1) f1[col, 0] = one f = ChainMap(a, b, [f1, f0]) h = f.cokernel() c = h.tgt
def test(): p = argv.get("p", 2) ring = element.FiniteField(p) space = Space(ring) zeros = space.zeros rand = space.rand dot = space.dot kron = space.kron direct_sum = space.direct_sum identity = space.identity coequalizer = space.coequalizer compose = space.compose rank = space.rank pseudo_inverse = space.pseudo_inverse tensor_swap = space.tensor_swap sum_swap = space.sum_swap schur = space.schur is_zero = space.is_zero is_identity = space.is_identity s = tensor_swap(3, 4) si = tensor_swap(4, 3) #print(shortstr(s)) assert eq(dot(si, s), identity(3 * 4)) assert eq(dot(s, si), identity(4 * 3)) m, n = 2, 3 A1 = rand(m, n, 1, 1) A2 = rand(m, n, 1, 1) B = kron(A1, A2) for m in range(1, 5): I = identity(m * m) s = tensor_swap(m, m) f = coequalizer(I, s) assert eq(compose(s, f), f) assert rank(f) == [1, 3, 6, 10][m - 1] # --------------------------------- m = argv.get("m", 3) n = argv.get("n", 4) if argv.toric: A = zeros(m, m) for i in range(m): A[i, i] = ring.one A[i, (i + 1) % m] = -ring.one elif argv.surface: A = zeros(m - 1, m) for i in range(m - 1): A[i, i] = ring.one A[i, (i + 1) % m] = -ring.one else: A = rand(m, n, p - 1, p - 1) if argv.transpose: A = A.transpose() print("A:") print(shortstr(A)) n, m = A.shape In = identity(n) Im = identity(m) H1s = kron(Im, A), -kron(A, Im) H1 = numpy.concatenate(H1s, axis=0) # horizontal concatenate H0s = kron(A, In), kron(In, A) H0 = numpy.concatenate(H0s, axis=1) # horizontal concatenate assert is_zero(dot(H0, H1)) assert H1.shape == (n * m + m * n, m * m) assert H0.shape == (n * n, n * m + m * n) f0 = -tensor_swap(n, n) a = direct_sum(-tensor_swap(m, n), -tensor_swap(n, m)) b = sum_swap(n * m, m * n) assert is_identity(compose(b, b)) f1 = compose(a, b) assert is_identity(compose(f1, f1)) f2 = tensor_swap(m, m) assert eq(compose(f2, H1), compose(H1, f1)) lhs, rhs = ((compose(f1, H0), compose(H0, f0))) #print("lhs:") #print(shortstr(lhs)) #print("rhs:") #print(shortstr(rhs)) assert eq(compose(f1, H0), compose(H0, f0)) g0 = coequalizer(f0, identity(f0.shape[0])) assert eq(compose(H0, g0), compose(f1, H0, g0)) e = compose(H0, g0) g1, J0 = coequalizer(f1, identity(f1.shape[0]), e) assert eq(compose(H0, g0), compose(g1, J0)) e = compose(H1, g1) g2, J1 = coequalizer(f2, identity(f2.shape[0]), e) assert eq(compose(H1, g1), compose(g2, J1)) assert is_zero(compose(J1, J0)) n = J1.shape[0] J1t = J1.transpose() mz = rank(J1t) mx = rank(J0) print("J1t:", J1t.shape, rank(J1t)) print(shortstr(J1t)) print("J0:", J0.shape) print(shortstr(J0)) print("n:", n) print("mz:", mz) print("mx:", mx) print("k =", n - mx - mz)
def test_main(): for ring in [element.Q, element.FiniteField(2), element.FiniteField(7)]: #for ring in [element.FiniteField(2)]: test_matrix(ring)
def test_surface(): ring = element.FiniteField(2) one = ring.one C0 = [Cell(0, i) for i in [0]] C1 = [Cell(1, i) for i in [0, 1]] C2 = [Cell(2, i) for i in [0]] Sx = Matrix(C1, C2, {}, ring) Sx[C1[0], C2[0]] = one Sz = Matrix(C0, C1, {}, ring) Sz[C0[0], C1[1]] = one src = Chain({0: C0, 1: C1, 2: C2}, {1: Sz, 2: Sx}, ring, check=True) Sx = Matrix(C1, C2, {}, ring) Sx[C1[0], C2[0]] = one Sx[C1[1], C2[0]] = one Sz = Matrix(C0, C1, {}, ring) Sz[C0[0], C1[0]] = one Sz[C0[0], C1[1]] = one tgt = Chain({0: C0, 1: C1, 2: C2}, {1: Sz, 2: Sx}, ring, check=True) f0 = Matrix.identity(C0, ring) f1 = Matrix.identity(C1, ring) f1[C1[1], C1[0]] = one f2 = Matrix.identity(C2, ring) hom = Hom(src, tgt, {0: f0, 1: f1, 2: f2}, ring, check=True) #tgt, cnot = src.cnot(0, 1) # ---- Higgott encoder ---- C0 = [Cell(0, i) for i in range(2)] C1 = [Cell(1, i) for i in range(5)] C2 = [Cell(2, i) for i in range(2)] Sx = Matrix(C1, C2, {}, ring) Sx[C1[1], C2[0]] = one Sx[C1[3], C2[1]] = one Sz = Matrix(C0, C1, {}, ring) Sz[C0[0], C1[0]] = one Sz[C0[1], C1[4]] = one src = Chain({0: C0, 1: C1, 2: C2}, {1: Sz, 2: Sx}, ring, check=True) src.dump() chain = src for i, j in [(2, 0), (1, 4), (2, 4), (3, 0), (3, 2), (1, 2)]: chain, hom = chain.transvect(i, j) chain.dump() # ------------------------- m, n = 3, 2 ambly = Assembly.build_surface(ring, (0, 0), (m, n), open_top=True, open_bot=True) chain = ambly.get_chain() #chain.dump() tgt, hom = chain.transvect(1, 2) #tgt.dump() tgt, hom = tgt.transvect(1, 2)