Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 860–959 of 1640. Source text is displayed literally.
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)900resK = abs(1j * d1(lambda t: K(0.7, t), 0.9) + 0.5 * d2(lambda x: K(x, 0.9), 0.7))901heatc = lambda x, tau: cmath.exp(-x * x / (2 * tau)) / cmath.sqrt(2 * PI * tau) # heat kernel with diffusivity 1/2 at complex time902pk_ = lambda x, t: cmath.exp(-x * x / (2 * (1 + 1j * t))) / cmath.sqrt(1 + 1j * t)903norms = [simpson(lambda x: abs(pk_(x, t)) ** 2, -40, 40, 8000) for t in (0.0, 2.0, 5.0)]904widths = [simpson(lambda x: x * x * abs(pk_(x, t)) ** 2, -60, 60, 12000) / norms[i] for i, t in enumerate((0.0, 2.0, 5.0))]905check('S3 the Schroedinger kernel is the heat kernel at imaginary time; the flow is unitary where the heat flow is a contraction, and a Gaussian'906 ' packet spreads with mean square width (1 + t^2)/2', resK < 1e-6 and abs(K(0.7, 0.9) - heatc(0.7, 0.9j)) < 1e-15907 and max(norms) - min(norms) < 1e-10 and all(abs(w - (1 + t * t) / 2) < 1e-8 for w, t in zip(widths, (0.0, 2.0, 5.0))))908okh, polys = True, []909for nq in (1, 2, 3, 4):910 be = Fr(1, nq)911 a = [Fr(1)]912 for k in range(1, 8):913 a.append((be * k - 1) * a[-1] / Fr(k * (k + 1), 2))914 okh = okh and all(v == 0 for v in a[nq:]) and a[nq - 1] != 0915 Rf = lambda r, a=a, be=float(be): sum(float(v) * r ** k for k, v in enumerate(a)) * math.exp(-be * r)916 for r in (0.7, 2.3, 6.0):917 okh = okh and abs(-d2(Rf, r) - 2 * d1(Rf, r) / r - 2 * Rf(r) / r + float(be) ** 2 * Rf(r)) < 1e-7918 polys.append([str(v) for v in a[:nq]])919check('S4 the hydrogen atom in the source\'s units: the series terminates exactly when beta = 1/n, the radial states satisfy the equation with'920 ' eigenvalue -1/n^2, and level n has multiplicity n^2', okh and all(sum(2 * l + 1 for l in range(nq)) == nq * nq for nq in range(1, 6)), 'polynomials %s' % polys)921h = 0.02922xs = [-10 + h * (i + 1) for i in range(999)]923ho = tri_eigs([2 / h ** 2 + x * x for x in xs], [-1 / h ** 2] * 998, 4)924check('S5 the harmonic oscillator -v\'\' + x^2 v = lambda v has eigenvalues 1, 3, 5, 7', all(abs(v - (2 * k + 1)) < 2e-3 for k, v in enumerate(ho)), '%.5f, %.5f, %.5f, %.5f' % tuple(ho))925 926sec('9. Symmetry and special functions')927X1, X2, X3 = pvar(0, 3), pvar(1, 3), pvar(2, 3)928Lop = [lambda p: padd(pmul(X2, pdiff(p, 2)), pmul(X3, pdiff(p, 1)), -1),929 lambda p: padd(pmul(X3, pdiff(p, 0)), pmul(X1, pdiff(p, 2)), -1),930 lambda p: padd(pmul(X1, pdiff(p, 1)), pmul(X2, pdiff(p, 0)), -1)]931Eul = lambda p: {k: v * sum(k) for k, v in p.items() if sum(k)}932r2 = padd(padd(pmul(X1, X1), pmul(X2, X2)), pmul(X3, X3))933oky = all(kernel_dim(plap, 3, l) == 2 * l + 1 for l in range(8))934okc = True935for _ in range(6):936 p = {(random.randint(0, 3), random.randint(0, 3), random.randint(0, 3)): Fr(random.randint(-4, 4)) for _ in range(5)}937 p = {k: v for k, v in p.items() if v != 0} or {(1, 1, 0): Fr(1)}938 L2p = {}939 for Lo in Lop:940 L2p = padd(L2p, Lo(Lo(p)), -1) # L^2 = -(L1^2 + L2^2 + L3^2)941 EEp = padd(Eul(Eul(p)), Eul(p))942 okc = okc and pmul(r2, plap(p)) == padd(EEp, L2p, -1) # r^2 Laplace = E(E+1) - L^2943 okc = okc and padd(Lop[0](Lop[1](p)), Lop[1](Lop[0](p)), -1) == pscale(Lop[2](p), -1) # [L1, L2] = -L3944 okc = okc and all(plap(Lo(p)) == Lo(plap(p)) for Lo in Lop)945check('Y1 rotations: harmonic homogeneous polynomials of degree l in three variables form a space of dimension 2l+1; the rotation fields'946 ' satisfy [L1, L2] = -L3 and commute with the Laplacian; and r^2 Laplace = E(E+1) - L^2, so L^2 = l(l+1) on that space (exact)', oky and okc)947 948 949def pol_mul(a, b):950 r = [Fr(0)] * (len(a) + len(b) - 1)951 for i, x in enumerate(a):952 for j, y in enumerate(b):953 r[i + j] += x * y954 return r955 956 957pol_int = lambda a: sum(c * (Fr(1) - Fr(-1) ** (k + 1)) / (k + 1) for k, c in enumerate(a))958pol_d = lambda a: [k * c for k, c in enumerate(a)][1:] or [Fr(0)]959okl = True