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 561–660 of 1604. Source text is displayed literally.

561    free = [c for c in range(ncols) if c not in piv]562    basis = []563    for f in free:564        v = [Fr(0)] * ncols565        v[f] = Fr(1)566        for i, c in enumerate(piv):567            v[c] = -A[i][f]568        basis.append(v)569    return basis570 571 572def independent_extend(base, cands):573    """greedily pick vectors of cands independent modulo span(base); returns (reduced rows, picked)"""574    rows = []575    def reduce(v):576        v = list(v)577        for r, c in rows:578            if v[c] != 0:579                f = v[c] / r[c]580                v = [a - f * b for a, b in zip(v, r)]581        return v582    def add(v):583        v = reduce(v)584        c = next((i for i, a in enumerate(v) if a != 0), None)585        if c is None:586            return False587        rows.append((v, c))588        return True589    for b in base:590        add([Fr(x) for x in b])591    picked = []592    for v in cands:593        if add(v):594            picked.append(v)595    return picked596 597 598def solve_coords(cols, v):599    """coordinates of v in the independent family cols (exact)"""600    n = len(cols)601    m = len(v)602    A = [[Fr(cols[j][i]) for j in range(n)] + [Fr(v[i])] for i in range(m)]603    rk = 0604    piv = []605    for c in range(n):606        p = next((i for i in range(rk, m) if A[i][c] != 0), None)607        if p is None:608            return None609        A[rk], A[p] = A[p], A[rk]610        A[rk] = [x / A[rk][c] for x in A[rk]]611        for i in range(m):612            if i != rk and A[i][c] != 0:613                f = A[i][c]614                A[i] = [x - f * y for x, y in zip(A[i], A[rk])]615        piv.append(c)616        rk += 1617    if any(A[i][n] != 0 for i in range(rk, m)):618        return None619    return [A[i][n] for i in range(n)]620 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                continue