Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 401–500 of 1640. Source text is displayed literally.
401 a11, a12, a22 = (random.randint(-4, 4) for _ in range(3))402 if a11 == a12 == a22 == 0:403 continue404 d = a12 * a12 - a11 * a22405 ok2 = ok2 and kind(inertia([[a11, a12], [a12, a22]])) == ('elliptic' if d < 0 else 'hyperbolic' if d > 0 else 'parabolic')406check('E1 the type of a second-order operator is the inertia of its symbol: invariant under 300 random linear changes of variables,'407 ' and equal to the source\'s discriminant test in two variables', ok and ok2 and arch == ('elliptic', 'hyperbolic', 'parabolic'), 'Laplace, wave, heat: %s, %s, %s' % arch)408pts = [(Fr(2), Fr(3)), (Fr(-2), Fr(-3)), (Fr(1, 2), Fr(1)), (Fr(-1), Fr(4)), (Fr(2), Fr(1, 2)), (Fr(0), Fr(0))]409kinds = [kind(inertia([[y, -1], [-1, x]])) for x, y in pts]410check('E2 the source\'s example y u_xx - 2 u_xy + x u_yy: elliptic where xy > 1, hyperbolic where xy < 1, parabolic on xy = 1',411 kinds == ['elliptic', 'elliptic', 'hyperbolic', 'hyperbolic', 'parabolic', 'hyperbolic'], ', '.join(kinds))412f = lambda s: math.sin(s) + s * s413r1 = max(abs(4 * d1(lambda x: f(-3 * x - 4 * y), x) - 3 * d1(lambda yy: f(-3 * x - 4 * yy), y)) for x, y in [(0.3, -0.7), (1.1, 0.4), (-2.0, 0.9)])414r2 = max(abs(d1(lambda xx: f(math.exp(-xx) * y), x) + y * d1(lambda yy: f(math.exp(-x) * yy), y)) for x, y in [(0.3, -0.7), (1.1, 0.4), (-2.0, 0.9)])415check('E3 first-order equations are solved by pulling back along the quotient by the characteristics: f(-3x-4y) solves 4u_x - 3u_y = 0,'416 ' f(e^{-x} y) solves u_x + y u_y = 0, and the line y = 0 is a characteristic, on which every solution is constant',417 r1 < 1e-8 and r2 < 1e-8 and all(math.exp(-x) * 0.0 == 0.0 for x in (-1.0, 0.5, 3.0)), 'residuals %.1e, %.1e' % (r1, r2))418ok, rows = True, []419for n in (2, 3, 4):420 for k in range(0, 6):421 want = math.comb(n + k - 1, k) - (math.comb(n + k - 3, k - 2) if k >= 2 else 0)422 got = (kernel_dim(plap, n, k), kernel_dim(lambda p: plap(p, [1] * (n - 1) + [-1]), n, k), kernel_dim(lambda p: pdiff(pdiff(p, 0), 0), n, k))423 ok = ok and got == (want, want, want)424 rows.append('n=%d: %s' % (n, [math.comb(n + k - 1, k) - (math.comb(n + k - 3, k - 2) if k >= 2 else 0) for k in range(6)]))425check('E4 formal solutions do not see the type: homogeneous polynomial solutions of degree k of the Laplace, wave and degenerate'426 ' operators have the same dimension C(n+k-1,k) - C(n+k-3,k-2) (exact ranks)', ok, '; '.join(rows))427c = Fr(3, 2)428ok = True429for _ in range(20):430 p = {(random.randint(0, 4), random.randint(0, 4)): Fr(random.randint(-5, 5)) for _ in range(6)}431 p = {k: v for k, v in p.items() if v != 0}432 if not p:433 continue434 A1 = padd(pdiff(p, 0), pdiff(p, 1), c) # (d_t + c d_x) p, variables (t, x)435 lhs = padd(pdiff(A1, 0), pdiff(A1, 1), -c) # (d_t - c d_x)(d_t + c d_x) p436 rhs = padd(pdiff(pdiff(p, 0), 0), pdiff(pdiff(p, 1), 1), -c * c)437 ok = ok and lhs == rhs438nreal = lambda a11, a12, a22: (0 if a12 * a12 < a11 * a22 else 1 if a12 * a12 == a11 * a22 else 2)439check('E5 the type is visible in the real characteristics: none for Laplace, one double for heat, two for wave; and the wave operator'440 ' factors exactly into two transport operators', ok and (nreal(1, 0, 1), nreal(1, 0, 0), nreal(1, 0, -1)) == (0, 1, 2))441ok = True442for k in range(0, 9):443 v = {(k - 2 * j, j): Fr(math.factorial(k), math.factorial(j) * math.factorial(k - 2 * j)) for j in range(k // 2 + 1)}444 ok = ok and padd(pdiff(v, 1), pdiff(pdiff(v, 0), 0), -1) == {}445ratios = [Fr(math.factorial(2 * j + 2), math.factorial(j + 1)) / Fr(math.factorial(2 * j), math.factorial(j)) for j in range(6)]446check('E6 prolongation: the heat polynomials solve u_t = u_xx exactly for degrees 0..8; for the analytic datum 1/(1+x^2) the formal series'447 ' in t at x = 0 has coefficients (2j)!/j! and radius of convergence zero (Kovalevskaya)', ok and ratios == [2 * (2 * j + 1) for j in range(6)],448 'successive ratios %s' % [int(r) for r in ratios])449 450sec('2. Archetypes and well-posedness')451un = lambda n: (lambda x, y: math.exp(-math.sqrt(n)) * math.sin(n * x) * math.sinh(n * y) / n)452res = max(abs(d2(lambda x: un(5)(x, 0.3), 0.7, 1e-3) + d2(lambda y: un(5)(0.7, y), 0.3, 1e-3)) for _ in range(1))453data = [math.exp(-math.sqrt(n)) for n in (25, 100, 400)]454sol = [math.exp(-math.sqrt(n)) * math.sinh(n) / n for n in (25, 100, 400)]455check('W1 Hadamard: Cauchy data for Laplace\'s equation tending to zero with solutions tending to infinity at y = 1',456 res < 1e-6 and data[0] > data[1] > data[2] and sol[0] < sol[1] < sol[2], 'n = 25, 100, 400: data %.1e, %.1e, %.1e; solution %.1e, %.1e, %.1e' % (*data, *sol))457amp = [math.exp(n * n * 0.1) for n in (1, 10, 30)]458check('W2 the heat semigroup is not a group: mode n is damped by exp(-n^2 t) forwards and amplified by exp(n^2 t) backwards', amp[2] > 1e38,459 't = 0.1, n = 1, 10, 30: %.3f, %.3e, %.3e' % tuple(amp))460n = 7461D = [[(-1 if j == i else 1 if j == i + 1 else 0) for j in range(n)] for i in range(n - 1)]462DtD = mm(tr(D), D)463check('W3 conservation plus a constitutive law: the Neumann Laplacian of a path is D^T D for the incidence matrix D, and its columns sum to zero'464 ' (mass is conserved)', all(DtD[i][j] == lap1(n, 'N')[i][j] for i in range(n) for j in range(n)) and all(abs(sum(DtD[i][j] for i in range(n))) < 1e-12 for j in range(n)))465L = lap1(20)466u = [random.uniform(-1, 1) for _ in range(20)]467mono, uu = True, list(u)468for _ in range(200):469 e0 = dot(uu, uu)470 uu = rk4(lambda y: [-z for z in mv(L, y)], uu, 0.01)471 mono = mono and dot(uu, uu) <= e0472st = u + [random.uniform(-1, 1) for _ in range(20)]473energy = lambda s: 0.5 * dot(s[20:], s[20:]) + 0.5 * dot(s[:20], mv(L, s[:20]))474E0 = energy(st)475for _ in range(1000):476 st = rk4(lambda s: s[20:] + [-z for z in mv(L, s[:20])], st, 0.005)477check('W4 the two energy laws on a grid of 20 points: the square norm decreases along the diffusion flow, and the wave energy is conserved',478 mono and abs(energy(st) - E0) < 1e-8 * E0, 'relative drift of the wave energy over time 5: %.1e' % (abs(energy(st) - E0) / E0))479LN = [[int(v) for v in r] for r in lap1(6, 'N')]480f_ok, f_bad = [1, -2, 3, 0, -1, -1], [1, -2, 3, 0, -1, 0]481check('W5 the Neumann problem: kernel the constants, solvable exactly when the source has mean zero (exact ranks)',482 rank_exact(LN) == 5 and rank_exact([r + [b] for r, b in zip(LN, f_ok)]) == 5 and rank_exact([r + [b] for r, b in zip(LN, f_bad)]) == 6)483check('W6 the source\'s first example of non-uniqueness: u\'\' + u = 0, u(0) = u(L) = 0 has a non-zero solution exactly when sin L = 0',484 abs(math.sin(PI)) < 1e-15 and abs(math.sin(2.0)) > 0.5)485 486sec('3. Waves')487c = 2.0488phi = lambda x: math.exp(-x * x)489psi = lambda x: x * math.exp(-x * x)490Psi = lambda x: -0.5 * math.exp(-x * x) # antiderivative of psi491dal = lambda x, t, ph=phi, PS=Psi: 0.5 * (ph(x + c * t) + ph(x - c * t)) + (PS(x + c * t) - PS(x - c * t)) / (2 * c)492pts = [(0.3, 0.2), (-1.1, 0.7), (2.0, 1.3)]493res = max(abs(d2(lambda t: dal(x, t), t) - c * c * d2(lambda xx: dal(xx, t), x)) for x, t in pts)494ini = max(abs(dal(x, 0.0) - phi(x)) + abs(d1(lambda t: dal(x, t), 0.0) - psi(x)) for x in (-1.0, 0.2, 0.9))495check('V1 d\'Alembert\'s formula solves the wave equation with the given data', res < 1e-6 and ini < 1e-9, 'residual %.1e' % res)496par = 0.0497for _ in range(50):498 xi, eta, h, kk = random.uniform(-2, 2), random.uniform(-2, 2), random.uniform(0.1, 1), random.uniform(0.1, 1)499 U = lambda a, b: dal((a + b) / 2, (a - b) / (2 * c))500 par = max(par, abs(U(xi + h, eta + kk) - U(xi + h, eta) - U(xi, eta + kk) + U(xi, eta)))