Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 660–759 of 1640. Source text is displayed literally.
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():701 A[r][r] = 4.0702 for di, dj in ((1, 0), (-1, 0), (0, 1), (0, -1)):703 p = (i + di, j + dj)704 if p in idx:705 A[r][idx[p]] = -1.0706 else:707 b[r] += bval(*p)708 return solve(A, b), A, b, idx709 710 711bv = {}712rb = lambda i, j: bv.setdefault((i, j), random.uniform(-1, 1))713sol_, A_, b_, idx_ = grid_dirichlet(6, rb)714check('H2 maximum principle on a grid: a discrete harmonic function with random boundary values stays strictly between their extremes',715 min(bv.values()) < min(sol_) and max(sol_) < max(bv.values()))716a_ = 2.0717hb = lambda ph: (a_ * math.cos(ph)) ** 2 - (a_ * math.sin(ph)) ** 2 + 3 * a_ * math.cos(ph)718xr, yr = 0.7, -0.4719rr, th = math.hypot(xr, yr), math.atan2(yr, xr)720pf = (a_ * a_ - rr * rr) / (2 * PI) * simpson(lambda ph: hb(ph) / (a_ * a_ - 2 * a_ * rr * math.cos(th - ph) + rr * rr), 0, 2 * PI, 400)721check('H3 Poisson\'s formula recovers x^2 - y^2 + 3x inside the disk of radius 2 from its boundary values', abs(pf - (xr * xr - yr * yr + 3 * xr)) < 1e-12)722 723 724def ppow(p, e, n):725 r = {tuple([0] * n): Fr(1)}726 for _ in range(e):727 r = pmul(r, p)728 return r729 730 731def pcomp(p, subs):732 n = len(next(iter(subs[0])))733 r = {}734 for k, v in p.items():735 term = {tuple([0] * n): Fr(v)}736 for i, e in enumerate(k):737 term = pmul(term, ppow(subs[i], e, n))738 r = padd(r, term)739 return r740 741 742X_, Y_ = pvar(0, 2), pvar(1, 2)743rot = [padd(pscale(X_, Fr(3, 5)), pscale(Y_, Fr(-4, 5))), padd(pscale(X_, Fr(4, 5)), pscale(Y_, Fr(3, 5)))]744shear = [padd(X_, Y_), Y_]745sq = [padd(pmul(X_, X_), pmul(Y_, Y_), -1), pscale(pmul(X_, Y_), 2)] # z -> z^2746p_ = {(4, 1): Fr(2), (1, 2): Fr(-3), (0, 3): Fr(1), (2, 0): Fr(5)}747u3 = {(3, 0): Fr(1), (1, 2): Fr(-3)}748jac = pscale(padd(pmul(X_, X_), pmul(Y_, Y_)), 4) # |f'|^2 = 4|z|^2749check('H4 the Laplacian is natural for isometries and, in the plane, for conformal maps up to the factor |f\'|^2 (exact polynomial identities);'750 ' it is not natural for a shear', plap(pcomp(p_, rot)) == pcomp(plap(p_), rot) and plap(pcomp(p_, shear)) != pcomp(plap(p_), shear)751 and plap(pcomp(u3, sq)) == {} and plap(pcomp(p_, sq)) == pmul(jac, pcomp(plap(p_), sq)))752 753 754def period(f, r=1.5):755 g = lambda th: (-d1(lambda y: f(r * math.cos(th), y), r * math.sin(th)) * (-r * math.sin(th)) + d1(lambda x: f(x, r * math.sin(th)), r * math.cos(th)) * (r * math.cos(th)))756 return simpson(g, 0, 2 * PI, 400)757 758 759pers = [period(lambda x, y: 0.5 * math.log(x * x + y * y)), period(lambda x, y: x * x - y * y), period(lambda x, y: x / (x * x + y * y))]