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 800–899 of 1640. Source text is displayed literally.

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} Lib841    return [[Lr[keep[a]][keep[b]] - sum(Lr[keep[a]][rest[r]] * sol[r][b] for r in range(len(rest))) for b in range(len(keep))] for a in range(len(keep))]842 843 844okg = True845for _ in range(10):846    # network 1 on vertices A(2) + M(2) + interior(3); network 2 on M(2) + C(2) + interior(3)847    L1, L2 = rand_lap(7), rand_lap(7)848    big = [[0.0] * 12 for _ in range(12)]                    # order: A0 A1 M0 M1 C0 C1 | I1(3) | I2(3)849    map1 = [0, 1, 2, 3, 6, 7, 8]850    map2 = [2, 3, 4, 5, 9, 10, 11]851    for Lr, mp in ((L1, map1), (L2, map2)):852        for i in range(7):853            for j in range(7):854                big[mp[i]][mp[j]] += Lr[i][j]855    direct = kron_red(big, [0, 1, 4, 5])856    K1, K2 = kron_red(L1, [0, 1, 2, 3]), kron_red(L2, [0, 1, 2, 3])857    mid = [[0.0] * 6 for _ in range(6)]858    for i in range(4):859        for j in range(4):860            mid[[0, 1, 2, 3][i]][[0, 1, 2, 3][j]] += K1[i][j]861            mid[[2, 3, 4, 5][i]][[2, 3, 4, 5][j]] += K2[i][j]862    comp = kron_red(mid, [0, 1, 4, 5])863    okg = okg and max(abs(direct[i][j] - comp[i][j]) for i in range(4) for j in range(4)) < 1e-10864ser = kron_red([[2.0, -2.0, 0.0], [-2.0, 5.0, -3.0], [0.0, -3.0, 3.0]], [0, 2])865check('G6 gluing is composition: the Dirichlet-to-Neumann matrix of two random networks glued along shared terminals is the composite of'866      ' their Dirichlet-to-Neumann matrices (ten trials); conductances 2 and 3 in series give 6/5', okg and abs(ser[0][1] + 1.2) < 1e-12)867uex = lambda x: x * (1 - x) / 2868coef = [simpson(lambda x: 1.0 * math.sin(k * PI * x), 0, 1, 400) / ((k * PI) ** 2 * 0.5) for k in (1, 2, 3)]869ug = lambda x: sum(c_ * math.sin((k + 1) * PI * x) for k, c_ in enumerate(coef))870orth = max(abs(simpson(lambda x: (d1(uex, x) - d1(ug, x)) * (k * PI) * math.cos(k * PI * x), 0, 1, 4000)) for k in (1, 2, 3))871check('G7 Galerkin approximation is orthogonal projection in the energy inner product: the error of the three-mode approximation to x(1-x)/2 is'872      ' energy-orthogonal to the trial space', orth < 1e-8, 'coefficients %.6f, %.6f, %.6f' % tuple(coef))873G1d = lambda x, y: x * (1 - y) if x < y else y * (1 - x)874gq = lambda f, x: simpson(lambda y: G1d(x, y) * f(y), 0, x, 400) + simpson(lambda y: G1d(x, y) * f(y), x, 1, 400)875check('G8 in one dimension the Green\'s function of -d^2/dx^2 on (0,1) is x(1-y) for x < y: it inverts the operator on 1 and on sin(pi x)',876      abs(gq(lambda y: 1.0, 0.3) - 0.105) < 1e-12 and abs(gq(lambda y: math.sin(PI * y), 0.3) - math.sin(0.3 * PI) / PI ** 2) < 1e-9)877 878sec('8. Waves and quanta in space')879c = 1.5880urad = lambda r, t: math.exp(-(r - c * t - 3) ** 2) / r881res = abs(d2(lambda t: urad(2.0, t), 0.4) - c * c * (d2(lambda r: urad(r, 0.4), 2.0) + 2 * d1(lambda r: urad(r, 0.4), 2.0) / 2.0))882pw = lambda nv: (lambda x, y, z, t: math.sin(3 * (t - (nv[0] * x + nv[1] * y + nv[2] * z) / c)))883def wres(u):884    return abs(d2(lambda t: u(0.2, 0.1, -0.3, t), 0.5) - c * c * (d2(lambda x: u(x, 0.1, -0.3, 0.5), 0.2) + d2(lambda y: u(0.2, y, -0.3, 0.5), 0.1) + d2(lambda z: u(0.2, 0.1, z, 0.5), -0.3)))885check('S1 waves in space: F(r - ct)/r solves the wave equation, and f(t - gamma(x)) does for a plane phase exactly when |grad gamma| = 1/c'886      ' (the source\'s characteristic condition)', res < 1e-6 and wres(pw((0.6, 0.0, 0.8))) < 1e-5 and wres(pw((0.6, 0.0, 0.9))) > 0.1)887c = 1.0888bumpr = lambda r: (math.exp(-1 / (1 - r * r)) if r < 1 else 0.0)889R = 5.0890kir = lambda t: t * 0.5 * simpson(lambda mu: bumpr(math.sqrt(R * R + t * t + 2 * R * t * mu)), -1, 1, 4000)891tot = simpson(lambda r: 2 * PI * r * bumpr(r), 0, 1, 2000)892def flat(t):893    f = lambda rho, th: bumpr(rho) * rho / math.sqrt(t * t - (R * R + rho * rho - 2 * R * rho * math.cos(th)))894    return simpson(lambda rho: simpson(lambda th: f(rho, th), 0, 2 * PI, 60), 0, 1, 60) / (2 * PI)895tails = [t * flat(t) * 2 * PI / tot for t in (8.0, 80.0, 800.0)]896check('S2 Huygens: in three dimensions a source in the unit ball is heard at distance 5 only for 4 < ct < 6; in two dimensions the signal'897      ' has a tail decaying like 1/t for ever', kir(3.0) == 0.0 and kir(5.0) > 1e-3 and kir(6.5) == 0.0 and all(v > 0 for v in tails) and abs(tails[2] - 1) < 1e-3,898      'three dimensions, ct = 3, 5, 6.5: %.4f, %.4f, %.4f; two dimensions, t u(t) relative to its limit at t = 8, 80, 800: %.4f, %.4f, %.4f' % (kir(3.0), kir(5.0), kir(6.5), *tails))899K = lambda x, t: cmath.exp(1j * x * x / (2 * t)) / cmath.sqrt(2j * PI * t)