Independent scholarship · Open accessThe monthly digest

Indexed text attachment

verify-topology.py

Source for Algebraic and Differential Topology in Higher Categories

View paper · Download original file

Lines 1021–1120 of 1961. Source text is displayed literally.

1021    for i in range(n + 1):1022        for j in range(n + 1 - i):1023            k = n - i - j1024            allowed = [c for c, v in ((0, k), (1, i), (2, j)) if v > 0]1025            lab[(i, j)] = random.choice(allowed)1026    return lab1027 1028 1029def signed_rainbow(tris, lab):1030    tot = cnt = 01031    for t in tris:1032        l = [lab[v] for v in t]1033        if sorted(l) == [0, 1, 2]:1034            cnt += 11035            tot += perm_sign(l)1036    return cnt, tot1037 1038 1039okS = True1040counts = []1041for _ in range(200):1042    n = 121043    lab = sperner_label(n)1044    c, s = signed_rainbow(sperner_grid(n), lab)1045    counts.append(c)1046    okS = okS and c % 2 == 1 and s == 11047check('S1 Sperner: every admissible labelling of a subdivided triangle has an odd number of complete cells, and their signed count is +1',1048      okS, '200 random labellings of the grid of side 12: between %d and %d complete cells' % (min(counts), max(counts)))1049 1050okE = True1051for _ in range(200):1052    n = 121053    lab = sperner_label(n)1054    e = sum((1 if (lab[(i, 0)], lab[(i + 1, 0)]) == (0, 1) else -1 if (lab[(i, 0)], lab[(i + 1, 0)]) == (1, 0) else 0) for i in range(n))1055    okE = okE and e == 11056check('S2 the signed count is the degree of the labelling on the boundary: on the side labelled 0 and 1 the signed number of 01-edges is +1', okE, '200 labellings')1057 1058 1059def sperner3(m):1060    """m-fold barycentric subdivision of a tetrahedron with coordinates; random Sperner labels"""1061    tets = [[tuple(Fr(int(i == k)) for k in range(4)) for i in range(4)]]1062    for _ in range(m):1063        new = []1064        for t in tets:1065            for perm in itertools.permutations(t):1066                new.append([tuple(sum(p[k] for p in perm[:j]) / j for k in range(4)) for j in (1, 2, 3, 4)])1067        tets = new1068    lab = {}1069    for t in tets:1070        for v in t:1071            if v not in lab:1072                lab[v] = random.choice([k for k in range(4) if v[k] > 0])1073    cnt = tot = 01074    for t in tets:1075        l = [lab[v] for v in t]1076        if sorted(l) == [0, 1, 2, 3]:1077            M = [[Fr(1)] + list(v[:3]) for v in t]1078            # orientation of the cell times the sign of the labelling1079            rows = [[a - b for a, b in zip(v[:3], t[0][:3])] for v in t[1:]]1080            det = (rows[0][0] * (rows[1][1] * rows[2][2] - rows[1][2] * rows[2][1]) - rows[0][1] * (rows[1][0] * rows[2][2] - rows[1][2] * rows[2][0])1081                   + rows[0][2] * (rows[1][0] * rows[2][1] - rows[1][1] * rows[2][0]))1082            cnt += 11083            tot += (1 if det > 0 else -1) * perm_sign(l)1084    return cnt, tot, len(tets)1085 1086 1087r3 = [sperner3(2) for _ in range(20)]1088ref = r3[0][1]1089check('S3 Sperner in dimension three: an odd number of complete cells, with signed count of absolute value one',1090      all(c % 2 == 1 and abs(s) == 1 and s == ref for c, s, _ in r3), '20 labellings of the second subdivision of a tetrahedron (%d cells): %d to %d complete cells' % (r3[0][2], min(c for c, _, _ in r3), max(c for c, _, _ in r3)))1091 1092 1093def fmap(p):1094    """a continuous self-map of the standard triangle (barycentric coordinates)"""1095    x, y, z = p1096    q = (0.2 + 0.5 * y + 0.3 * z * z, 0.1 + 0.6 * z + 0.2 * x * y, 0.3 + 0.4 * x + 0.1 * y * y)1097    s = sum(q)1098    return tuple(a / s for a in q)1099 1100 1101def sperner_fixed(n):1102    def pt(i, j):1103        return ((n - i - j) / n, i / n, j / n)1104    def lab(i, j):1105        p = pt(i, j)1106        q = fmap(p)1107        for k in range(3):1108            if p[k] > 0 and q[k] <= p[k]:1109                return k1110        return max(range(3), key=lambda k: p[k])1111    best = None1112    for t in sperner_grid(n):1113        if sorted(lab(*v) for v in t) == [0, 1, 2]:1114            c = tuple(sum(pt(*v)[k] for v in t) / 3 for k in range(3))1115            e = max(abs(a - b) for a, b in zip(c, fmap(c)))1116            best = e if best is None else min(best, e)1117    return best1118 1119 1120errs = [sperner_fixed(n) for n in (8, 32, 128)]