Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 941–1040 of 1640. Source text is displayed literally.
941 EEp = padd(Eul(Eul(p)), Eul(p))942 okc = okc and pmul(r2, plap(p)) == padd(EEp, L2p, -1) # r^2 Laplace = E(E+1) - L^2943 okc = okc and padd(Lop[0](Lop[1](p)), Lop[1](Lop[0](p)), -1) == pscale(Lop[2](p), -1) # [L1, L2] = -L3944 okc = okc and all(plap(Lo(p)) == Lo(plap(p)) for Lo in Lop)945check('Y1 rotations: harmonic homogeneous polynomials of degree l in three variables form a space of dimension 2l+1; the rotation fields'946 ' satisfy [L1, L2] = -L3 and commute with the Laplacian; and r^2 Laplace = E(E+1) - L^2, so L^2 = l(l+1) on that space (exact)', oky and okc)947 948 949def pol_mul(a, b):950 r = [Fr(0)] * (len(a) + len(b) - 1)951 for i, x in enumerate(a):952 for j, y in enumerate(b):953 r[i + j] += x * y954 return r955 956 957pol_int = lambda a: sum(c * (Fr(1) - Fr(-1) ** (k + 1)) / (k + 1) for k, c in enumerate(a))958pol_d = lambda a: [k * c for k, c in enumerate(a)][1:] or [Fr(0)]959okl = True960for 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):