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

601 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))660peaks = [SN(N, PI / (N + 1)) for N in (51, 201, 801)]661check('F5 Gibbs: partial sums of a unit jump overshoot to (1/pi) Si(pi) = 0.58949 on the side where the value is 1/2: 8.95 per cent of the jump',662      abs(Si - 0.5894899) < 1e-6 and abs(peaks[2] - Si) < 1e-5, 'peaks at N = 51, 201, 801: %.6f, %.6f, %.6f' % tuple(peaks))663co = lambda f, n, kind: simpson(lambda x: f(x) * (math.cos(n * x) if kind == 'c' else math.sin(n * x)), -PI, PI, 4000) / PI664st = [n * abs(co(lambda x: (1 if x > 0 else -1), n, 's')) for n in (5, 25, 125)]665ab = [n * n * abs(co(abs, n, 'c')) for n in (5, 25, 125)]666an = [co(lambda x: 1 / (5 - 4 * math.cos(x)), n, 'c') / co(lambda x: 1 / (5 - 4 * math.cos(x)), n + 1, 'c') for n in (2, 5, 9)]667check('F6 smoothness is decay: coefficients of a jump fall like 1/n, of a corner like 1/n^2, of an analytic function geometrically',668      all(abs(v - 4 / PI) < 1e-3 for v in st) and all(abs(v - 4 / PI) < 1e-3 for v in ab) and all(abs(v - 2) < 1e-6 for v in an))669n = 9670okc = True671for k in range(n):672    lamP = 4 * math.sin(PI * k / n) ** 2673    v = [math.cos(2 * PI * j * k / n) for j in range(n)]674    okc = okc and max(abs(a - lamP * b) for a, b in zip(mv(lap1(n, 'P'), v), v)) < 1e-12675    lamN = 4 * math.sin(PI * k / (2 * n)) ** 2676    v = [math.cos(PI * k * (j + 0.5) / n) for j in range(n)]677    okc = okc and max(abs(a - lamN * b) for a, b in zip(mv(lap1(n, 'N'), v), v)) < 1e-12678    lamD = 4 * math.sin(PI * (k + 1) / (2 * (n + 1))) ** 2679    v = [math.sin(PI * (k + 1) * (j + 1) / (n + 1)) for j in range(n)]680    okc = okc and max(abs(a - lamD * b) for a, b in zip(mv(lap1(n), v), v)) < 1e-12681check('F7 finite Fourier analysis: characters of Z/n diagonalize the periodic Laplacian; their even and odd parts under reflection are the'682      ' Neumann (cosine) and Dirichlet (sine) eigenvectors', okc)683useries = lambda x, t, N: x + sum(2 * (-1) ** n / (n * PI) * math.exp(-n * n * PI * PI * t) * math.sin(n * PI * x) for n in range(1, N + 1))684check('F8 inhomogeneous boundary values are removed by subtracting the steady state: u_t = u_xx, u(0) = 0, u(1) = 1 from zero data',685      abs(useries(0.3, 0.0, 200000)) < 1e-5 and abs(useries(0.3, 0.5, 50) - 0.3) < 1e-2 and686      abs(d1(lambda t: useries(0.3, t, 60), 0.05) - d2(lambda x: useries(x, 0.05, 60), 0.3)) < 1e-6)687 688sec('6. Harmonic functions')689uh = lambda x, y: x ** 3 - 3 * x * y * y + 2 * x - y690avg = lambda f, x0, y0, r: simpson(lambda th: f(x0 + r * math.cos(th), y0 + r * math.sin(th)), 0, 2 * PI, 400) / (2 * PI)691check('H1 mean value property: holds for a harmonic polynomial; for x^2 the circle average exceeds the central value by r^2/2',692      abs(avg(uh, 0.3, -0.2, 0.8) - uh(0.3, -0.2)) < 1e-12 and abs(avg(lambda x, y: x * x, 0.3, -0.2, 0.8) - 0.09 - 0.32) < 1e-12)693 694 695def grid_dirichlet(m, bval):696    """solve the five-point Laplace equation on the m x m interior of a square; bval(i, j) gives boundary values"""697    idx = {(i, j): i * m + j for i in range(m) for j in range(m)}698    A = [[0.0] * (m * m) for _ in range(m * m)]699    b = [0.0] * (m * m)700    for (i, j), r in idx.items():