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

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 1060def count(lam):1061    c_, m_ = 0, 11062    while PI ** 2 * m_ * m_ / ar ** 2 < lam:1063        rest = lam - PI ** 2 * m_ * m_ / ar ** 21064        c_ += int(math.floor(math.sqrt(rest) * br / PI - 1e-12))1065        m_ += 11066    return c_1067 1068 1069rows = []1070okW = True1071for lam in (2000.0, 8000.0, 32000.0):1072    Nn = count(lam)1073    two = Area * lam / (4 * PI) - Per * math.sqrt(lam) / (4 * PI)1074    okW = okW and abs(Nn - two) / Nn < 0.021075    rows.append('lambda=%g: N=%d, two-term Weyl %.1f, lambda/N=%.3f' % (lam, Nn, two, lam / Nn))1076check('L4 Weyl\'s law for a 1 x sqrt 2 rectangle: N(lambda) against A lambda/(4 pi) - P sqrt(lambda)/(4 pi), and lambda_n/n tending to 4 pi/A = 8.886',1077      okW and abs(32000.0 / count(32000.0) - 4 * PI / Area) < 0.25, '; '.join(rows))1078ztrace = lambda t: sum(math.exp(-PI ** 2 * m_ * m_ * t / ar ** 2) for m_ in range(1, 400)) * sum(math.exp(-PI ** 2 * n_ * n_ * t / br ** 2) for n_ in range(1, 400))1079hk = [ztrace(t) - (Area / (4 * PI * t) - Per / (8 * math.sqrt(PI * t)) + 0.25) for t in (0.02, 0.01, 0.005)]