Independent scholarship · Open accessThe monthly digest

Indexed text attachment

verify-topology-learning.py

Source for Algebraic and Differential Topology in Machine Learning and AI: Obstructions, Invariants, and Counts

View paper · Download original file

Lines 621–720 of 1604. Source text is displayed literally.

621 622def homology_trace(K, vmap, d):623    """trace of the map induced on H_d(K; Q) by a simplicial self-map"""624    n = K.n(d)625    Z = nullspace(K.bd(d), n) if d > 0 else [[Fr(int(i == j)) for i in range(n)] for j in range(n)]626    Bd = K.bd(d + 1) if d < K.dim else []627    Bcols = [list(c) for c in zip(*Bd)] if Bd and Bd[0] else []628    Bbasis = independent_extend([], [[Fr(x) for x in c] for c in Bcols])629    H = independent_extend(Bbasis, Z)630    if not H:631        return 0632    F = chain_map(K, K, vmap, d)633    cols = Bbasis + H634    t = Fr(0)635    for j, h in enumerate(H):636        img = [sum(F[i][k] * h[k] for k in range(n)) for i in range(n)]637        co = solve_coords(cols, img)638        t += co[len(Bbasis) + j]639    return t640 641 642def lefschetz_chain(K, vmap):643    return sum((-1) ** d * trace(chain_map(K, K, vmap, d)) for d in range(K.dim + 1))644 645 646def lefschetz_hom(K, vmap):647    return sum((-1) ** d * homology_trace(K, vmap, d) for d in range(K.dim + 1))648 649 650def sd(K):651    """barycentric subdivision; vertices are the simplices of K, numbered by (dim, index)"""652    num = {}653    for d in range(K.dim + 1):654        for x in K.S[d]:655            num[x] = len(num)656    facets = []657    for d in range(K.dim, -1, -1):658        for x in K.S[d]:659            if any(set(x) < set(y) for y in K.S.get(d + 1, [])):660                continue661            for perm in itertools.permutations(x):662                facets.append([num[tuple(sorted(perm[:k]))] for k in range(1, len(perm) + 1)])663    return Cx(facets), num664 665 666def cone(K, apex=None):667    top = max(v for x in K.S[0] for v in x) + 1 if apex is None else apex668    return Cx([list(x) + [top] for d in K.S for x in K.S[d]]), top669 670 671def suspension(K):672    top = max(v for x in K.S[0] for v in x) + 1673    return Cx([list(x) + [top] for x in K.S[K.dim]] + [list(x) + [top + 1] for x in K.S[K.dim]] +674              [list(x) + [top] for d in K.S for x in K.S[d]] + [list(x) + [top + 1] for d in K.S for x in K.S[d]])675 676 677def facets_of(K):678    return [x for d in K.S for x in K.S[d] if not any(set(x) < set(y) for y in K.S.get(d + 1, []))]679 680 681def prism(K):682    """K x I with vertices v (bottom) and v + N (top)"""683    N = max(v for x in K.S[0] for v in x) + 1684    F = []685    for x in facets_of(K):686        for i in range(len(x)):687            F.append(list(x[:i + 1]) + [v + N for v in x[i:]])688    return Cx(F), N689 690 691def is_pseudomanifold(K, closed=True):692    d = K.dim693    cnt = {}694    for x in K.S[d]:695        for i in range(d + 1):696            f = x[:i] + x[i + 1:]697            cnt[f] = cnt.get(f, 0) + 1698    vals = set(cnt.values())699    return (vals == {2}) if closed else (vals <= {1, 2})700 701 702def torus7():703    return Cx([[i, (i + 1) % 7, (i + 3) % 7] for i in range(7)] + [[i, (i + 2) % 7, (i + 3) % 7] for i in range(7)])704 705 706def rp2():707    return Cx([[1, 2, 4], [1, 2, 6], [1, 3, 4], [1, 3, 5], [1, 5, 6], [2, 3, 5], [2, 3, 6], [2, 4, 5], [3, 4, 6], [4, 5, 6]])708 709 710def klein():711    def v(i, j):712        if i == 4:713            i, j = 0, (-j) % 4714        return 4 * i + (j % 4)715    F = []716    for i in range(4):717        for j in range(4):718            F.append([v(i, j), v(i + 1, j), v(i + 1, j + 1)])719            F.append([v(i, j), v(i, j + 1), v(i + 1, j + 1)])720    return Cx(F)