Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 341–440 of 1640. Source text is displayed literally.
341 if l == 0:342 return p0343 for k in range(1, l):344 nxt = [Fr(0)] * (k + 2)345 for i, c in enumerate(p1):346 nxt[i + 1] += Fr(2 * k + 1, k + 1) * c347 for i, c in enumerate(p0):348 nxt[i] -= Fr(k, k + 1) * c349 p0, p1 = p1, nxt350 return p1351 352 353def heat(x, t, k=1.0):354 return math.exp(-x * x / (4 * k * t)) / math.sqrt(4 * PI * k * t)355 356 357def charpoly_int(A):358 n = len(A)359 M = [[int(i == j) for j in range(n)] for i in range(n)]360 cs = [1]361 for k in range(1, n + 1):362 AM = [[sum(A[i][l] * M[l][j] for l in range(n)) for j in range(n)] for i in range(n)]363 c = -sum(AM[i][i] for i in range(n)) // k364 cs.append(c)365 M = [[AM[i][j] + (c if i == j else 0) for j in range(n)] for i in range(n)]366 return tuple(cs)367 368 369# ######################## CHECKS FOR THE BOOK ########################370sec('1. Equations: symbol, type, characteristics, formal solutions')371 372 373def kind(sig):374 p, q, z = sig375 n = p + q + z376 if z == 0 and (p == 0 or q == 0):377 return 'elliptic'378 if z == 0 and min(p, q) == 1:379 return 'hyperbolic'380 if z == 1 and (p == 0 or q == 0):381 return 'parabolic'382 return 'other'383 384 385ok = True386for _ in range(300):387 n = random.choice([2, 3, 4])388 A = [[0] * n for _ in range(n)]389 for i in range(n):390 for j in range(i, n):391 A[i][j] = A[j][i] = random.randint(-3, 3)392 while True:393 B = [[random.randint(-3, 3) for _ in range(n)] for _ in range(n)]394 if rank_exact(B) == n:395 break396 BAB = [[sum(B[k][i] * A[k][l] * B[l][j] for k in range(n) for l in range(n)) for j in range(n)] for i in range(n)]397 ok = ok and inertia(BAB) == inertia(A)398arch = (kind(inertia([[1, 0], [0, 1]])), kind(inertia([[1, 0], [0, -1]])), kind(inertia([[1, 0], [0, 0]])))399ok2 = True400for _ in range(300):401 a11, a12, a22 = (random.randint(-4, 4) for _ in range(3))402 if a11 == a12 == a22 == 0:403 continue404 d = a12 * a12 - a11 * a22405 ok2 = ok2 and kind(inertia([[a11, a12], [a12, a22]])) == ('elliptic' if d < 0 else 'hyperbolic' if d > 0 else 'parabolic')406check('E1 the type of a second-order operator is the inertia of its symbol: invariant under 300 random linear changes of variables,'407 ' and equal to the source\'s discriminant test in two variables', ok and ok2 and arch == ('elliptic', 'hyperbolic', 'parabolic'), 'Laplace, wave, heat: %s, %s, %s' % arch)408pts = [(Fr(2), Fr(3)), (Fr(-2), Fr(-3)), (Fr(1, 2), Fr(1)), (Fr(-1), Fr(4)), (Fr(2), Fr(1, 2)), (Fr(0), Fr(0))]409kinds = [kind(inertia([[y, -1], [-1, x]])) for x, y in pts]410check('E2 the source\'s example y u_xx - 2 u_xy + x u_yy: elliptic where xy > 1, hyperbolic where xy < 1, parabolic on xy = 1',411 kinds == ['elliptic', 'elliptic', 'hyperbolic', 'hyperbolic', 'parabolic', 'hyperbolic'], ', '.join(kinds))412f = lambda s: math.sin(s) + s * s413r1 = max(abs(4 * d1(lambda x: f(-3 * x - 4 * y), x) - 3 * d1(lambda yy: f(-3 * x - 4 * yy), y)) for x, y in [(0.3, -0.7), (1.1, 0.4), (-2.0, 0.9)])414r2 = max(abs(d1(lambda xx: f(math.exp(-xx) * y), x) + y * d1(lambda yy: f(math.exp(-x) * yy), y)) for x, y in [(0.3, -0.7), (1.1, 0.4), (-2.0, 0.9)])415check('E3 first-order equations are solved by pulling back along the quotient by the characteristics: f(-3x-4y) solves 4u_x - 3u_y = 0,'416 ' f(e^{-x} y) solves u_x + y u_y = 0, and the line y = 0 is a characteristic, on which every solution is constant',417 r1 < 1e-8 and r2 < 1e-8 and all(math.exp(-x) * 0.0 == 0.0 for x in (-1.0, 0.5, 3.0)), 'residuals %.1e, %.1e' % (r1, r2))418ok, rows = True, []419for n in (2, 3, 4):420 for k in range(0, 6):421 want = math.comb(n + k - 1, k) - (math.comb(n + k - 3, k - 2) if k >= 2 else 0)422 got = (kernel_dim(plap, n, k), kernel_dim(lambda p: plap(p, [1] * (n - 1) + [-1]), n, k), kernel_dim(lambda p: pdiff(pdiff(p, 0), 0), n, k))423 ok = ok and got == (want, want, want)424 rows.append('n=%d: %s' % (n, [math.comb(n + k - 1, k) - (math.comb(n + k - 3, k - 2) if k >= 2 else 0) for k in range(6)]))425check('E4 formal solutions do not see the type: homogeneous polynomial solutions of degree k of the Laplace, wave and degenerate'426 ' operators have the same dimension C(n+k-1,k) - C(n+k-3,k-2) (exact ranks)', ok, '; '.join(rows))427c = Fr(3, 2)428ok = True429for _ in range(20):430 p = {(random.randint(0, 4), random.randint(0, 4)): Fr(random.randint(-5, 5)) for _ in range(6)}431 p = {k: v for k, v in p.items() if v != 0}432 if not p:433 continue434 A1 = padd(pdiff(p, 0), pdiff(p, 1), c) # (d_t + c d_x) p, variables (t, x)435 lhs = padd(pdiff(A1, 0), pdiff(A1, 1), -c) # (d_t - c d_x)(d_t + c d_x) p436 rhs = padd(pdiff(pdiff(p, 0), 0), pdiff(pdiff(p, 1), 1), -c * c)437 ok = ok and lhs == rhs438nreal = lambda a11, a12, a22: (0 if a12 * a12 < a11 * a22 else 1 if a12 * a12 == a11 * a22 else 2)439check('E5 the type is visible in the real characteristics: none for Laplace, one double for heat, two for wave; and the wave operator'440 ' factors exactly into two transport operators', ok and (nreal(1, 0, 1), nreal(1, 0, 0), nreal(1, 0, -1)) == (0, 1, 2))