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

1681 1682def qconj(a):1683    return (a[0], -a[1], -a[2], -a[3])1684 1685 1686def qn2(a):1687    return sum(x * x for x in a)1688 1689 1690def qrot(q):1691    """matrix of v -> q v q^{-1} on the imaginary quaternions"""1692    n = qn2(q)1693    cols = []1694    for k in range(3):1695        v = tuple([0] + [1 if i == k else 0 for i in range(3)])1696        w = qmul(qmul(q, v), qconj(q))1697        cols.append([w[1] / n, w[2] / n, w[3] / n])1698    return [[cols[j][i] for j in range(3)] for i in range(3)]1699 1700 1701def rq():1702    return tuple(Fr(random.randint(-5, 5), random.randint(1, 4)) for _ in range(4))1703 1704 1705okq = True1706for _ in range(100):1707    a, b, c = rq(), rq(), rq()1708    okq = okq and qmul(qmul(a, b), c) == qmul(a, qmul(b, c)) and qn2(qmul(a, b)) == qn2(a) * qn2(b)1709check('R1 the quaternions are associative and their norm is multiplicative', okq, '100 random rational triples, exactly')1710 1711okr = True1712for _ in range(60):1713    a, b = rq(), rq()1714    if qn2(a) == 0 or qn2(b) == 0:1715        continue1716    A, B, AB = qrot(a), qrot(b), qrot(qmul(a, b))1717    I3 = [[Fr(int(i == j)) for j in range(3)] for i in range(3)]1718    det = (A[0][0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1]) - A[0][1] * (A[1][0] * A[2][2] - A[1][2] * A[2][0]) + A[0][2] * (A[1][0] * A[2][1] - A[1][1] * A[2][0]))1719    okr = okr and mat_mul([list(r) for r in zip(*A)], A) == I3 and det == 1 and mat_mul(A, B) == AB and qrot(tuple(-x for x in a)) == A1720    okr = okr and ((A == I3) == (a[1] == a[2] == a[3] == 0))1721check('R2 q -> (v -> q v q^-1) is a homomorphism onto rotations with kernel the real quaternions: the 3-sphere covers SO(3) twice', okr, '60 random rational pairs, exactly')1722 1723 1724def mat_to_quat(R):1725    t = R[0][0] + R[1][1] + R[2][2]1726    cands = [(1 + t, R[2][1] - R[1][2], R[0][2] - R[2][0], R[1][0] - R[0][1]),1727             (R[2][1] - R[1][2], 1 + R[0][0] - R[1][1] - R[2][2], R[0][1] + R[1][0], R[0][2] + R[2][0]),1728             (R[0][2] - R[2][0], R[0][1] + R[1][0], 1 - R[0][0] + R[1][1] - R[2][2], R[1][2] + R[2][1]),1729             (R[1][0] - R[0][1], R[0][2] + R[2][0], R[1][2] + R[2][1], 1 - R[0][0] - R[1][1] + R[2][2])]1730    q = max(cands, key=lambda c: sum(x * x for x in c))1731    n = math.sqrt(sum(x * x for x in q))1732    return [x / n for x in q]1733 1734 1735def axis_rot(u, th):1736    n = vnorm(u)1737    q = (math.cos(th / 2),) + tuple(math.sin(th / 2) * x / n for x in u)1738    return [[float(v) for v in r] for r in qrot(q)]1739 1740 1741def lift_class(loop, n=2000):1742    """0 if the lift of a loop of rotations to unit quaternions closes, 1 if it ends at minus the start"""1743    q0 = mat_to_quat(loop(0.0))1744    q = q01745    for i in range(1, n + 1):1746        c = mat_to_quat(loop(2 * PI * i / n))1747        if sum(a * b for a, b in zip(c, q)) < 0:1748            c = [-x for x in c]1749        q = c1750    return 0 if sum(a * b for a, b in zip(q, q0)) > 0 else 11751 1752 1753u, v = [0.3, -0.5, 0.8], [0.7, 0.6, -0.2]1754cls = {(a, b): lift_class(lambda t, a=a, b=b: [[float(x) for x in r] for r in mat_mul(axis_rot(u, a * t), axis_rot(v, b * t))]) for a, b in ((1, 0), (2, 0), (1, 1), (1, 2), (3, 2), (0, 0))}1755check('R3 the fundamental group of SO(3) has order two: a full turn lifts to a path from 1 to -1, two full turns to a closed path; the class of a product of turns is the parity of their number',1756      all(cls[k] == (k[0] + k[1]) % 2 for k in cls), 'classes %s' % cls)1757 1758 1759def rp_homology(n):1760    out = []1761    for k in range(n + 1):1762        dk = (1 + (-1) ** k) if k >= 1 else None1763        dk1 = (1 + (-1) ** (k + 1)) if k + 1 <= n else None1764        rank_k = 1 if (dk is not None and dk != 0) else 01765        rank_k1 = 1 if (dk1 is not None and dk1 != 0) else 01766        out.append((1 - rank_k - rank_k1, [abs(dk1)] if dk1 not in (None, 0) and abs(dk1) > 1 else []))1767    return out1768 1769 1770check('R4 SO(3) is projective 3-space: its cellular complex Z <-0- Z <-2- Z <-0- Z has homology Z, Z/2, 0, Z',1771      rp_homology(3) == [(1, []), (0, [2]), (0, []), (1, [])] and rp_homology(2) == homology(RP2), 'projective plane from the same recipe: %s' % rp_homology(2))1772 1773 1774def bernoulli(m):1775    B = [Fr(1)]1776    for n in range(1, m + 1):1777        B.append(-sum(Fr(math.comb(n + 1, k)) * B[k] for k in range(n)) / (n + 1))1778    return B1779 1780