Independent scholarship · Open accessThe monthly digest

Indexed text attachment

verify-pde.py

Source for Partial Differential Equations in Higher Categories

View paper · Download original file

Lines 741–840 of 1640. Source text is displayed literally.

741 742X_, Y_ = pvar(0, 2), pvar(1, 2)743rot = [padd(pscale(X_, Fr(3, 5)), pscale(Y_, Fr(-4, 5))), padd(pscale(X_, Fr(4, 5)), pscale(Y_, Fr(3, 5)))]744shear = [padd(X_, Y_), Y_]745sq = [padd(pmul(X_, X_), pmul(Y_, Y_), -1), pscale(pmul(X_, Y_), 2)]            # z -> z^2746p_ = {(4, 1): Fr(2), (1, 2): Fr(-3), (0, 3): Fr(1), (2, 0): Fr(5)}747u3 = {(3, 0): Fr(1), (1, 2): Fr(-3)}748jac = pscale(padd(pmul(X_, X_), pmul(Y_, Y_)), 4)                               # |f'|^2 = 4|z|^2749check('H4 the Laplacian is natural for isometries and, in the plane, for conformal maps up to the factor |f\'|^2 (exact polynomial identities);'750      ' it is not natural for a shear', plap(pcomp(p_, rot)) == pcomp(plap(p_), rot) and plap(pcomp(p_, shear)) != pcomp(plap(p_), shear)751      and plap(pcomp(u3, sq)) == {} and plap(pcomp(p_, sq)) == pmul(jac, pcomp(plap(p_), sq)))752 753 754def period(f, r=1.5):755    g = lambda th: (-d1(lambda y: f(r * math.cos(th), y), r * math.sin(th)) * (-r * math.sin(th)) + d1(lambda x: f(x, r * math.sin(th)), r * math.cos(th)) * (r * math.cos(th)))756    return simpson(g, 0, 2 * PI, 400)757 758 759pers = [period(lambda x, y: 0.5 * math.log(x * x + y * y)), period(lambda x, y: x * x - y * y), period(lambda x, y: x / (x * x + y * y))]760check('H5 the annulus has first cohomology: the conjugate differential of log r has period 2 pi, so log r has no harmonic conjugate there;'761      ' the periods of r^2 cos 2theta and r^{-1} cos theta vanish', abs(pers[0] - 2 * PI) < 1e-8 and abs(pers[1]) < 1e-8 and abs(pers[2]) < 1e-8)762beta = 1.5 * PI763 764 765def wedge(x, y):766    r, th = math.hypot(x, y), math.atan2(y, x) % (2 * PI)767    return r ** (PI / beta) * math.sin(PI * th / beta)768 769 770lapw = d2(lambda x: wedge(x, 0.4), -0.3) + d2(lambda y: wedge(-0.3, y), 0.4)771gradr = lambda r: math.hypot(d1(lambda x: wedge(x, r * math.sin(1.0)), r * math.cos(1.0), r * 1e-3), d1(lambda y: wedge(r * math.cos(1.0), y), r * math.sin(1.0), r * 1e-3))772check('H6 a re-entrant corner of angle 3 pi / 2: r^{2/3} sin(2 theta / 3) is harmonic and vanishes on both edges, and its gradient grows like r^{-1/3}',773      abs(lapw) < 1e-6 and abs(wedge(1.0, 0.0)) < 1e-15 and abs(wedge(0.0, -1.0)) < 1e-12 and abs(gradr(1e-3) / gradr(1e-6) - 0.1) < 1e-6)774m = 7775top = lambda i, j: 1.0 if i == -1 else 0.0776hp, A7, b7, idx7 = grid_dirichlet(m, top)777pcur = [0.0] * (m * m)778for _ in range(1500):                                         # run the absorbing walk: p <- (neighbours + boundary hits) / 4779    pcur = [(b7[r] - sum(A7[r][c] * pcur[c] for c in range(m * m) if c != r)) / 4.0 for r in range(m * m)]780check('H7 Dirichlet\'s problem is a hitting problem: the probability that a random walk on a 7 x 7 grid leaves through the top side solves the'781      ' five-point equation, and from the centre it is 1/4', max(abs(a - b) for a, b in zip(hp, pcur)) < 1e-10 and abs(hp[idx7[(3, 3)]] - 0.25) < 1e-12)782 783sec('7. Green\'s identities and Green\'s functions')784n = 8785Dm = [[(1.0 if j == i else -1.0 if j == i - 1 else 0.0) for j in range(n)] for i in range(n + 1)]786check('G1 Green\'s first identity on a grid: the Dirichlet Laplacian is D^T D, so (v, Lu) = (Dv, Du)',787      all(abs(mm(tr(Dm), Dm)[i][j] - lap1(n)[i][j]) < 1e-15 for i in range(n) for j in range(n)))788z = [random.uniform(-1, 1) for _ in range(36)]789en = lambda w, bv_: sum((w[idx_[(i, j)]] - (w[idx_[(i + di, j + dj)]] if (i + di, j + dj) in idx_ else bv_[(i + di, j + dj)])) ** 2 * (0.5 if (i + di, j + dj) in idx_ else 1.0)790                        for (i, j) in idx_ for di, dj in ((1, 0), (-1, 0), (0, 1), (0, -1)))791zero_b = {k: 0.0 for k in bv}792Eu, Ew, Ez = en(sol_, bv), en([a + b for a, b in zip(sol_, z)], bv), en(z, zero_b)793check('G2 Dirichlet\'s principle: the harmonic function minimizes the energy among functions with its boundary values, and the excess energy'794      ' of a competitor is the energy of the difference', abs(Ew - Eu - Ez) < 1e-10 and Ez > 0)795Gm = inv(lap1(n))796check('G3 the Green\'s function is symmetric and positive: the inverse of the Dirichlet Laplacian on 8 points is min(i,j)(9 - max(i,j))/9',797      all(abs(Gm[i][j] - min(i + 1, j + 1) * (n + 1 - max(i + 1, j + 1)) / (n + 1)) < 1e-12 for i in range(n) for j in range(n)))798dist = lambda p, q: math.sqrt(sum((a - b) ** 2 for a, b in zip(p, q)))799Ghs = lambda x, x0: -1 / (4 * PI * dist(x, x0)) + 1 / (4 * PI * dist(x, (x0[0], x0[1], -x0[2])))800z0 = 0.7801mass = simpson(lambda s: (lambda rho: rho * z0 / (rho * rho + z0 * z0) ** 1.5 * z0 / (1 - s) ** 2)(z0 * s / (1 - s)) if s < 1 else 1.0, 0, 1, 2000)802check('G4 the half-space by reflection: the Green\'s function vanishes on the plane, is symmetric, and its normal derivative has total mass one',803      abs(Ghs((0.3, -1.2, 0.0), (0.1, 0.2, z0))) < 1e-16 and abs(Ghs((0.3, -1.2, 0.4), (0.1, 0.2, z0)) - Ghs((0.1, 0.2, z0), (0.3, -1.2, 0.4))) < 1e-16 and abs(mass - 1) < 1e-9)804a_ = 1.0805 806 807def Gball(x, x0):808    r0 = math.sqrt(sum(v * v for v in x0))809    xs = tuple(v * (a_ / r0) ** 2 for v in x0)810    return -1 / (4 * PI * dist(x, x0)) + (a_ / r0) / (4 * PI * dist(x, xs))811 812 813r0 = 0.6814pk = lambda th: (a_ * a_ - r0 * r0) / (4 * PI * a_ * (a_ * a_ + r0 * r0 - 2 * a_ * r0 * math.cos(th)) ** 1.5) * 2 * PI * a_ * a_ * math.sin(th)815onb = (math.sin(1.1) * math.cos(0.4), math.sin(1.1) * math.sin(0.4), math.cos(1.1))816check('G5 the ball by inversion: the Green\'s function vanishes on the sphere; the Poisson kernel has mass one and returns the value r0 of the'817      ' harmonic function z at distance r0 from the centre', abs(Gball(onb, (0.2, -0.3, 0.5))) < 1e-15 and818      abs(simpson(pk, 0, PI, 2000) - 1) < 1e-10 and abs(simpson(lambda th: pk(th) * a_ * math.cos(th), 0, PI, 2000) - r0) < 1e-10)819 820 821def rand_lap(nv, dens=0.7):822    Lr = [[0.0] * nv for _ in range(nv)]823    for i in range(nv):824        for j in range(i + 1, nv):825            if random.random() < dens:826                w = random.uniform(0.5, 2.0)827                Lr[i][j] -= w828                Lr[j][i] -= w829                Lr[i][i] += w830                Lr[j][j] += w831    return Lr832 833 834def kron_red(Lr, keep):835    rest = [i for i in range(len(Lr)) if i not in keep]836    if not rest:837        return [[Lr[i][j] for j in keep] for i in keep]838    Lii = [[Lr[i][j] for j in rest] for i in rest]839    Lib = [[Lr[i][j] for j in keep] for i in rest]840    sol = tr([solve(Lii, [Lib[r][c] for r in range(len(rest))]) for c in range(len(keep))])     # Lii^{-1} Lib