Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 560–659 of 1640. Source text is displayed literally.
560P5, P6 = step_matrix(12, 0.5), step_matrix(12, 0.6)561u = [random.uniform(-1, 1) for _ in range(12)]562grow, alt = True, [(-1.0) ** i for i in range(12)]563for _ in range(30):564 un_ = mv(P5, u)565 grow = grow and max(map(abs, un_)) <= max(map(abs, u)) + 1e-15566 u = un_567a1 = mv(P6, alt)568check('D3 the explicit scheme is a (sub)Markov kernel exactly when s <= 1/2, and then it obeys the maximum principle; at s = 0.6 the'569 ' alternating mode grows by |1 - 4s| in the interior', min(min(r) for r in P5) >= 0 and min(min(r) for r in P6) < 0 and grow and abs(abs(a1[5]) - 1.4) < 1e-12)570Pp = step_matrix(16, 0.3, 'P')571ent = lambda p: -sum(x * math.log(x) for x in p if x > 0)572okH = True573for _ in range(50):574 p = [random.random() for _ in range(16)]575 sp = sum(p)576 p = [x / sp for x in p]577 q = mv(Pp, p)578 okH = okH and ent(q) >= ent(p) - 1e-13 and dot(q, q) <= dot(p, p) + 1e-15 and abs(sum(q) - 1) < 1e-12579check('D4 irreversibility: under a doubly stochastic step entropy does not decrease and the square norm does not increase', okH)580surv = lambda x, t: simpson(lambda y: S(x - y, t) - S(x + y, t), 0, 40, 8000)581neu = lambda x, t: simpson(lambda y: (S(x - y, t) + S(x + y, t)) * math.exp(-(y - 1) ** 2), 0, 40, 8000)582dq = max(abs(surv(x, t) - math.erf(x / math.sqrt(4 * kd * t))) for x, t in [(0.5, 0.3), (2.0, 1.5)])583check('D5 images on the half-line: the odd kernel S(x-y) - S(x+y) vanishes at the wall and gives the survival probability erf(x/sqrt(4kt));'584 ' the even kernel has zero slope there', dq < 1e-9 and abs(surv(0.0, 0.3)) < 1e-15 and abs(d1(lambda x: neu(x, 0.4), 0.0)) < 1e-8)585l = 1.0586 587 588def K_img(x, y, t, M):589 return sum(S(x - y - 2 * m * l, t) - S(x + y - 2 * m * l, t) for m in range(-M, M + 1))590 591 592def K_eig(x, y, t, N):593 return (2 / l) * sum(math.exp(-kd * (n * PI / l) ** 2 * t) * math.sin(n * PI * x / l) * math.sin(n * PI * y / l) for n in range(1, N + 1))594 595 596def terms(fn, x, y, t, ref):597 for M in range(0, 400):598 if abs(fn(x, y, t, M) - ref) < 1e-10:599 return M600 return None601 602 603rows, okK = [], True604for t in (0.001, 0.05, 1.0):605 ref = K_img(0.3, 0.45, t, 60)606 okK = okK and abs(ref - K_eig(0.3, 0.45, t, 2000)) < 1e-10607 rows.append('t=%g: %d image shells, %d modes' % (t, terms(K_img, 0.3, 0.45, t, ref), terms(K_eig, 0.3, 0.45, t, ref)))608check('D6 the heat kernel of an interval two ways: the sum over the reflection group and the sum over eigenfunctions agree'609 ' (Poisson summation); terms needed for accuracy 1e-10', okK, '; '.join(rows))610check('D7 scaling: S(x, t) = t^{-1/2} S(x / sqrt t, 1)', all(abs(S(x, t) - S(x / math.sqrt(t), 1.0) / math.sqrt(t)) < 1e-15 for x, t in [(0.3, 0.2), (2.0, 5.0)]))611far = 0.5 * (math.erfc((10 - 1) / math.sqrt(4 * 0.1)) - math.erfc((10 + 1) / math.sqrt(4 * 0.1)))612check('D8 infinite speed: data supported in [-1, 1] give a positive value at x = 10 after time 0.1 (unit diffusivity)', 0 < far < 1e-80, 'u = %.2e' % far)613S1 = lambda x, t: heat(x, t, 1.0)614inner = simpson(lambda y: S1(0.8 - y, 0.37) * math.sin(y), -14, 15, 4000)615duh = simpson(lambda s: math.exp(-(1.2 - s)), 0.0, 1.2, 200) * math.sin(0.8)616check('D9 Duhamel for diffusion: the semigroup maps sin to e^{-t} sin, and the forced solution is (1 - e^{-t}) sin x',617 abs(inner - math.exp(-0.37) * math.sin(0.8)) < 1e-11 and abs(duh - (1 - math.exp(-1.2)) * math.sin(0.8)) < 1e-10)618 619sec('5. Boundary conditions and Fourier series')620l = 1.3621og = max(abs(simpson(lambda x: math.sin(n * PI * x / l) * math.sin(m * PI * x / l), 0, l, 400) - (l / 2 if n == m else 0)) for n in range(1, 5) for m in range(1, 5))622check('F1 the Dirichlet eigenfunctions sin(n pi x / l) are orthogonal with square norm l/2', og < 1e-9)623 624 625def robin_F(lam, a0, al, l):626 if lam > 1e-12:627 b = math.sqrt(lam)628 X, dX = math.cos(b * l) + a0 * math.sin(b * l) / b, -b * math.sin(b * l) + a0 * math.cos(b * l)629 elif lam < -1e-12:630 g = math.sqrt(-lam)631 X, dX = math.cosh(g * l) + a0 * math.sinh(g * l) / g, g * math.sinh(g * l) + a0 * math.cosh(g * l)632 else:633 X, dX = 1 + a0 * l, a0634 return dX + al * X635 636 637def neg_roots(a0, al, l):638 grid = [-60 + 0.01 * i for i in range(5990)] # lambda in (-60, -0.1)639 vals = [robin_F(x, a0, al, l) for x in grid]640 return [bisect(lambda x: robin_F(x, a0, al, l), grid[i], grid[i + 1]) for i in range(len(grid) - 1) if (vals[i] < 0) != (vals[i + 1] < 0)]641 642 643nr = [len(neg_roots(1, 1, 1.0)), len(neg_roots(-1, -1, 1.0)), len(neg_roots(-1, -1, 3.0))]644lam1 = neg_roots(-1, -1, 1.0)[0]645check('F2 Robin conditions X\' - a0 X = 0, X\' + al X = 0: a zero eigenvalue exactly when a0 + al = -a0 al l (the source\'s criterion);'646 ' with a0 = al = -1 there is one negative eigenvalue for l = 1 and two for l = 3; none for a0 = al = 1',647 nr == [0, 1, 2] and abs(robin_F(0.0, -1, -1, 2.0)) < 1e-15 and abs(robin_F(0.0, -1, -1, 1.0)) > 0.5, 'negative eigenvalue for l = 1: %.6f' % lam1)648om = lambda u, v: u[3] * v[2] - u[2] * v[3] - u[1] * v[0] + u[0] * v[1] # [f'g - fg'] from a to b on (f(a), f'(a), f(b), f'(b))649bcs = {'Dirichlet': ((0, 1, 0, 0), (0, 0, 0, 1)), 'Neumann': ((1, 0, 0, 0), (0, 0, 1, 0)), 'Robin': ((1, 0.7, 0, 0), (0, 0, 1, -2.5)),650 'periodic': ((1, 0, 1, 0), (0, 1, 0, 1)), 'source\'s fifth': ((1, 0, 1, 0), (0, 2, 0, 1))}651vals = {k: om(*v) for k, v in bcs.items()}652check('F3 a boundary condition is a plane in the space of boundary values, and it is symmetric exactly when the plane is Lagrangian for'653 ' the form of Green\'s second identity: true for Dirichlet, Neumann, Robin, periodic; false for X(a) = X(b), X\'(a) = 2X\'(b)',654 all(abs(vals[k]) < 1e-15 for k in ('Dirichlet', 'Neumann', 'Robin', 'periodic')) and abs(vals['source\'s fifth'] - 1) < 1e-15)655N = 200000656ps = sum(1.0 / (n * n) for n in range(1, N + 1)) + 1.0 / N - 1.0 / (2 * N * N)657check('F4 Parseval for the sine series of x gives sum 1/n^2 = pi^2/6', abs(ps - PI ** 2 / 6) < 1e-12)658Si = simpson(lambda th: math.sin(th) / th if th else 1.0, 0, PI, 2000) / PI659SN = lambda N, x: (2 / PI) * sum(math.sin(n * x) / n for n in range(1, N + 1, 2))