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 881–980 of 1604. Source text is displayed literally.
881 882 883def six(R):884 return [R[0][0], R[1][0], R[2][0], R[0][1], R[1][1], R[2][1]]885 886 887def gram_schmidt(s):888 a1, a2 = s[:3], s[3:]889 n1 = vnorm(a1)890 b1 = [x / n1 for x in a1]891 d = sum(x * y for x, y in zip(b1, a2))892 c = [y - d * x for x, y in zip(b1, a2)]893 n2 = vnorm(c)894 b2 = [x / n2 for x in c]895 b3 = [b1[1] * b2[2] - b1[2] * b2[1], b1[2] * b2[0] - b1[0] * b2[2], b1[0] * b2[1] - b1[1] * b2[0]]896 return [[b1[i], b2[i], b3[i]] for i in range(3)]897 898 899def fro(A, B):900 return math.sqrt(sum((a - b) ** 2 for r, q in zip(A, B) for a, b in zip(r, q)))901 902 903Nst = 2000904u0 = [0.3, -0.5, 0.8]905jq = je = jm = j6 = 0.0906prevR = prevq = preve = None907for i in range(Nst + 1):908 th = 2 * PI * i / Nst909 R = qrotf(axis_q(u0, th))910 Ry = qrotf(axis_q([0, 1, 0], th))911 q, e = canon(axis_q(u0, th)), euler(Ry)912 if prevR is not None:913 jm = max(jm, fro(R, prevR))914 jq = max(jq, vnorm([a - b for a, b in zip(q, prevq)]))915 je = max(je, max(abs(a - b) for a, b in zip(e, preve)))916 j6 = max(j6, vnorm([a - b for a, b in zip(six(R), six(prevR))]))917 prevR, prevq, preve = R, q, e918check('O4 along a full turn the rotation matrix moves by small steps, the canonical quaternion jumps by 2 and the Euler angles by pi; the six-dimensional code does not jump',919 jm < 0.01 and close(jq, 2.0, 1e-3) and close(je, PI, 1e-2) and j6 <= jm + 1e-12,920 'largest step over 2000 steps: matrix %.4f, quaternion %.4f, Euler angles %.4f, 6D code %.4f' % (jm, jq, je, j6))921 922ok6 = True923lip = 0.0924for _ in range(200):925 q = [random.gauss(0, 1) for _ in range(4)]926 n = vnorm(q)927 R = qrotf(tuple(a / n for a in q))928 ok6 = ok6 and fro(gram_schmidt(six(R)), R) < 1e-12929 noise = [random.gauss(0, 1e-3) for _ in range(6)]930 R2 = gram_schmidt([a + b for a, b in zip(six(R), noise)])931 lip = max(lip, fro(R2, R) / vnorm(noise))932check('O5 the six-dimensional code (two columns, then Gram-Schmidt) is a continuous right inverse with a Lipschitz decoder', ok6 and lip < 3.0,933 '200 random rotations recovered to 1e-12; decoder moves by at most %.2f times a perturbation of the code' % lip)934 935 936def link_cycle(K, v):937 edges = [tuple(a for a in t if a != v) for t in K.S[2] if v in t]938 nxt = {}939 for a, b in edges:940 nxt.setdefault(a, []).append(b)941 nxt.setdefault(b, []).append(a)942 start = edges[0][0]943 cyc = [start]944 prev, cur = None, start945 while True:946 cand = [w for w in nxt[cur] if w != prev]947 if not cand:948 break949 w = cand[0]950 if w == start:951 break952 cyc.append(w)953 prev, cur = cur, w954 return cyc955 956 957def index_sum(K):958 h = {x[0]: random.random() for x in K.S[0]}959 tot = 0960 for x in K.S[0]:961 v = x[0]962 cyc = link_cycle(K, v)963 s = [h[w] > h[v] for w in cyc]964 changes = sum(1 for i in range(len(s)) if s[i] != s[(i + 1) % len(s)])965 tot += Fr(2 - changes, 2)966 return tot967 968 969surfs = [('sphere', simplex_boundary(2), 2), ('icosahedron', icosahedron(), 2), ('torus', torus7(), 0), ('projective plane', rp2(), 1), ('Klein bottle', klein(), 0)]970okidx = all(index_sum(K) == chi for _, K, chi in surfs for _ in range(100))971check('O6 hairy ball on meshes: the indices of the critical vertices of any generic function on a triangulated surface add up to the Euler characteristic',972 okidx, '100 random functions on each of five surfaces; sums 2, 2, 0, 1, 0')973 974okcp = True975for _ in range(50):976 k = 6977 xs = sorted(random.random() for _ in range(k))978 knots = [0.0] + xs + [1.0]979 ys = [random.gauss(0, 1) for _ in knots]980 slopes = [(ys[i + 1] - ys[i]) / (knots[i + 1] - knots[i]) for i in range(len(knots) - 1)]