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

1361def vsub(a, b):1362    return [x - y for x, y in zip(a, b)]1363 1364 1365def cross(a, b):1366    return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]1367 1368 1369def vnorm(a):1370    return math.sqrt(sum(x * x for x in a))1371 1372 1373def gauss_link(c1, c2, n=200):1374    """Gauss linking integral of two closed curves given as functions on [0, 2 pi)"""1375    h = 2 * PI / n1376    P = [c1((i + 0.5) * h) for i in range(n)]1377    Q = [c2((i + 0.5) * h) for i in range(n)]1378    dP = [vsub(c1((i + 1) * h), c1(i * h)) for i in range(n)]1379    dQ = [vsub(c2((i + 1) * h), c2(i * h)) for i in range(n)]1380    tot = 0.01381    for i in range(n):1382        for j in range(n):1383            r = vsub(P[i], Q[j])1384            d = vnorm(r)1385            tot += dot(r, cross(dP[i], dQ[j])) / d ** 31386    return tot / (4 * PI)1387 1388 1389sec('7. General position')1390 1391 1392def seg_cross(p, q, r, s):1393    def o(a, b, c):1394        return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])1395    return o(p, q, r) * o(p, q, s) < 0 and o(r, s, p) * o(r, s, q) < 01396 1397 1398def trefoil(t):1399    return [math.sin(t) + 2 * math.sin(2 * t), math.cos(t) - 2 * math.cos(2 * t), -math.sin(3 * t)]1400 1401 1402def crossings(direction, n=240):1403    d = [x / vnorm(direction) for x in direction]1404    a = cross(d, [0.3, 0.5, 0.81])1405    a = [x / vnorm(a) for x in a]1406    b = cross(d, a)1407    pts = [(dot(trefoil(2 * PI * i / n), a), dot(trefoil(2 * PI * i / n), b)) for i in range(n)]1408    c = 01409    for i in range(n):1410        for j in range(i + 2, n):1411            if i == 0 and j == n - 1:1412                continue1413            if seg_cross(pts[i], pts[(i + 1) % n], pts[j], pts[(j + 1) % n]):1414                c += 11415    return c1416 1417 1418cz = crossings([0, 0, 1])1419cr = [crossings([random.gauss(0, 1) for _ in range(3)]) for _ in range(12)]1420check('M1 a generic projection of a closed curve from space to the plane has finitely many transverse double points: the trefoil shows 3 from above and never fewer',1421      cz == 3 and min(cr) >= 3, 'twelve random directions: %s' % sorted(cr))1422 1423 1424def sard_measure(delta):1425    # f(x) = x^3 - 3x; the set |f'| < delta consists of intervals around +-1; total length of the image1426    lo, hi = math.sqrt(1 - delta / 3), math.sqrt(1 + delta / 3)1427    f = lambda x: x ** 3 - 3 * x1428    return 2 * (max(f(lo), f(hi)) - f(1.0))1429 1430 1431sm = [sard_measure(d) / d ** 2 for d in (0.1, 0.01, 0.001)]1432check('M2 Sard, quantitatively: for x^3 - 3x the image of the set where |f\'| < delta has length delta^2/6 + O(delta^3)',1433      all(close(v, 1 / 6, 0.03) for v in sm) and abs(sm[2] - 1 / 6) < abs(sm[0] - 1 / 6), 'length / delta^2 = %s' % ', '.join('%.5f' % v for v in sm))1434 1435 1436def umbrella_rank(x, y):1437    J = [[1, 0], [y, x], [0, 2 * y]]1438    minors = [J[0][0] * J[1][1] - J[0][1] * J[1][0], J[0][0] * J[2][1] - J[0][1] * J[2][0], J[1][0] * J[2][1] - J[1][1] * J[2][0]]1439    return 2 if any(m != 0 for m in minors) else 11440 1441 1442grid = [Fr(i, 4) for i in range(-8, 9)]1443sing = [(x, y) for x in grid for y in grid if umbrella_rank(x, y) < 2]1444dbl = all((0, 0 * y, y * y) == (0, 0 * (-y), (-y) * (-y)) for y in grid)1445check('M3 the Whitney umbrella (x, xy, y^2) fails to be an immersion only at the origin and identifies (0,y) with (0,-y)', sing == [(Fr(0), Fr(0))] and dbl,1446      'rank 2 at the other %d grid points' % (len(grid) ** 2 - 1))1447 1448 1449def height(th, ph, R=2.0, r=1.0):1450    return (R + r * math.cos(th)) * math.cos(ph)1451 1452 1453idx = []1454for th in (0.0, PI):1455    for ph in (0.0, PI):1456        hxx = d2(lambda t: height(t, ph), th)1457        hyy = d2(lambda p: height(th, p), ph)1458        hxy = (height(th + 1e-4, ph + 1e-4) - height(th + 1e-4, ph - 1e-4) - height(th - 1e-4, ph + 1e-4) + height(th - 1e-4, ph - 1e-4)) / 4e-81459        g = max(abs(d1(lambda t: height(t, ph), th)), abs(d1(lambda p: height(th, p), ph)))1460        det = hxx * hyy - hxy * hxy