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 541–640 of 1961. Source text is displayed literally.

541 542def trace(M):543    return sum(M[i][i] for i in range(min(len(M), len(M[0]) if M else 0)))544 545 546def nullspace(M, ncols):547    """rational basis of the kernel"""548    A = [[Fr(x) for x in r] for r in M]549    piv = []550    rk = 0551    for c in range(ncols):552        p = next((i for i in range(rk, len(A)) if A[i][c] != 0), None)553        if p is None:554            continue555        A[rk], A[p] = A[p], A[rk]556        A[rk] = [x / A[rk][c] for x in A[rk]]557        for i in range(len(A)):558            if i != rk and A[i][c] != 0:559                f = A[i][c]560                A[i] = [x - f * y for x, y in zip(A[i], A[rk])]561        piv.append(c)562        rk += 1563    free = [c for c in range(ncols) if c not in piv]564    basis = []565    for f in free:566        v = [Fr(0)] * ncols567        v[f] = Fr(1)568        for i, c in enumerate(piv):569            v[c] = -A[i][f]570        basis.append(v)571    return basis572 573 574def independent_extend(base, cands):575    """greedily pick vectors of cands independent modulo span(base); returns (reduced rows, picked)"""576    rows = []577    def reduce(v):578        v = list(v)579        for r, c in rows:580            if v[c] != 0:581                f = v[c] / r[c]582                v = [a - f * b for a, b in zip(v, r)]583        return v584    def add(v):585        v = reduce(v)586        c = next((i for i, a in enumerate(v) if a != 0), None)587        if c is None:588            return False589        rows.append((v, c))590        return True591    for b in base:592        add([Fr(x) for x in b])593    picked = []594    for v in cands:595        if add(v):596            picked.append(v)597    return picked598 599 600def solve_coords(cols, v):601    """coordinates of v in the independent family cols (exact)"""602    n = len(cols)603    m = len(v)604    A = [[Fr(cols[j][i]) for j in range(n)] + [Fr(v[i])] for i in range(m)]605    rk = 0606    piv = []607    for c in range(n):608        p = next((i for i in range(rk, m) if A[i][c] != 0), None)609        if p is None:610            return None611        A[rk], A[p] = A[p], A[rk]612        A[rk] = [x / A[rk][c] for x in A[rk]]613        for i in range(m):614            if i != rk and A[i][c] != 0:615                f = A[i][c]616                A[i] = [x - f * y for x, y in zip(A[i], A[rk])]617        piv.append(c)618        rk += 1619    if any(A[i][n] != 0 for i in range(rk, m)):620        return None621    return [A[i][n] for i in range(n)]622 623 624def homology_trace(K, vmap, d):625    """trace of the map induced on H_d(K; Q) by a simplicial self-map"""626    n = K.n(d)627    Z = nullspace(K.bd(d), n) if d > 0 else [[Fr(int(i == j)) for i in range(n)] for j in range(n)]628    Bd = K.bd(d + 1) if d < K.dim else []629    Bcols = [list(c) for c in zip(*Bd)] if Bd and Bd[0] else []630    Bbasis = independent_extend([], [[Fr(x) for x in c] for c in Bcols])631    H = independent_extend(Bbasis, Z)632    if not H:633        return 0634    F = chain_map(K, K, vmap, d)635    cols = Bbasis + H636    t = Fr(0)637    for j, h in enumerate(H):638        img = [sum(F[i][k] * h[k] for k in range(n)) for i in range(n)]639        co = solve_coords(cols, img)640        t += co[len(Bbasis) + j]