Indexed text attachment
verify-pde.py
Source for Partial Differential Equations in Higher Categories
View paper · Download original file
Lines 313–412 of 1640. Source text is displayed literally.
313 r = {}314 for i in range(n):315 r = padd(r, pdiff(pdiff(p, i), i), sig[i])316 return r317 318 319def monomials(n, k):320 return [e for e in itertools.product(range(k + 1), repeat=n) if sum(e) == k]321 322 323def kernel_dim(op, n, k):324 """dimension of the kernel of a linear operator on homogeneous polynomials of degree k in n variables"""325 src = monomials(n, k)326 imgs = [op({m: Fr(1)}) for m in src]327 tgt = sorted({key for im in imgs for key in im})328 if not tgt:329 return len(src)330 return len(src) - rank_exact([[im.get(t, 0) for t in tgt] for im in imgs])331 332 333# ---------- special functions ----------334def besselj(n, x, m=400):335 return sum(math.cos(n * (PI * (j + 0.5) / m) - x * math.sin(PI * (j + 0.5) / m)) for j in range(m)) / m336 337 338def legendre(l):339 """coefficients (Fractions, ascending powers) of the Legendre polynomial P_l"""340 p0, p1 = [Fr(1)], [Fr(0), Fr(1)]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 * s