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