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 641–740 of 1604. Source text is displayed literally.
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)721 722 723def moebius():724 return Cx([[i, (i + 1) % 5, (i + 2) % 5] for i in range(5)])725 726 727def simplex_boundary(n):728 return Cx([c for c in itertools.combinations(range(n + 2), n + 1)])729 730 731def cross_sphere(n):732 """boundary of the (n+1)-dimensional cross-polytope; vertex 2i is +e_i, 2i+1 is -e_i"""733 return Cx([[2 * i + s for i, s in enumerate(signs)] for signs in itertools.product((0, 1), repeat=n + 1)])734 735 736def icosahedron():737 T, B = 0, 11738 u = lambda i: 1 + i % 5739 l = lambda i: 6 + i % 5740 F = []