Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 881–980 of 1640. Source text is displayed literally.
881res = 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 = True960for l in range(8):961 P = legendre(l)962 dP = pol_d(P)963 t = pol_d(pol_mul([Fr(1), Fr(0), Fr(-1)], dP)) # ((1 - s^2) P')'964 lhs = [(t[k] if k < len(t) else 0) + l * (l + 1) * (P[k] if k < len(P) else 0) for k in range(max(len(t), len(P)))]965 okl = okl and all(v == 0 for v in lhs) and sum(P) == 1966 for m_ in range(8):967 okl = okl and pol_int(pol_mul(P, legendre(m_))) == (Fr(2, 2 * l + 1) if l == m_ else 0)968check('Y2 Legendre polynomials: the zonal equation ((1-s^2)P\')\' + l(l+1)P = 0, P(1) = 1, and orthogonality with square norm 2/(2l+1) (exact, l < 8)', okl)969jz = lambda n, a, b: bisect(lambda x: besselj(n, x), a, b)970zeros = [jz(0, 2, 3), jz(1, 3, 4.5), jz(2, 4.5, 5.4), jz(0, 5.4, 6), jz(0, 8, 9)]971bes = max(abs(x * x * d2(lambda s: besselj(n, s), x) + x * d1(lambda s: besselj(n, s), x) + (x * x - n * n) * besselj(n, x)) for n, x in [(0, 1.3), (1, 2.7), (3, 4.1)])972check('Y3 Bessel functions: the equation, and the zeros that give the frequencies of a circular drum',973 bes < 1e-6 and all(abs(a - b) < 1e-6 for a, b in zip(zeros, [2.404826, 3.831706, 5.135622, 5.520078, 8.653728])),974 'zeros %.6f, %.6f, %.6f, %.6f; frequency ratios %.4f, %.4f, %.4f' % (*zeros[:4], zeros[1] / zeros[0], zeros[2] / zeros[0], zeros[3] / zeros[0]))975ea, eb = jacobi_eig(lap1(4))[0], jacobi_eig(lap1(5))[0]976es = jacobi_eig(kron_sum(lap1(4), lap1(5)))[0]977check('Y4 separation of variables: the spectrum of a product is the set of sums (a 4 x 5 grid)',978 max(abs(a - b) for a, b in zip(es, sorted(x + y for x in ea for y in eb))) < 1e-10)979 980