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

960for 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 981def nodal_domains(vec, m, n):982    sgn = [0 if abs(v) < 1e-9 else (1 if v > 0 else -1) for v in vec]983    seen, cnt = set(), 0984    for s in range(m * n):985        if sgn[s] == 0 or s in seen:986            continue987        cnt += 1988        st = [s]989        seen.add(s)990        while st:991            q = st.pop()992            i, j = divmod(q, n)993            for a, b in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):994                if 0 <= a < m and 0 <= b < n and a * n + b not in seen and sgn[a * n + b] == sgn[s]:995                    seen.add(a * n + b)996                    st.append(a * n + b)997    return cnt998 999 1000_, vp = jacobi_eig(lap1(12))1001changes = [sum(1 for i in range(11) if v[i] * v[i + 1] < 0) for v in vp]1002ev56, vec56 = jacobi_eig(kron_sum(lap1(5), lap1(6)))1003nd = [nodal_domains(v, 5, 6) for v in vec56[:8]]1004sq_ev = sorted(a * a + b * b for a in range(1, 5) for b in range(1, 5))[:6]1005check('Y5 nodes: the k-th eigenvector of an interval has k-1 sign changes; on a 5 x 6 grid, whose eigenvalues are simple, the k-th has at most'1006      ' k nodal domains; and the square has eigenvalues 2, 5, 5, 8, 10, 10 as the source lists', changes == list(range(12)) and all(d <= k + 1 for k, d in enumerate(nd))1007      and nd[0] == 1 and nd[1] == 2 and sq_ev == [2, 5, 5, 8, 10, 10], 'nodal domains of the first eight: %s' % nd)1008e55 = jacobi_eig(kron_sum(lap1(5), lap1(5)))[0]1009L55 = kron_sum(lap1(5), lap1(5))1010Rq = [[1.0 if (j // 5, j % 5) == (i % 5, 4 - i // 5) else 0.0 for j in range(25)] for i in range(25)]      # quarter turn of the grid1011comm = max(abs(a - b) for ra, rb in zip(mm(Rq, L55), mm(L55, Rq)) for a, b in zip(ra, rb))1012check('Y6 symmetry forces degeneracy: the Laplacian of a square grid commutes with the quarter turn and its second eigenvalue is double;'1013      ' on the 5 x 6 grid the second eigenvalue is simple', comm < 1e-12 and abs(e55[1] - e55[2]) < 1e-10 and abs(ev56[1] - ev56[2]) > 1e-3)1014 1015sec('10. Eigenvalues')1016w1, w2 = [Fr(0), Fr(1), Fr(-1)], pol_mul([Fr(0), Fr(1), Fr(-1)], [Fr(0), Fr(1), Fr(-1)])1017i01 = lambda a: sum(c / (k + 1) for k, c in enumerate(a))1018Am = [[i01(pol_mul(pol_d(p), pol_d(q))) for q in (w1, w2)] for p in (w1, w2)]1019Bm = [[i01(pol_mul(p, q)) for q in (w1, w2)] for p in (w1, w2)]1020qa = float(Bm[0][0] * Bm[1][1] - Bm[0][1] ** 2)1021qb = float(-(Am[0][0] * Bm[1][1] + Am[1][1] * Bm[0][0] - 2 * Am[0][1] * Bm[0][1]))1022qc = float(Am[0][0] * Am[1][1] - Am[0][1] ** 2)1023ritz = sorted([(-qb - math.sqrt(qb * qb - 4 * qa * qc)) / (2 * qa), (-qb + math.sqrt(qb * qb - 4 * qa * qc)) / (2 * qa)])1024check('L1 Rayleigh-Ritz gives upper bounds: on (0,1) the trial function x(1-x) gives 10, and adding x^2(1-x)^2 gives 9.86975 against pi^2 = 9.86960;'1025      ' the second Ritz value bounds the third eigenvalue 9 pi^2', Am[0][0] / Bm[0][0] == 10 and 0 < ritz[0] - PI ** 2 < 2e-4 and ritz[1] > 9 * PI ** 2,1026      'Ritz values %.6f, %.4f' % tuple(ritz))1027okI = True1028for _ in range(10):1029    Ar = [[0.0] * 8 for _ in range(8)]1030    for i in range(8):1031        for j in range(i, 8):1032            Ar[i][j] = Ar[j][i] = random.uniform(-1, 1)1033    ea_, eb_ = jacobi_eig(Ar)[0], jacobi_eig([r[:7] for r in Ar[:7]])[0]1034    okI = okI and all(ea_[k] <= eb_[k] + 1e-12 and eb_[k] <= ea_[k + 1] + 1e-12 for k in range(7))1035check('L2 the minimax principle in its finite form: eigenvalues of a compression interlace those of the matrix (ten random cases)', okI)1036cells = [(i, j) for i in range(6) for j in range(6)]1037Lsh = [(i, j) for (i, j) in cells if not (i >= 3 and j >= 3)]1038 1039 1040def region_lap(cs):1041    ix = {c_: k for k, c_ in enumerate(cs)}1042    Lr = [[0.0] * len(cs) for _ in cs]1043    for (i, j), k in ix.items():1044        Lr[k][k] = 4.01045        for p in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):1046            if p in ix:1047                Lr[k][ix[p]] = -1.01048    return Lr1049 1050 1051eS, eL = jacobi_eig(region_lap(cells))[0], jacobi_eig(region_lap(Lsh))[0]1052corner = 0.5 + (0.65 + 0.05) / math.sqrt(2)1053check('L3 enlarging a domain lowers every Dirichlet eigenvalue (an L-shaped region inside a 6 x 6 square); for Neumann conditions this fails:'1054      ' a 1.3 x 0.1 rectangle fits inside the unit square along its diagonal and has the smaller second eigenvalue',1055      all(eL[k] >= eS[k] - 1e-12 for k in range(len(eL))) and corner < 1 and PI ** 2 / 1.69 < PI ** 2, 'first eigenvalues: square %.4f, L-shape %.4f; Neumann: %.3f < %.3f' % (eS[0], eL[0], PI ** 2 / 1.69, PI ** 2))1056ar, br = 1.0, math.sqrt(2.0)1057Area, Per = ar * br, 2 * (ar + br)1058 1059