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 744–843 of 1604. Source text is displayed literally.
744 745 746def grid_torus(m, d):747 """Freudenthal triangulation of (Z/m)^d"""748 def num(v):749 r = 0750 for a in v:751 r = r * m + a % m752 return r753 F = []754 for v in itertools.product(range(m), repeat=d):755 for perm in itertools.permutations(range(d)):756 w = list(v)757 s = [num(w)]758 for k in perm:759 w[k] += 1760 s.append(num(w))761 F.append(s)762 return Cx(F)763 764 765def circle(n):766 return Cx([[i, (i + 1) % n] for i in range(n)])767 768 769# ######################## CHECKS FOR THE PAPER ########################770random.seed(20261119)771 772 773def vnorm(a):774 return math.sqrt(sum(x * x for x in a))775 776 777def trig_poly(deg=3):778 co = [(random.gauss(0, 1), random.gauss(0, 1)) for _ in range(deg + 1)]779 return lambda t: sum(a * math.cos(k * t) + b * math.sin(k * t) for k, (a, b) in enumerate(co))780 781 782sec('1. Obstructions')783okbu = True784for _ in range(200):785 e = trig_poly()786 g = lambda t: e(t) - e(t + PI)787 t0 = bisect(g, 0.0, PI)788 okbu = okbu and abs(e(t0) - e(t0 + PI)) < 1e-9789check('O1 Borsuk-Ulam on the circle: every continuous code e: S^1 -> R identifies some pair of antipodal points', okbu, '200 random trigonometric encoders; collision found by bisection each time')790 791 792def cubic_odd():793 lin = [[random.gauss(0, 1) for _ in range(3)] for _ in range(2)]794 cub = [[random.gauss(0, 1) for _ in range(10)] for _ in range(2)]795 mons = [(3, 0, 0), (0, 3, 0), (0, 0, 3), (2, 1, 0), (2, 0, 1), (1, 2, 0), (0, 2, 1), (1, 0, 2), (0, 1, 2), (1, 1, 1)]796 def g(u, v):797 x = (math.sin(u) * math.cos(v), math.sin(u) * math.sin(v), math.cos(u))798 return [sum(l * a for l, a in zip(lin[i], x)) + sum(c * x[0] ** m[0] * x[1] ** m[1] * x[2] ** m[2] for c, m in zip(cub[i], mons)) for i in range(2)]799 return g800 801 802def find_zero_sphere(g):803 grid = []804 for i in range(1, 36):805 for j in range(72):806 u, v = PI * i / 36, 2 * PI * j / 72807 grid.append((vnorm(g(u, v)), u, v))808 grid.sort()809 best = 1.0810 for _, u, v in grid[:25]:811 for _ in range(80):812 f0 = g(u, v)813 h = 1e-6814 fu = [(a - b) / (2 * h) for a, b in zip(g(u + h, v), g(u - h, v))]815 fv = [(a - b) / (2 * h) for a, b in zip(g(u, v + h), g(u, v - h))]816 det = fu[0] * fv[1] - fu[1] * fv[0]817 if abs(det) < 1e-14:818 break819 du = (f0[0] * fv[1] - f0[1] * fv[0]) / det820 dv = (fu[0] * f0[1] - fu[1] * f0[0]) / det821 step = max(abs(du), abs(dv))822 sc = min(1.0, 0.2 / step) if step > 0 else 1.0823 u, v = u - sc * du, v - sc * dv824 best = min(best, vnorm(g(u, v)))825 if best < 1e-11:826 break827 return best828 829 830res2 = [find_zero_sphere(cubic_odd()) for _ in range(40)]831check('O2 Borsuk-Ulam on the sphere: every continuous code e: S^2 -> R^2 identifies a pair of antipodal points', max(res2) < 1e-9,832 '40 random cubic encoders; largest residual |e(x) - e(-x)| at the point found %.1e' % max(res2))833 834 835def bottleneck_radius(e, M=3000, bins=100):836 ts = [2 * PI * i / M for i in range(M)]837 cs = [e(t) for t in ts]838 lo, hi = min(cs), max(cs)839 groups = {}840 for t, c in zip(ts, cs):841 groups.setdefault(min(bins - 1, int((c - lo) / (hi - lo) * bins)), []).append(t)842 best = 0.0843 for b, g in groups.items():