Indexed text attachment
verify-pde-learning.py
Source for Partial Differential Equations in Machine Learning and AI: Naturality, Well-Posedness, and Spectral Rates
View paper · Download original file
Lines 464–563 of 1244. Source text is displayed literally.
464 465def odd_ext(x):466 return [0.0] + list(x) + [0.0] + [-a for a in reversed(x)]467 468 469def zero_vs_odd(w):470 a = conv_pad(x, w, 'zero')471 b = conv_pad(odd_ext(x), w, 'circ')[1:n + 1]472 return norm(axpy(-1, a, b)) / norm(a)473 474 475z1, z2 = zero_vs_odd([0.3, 1.0, 0.3]), zero_vs_odd(ws)476check('N4 a three-point symmetric stencil agrees with the Dirichlet image construction, while the tested five-point stencil differs',477 z1 < 1e-12 and z2 > 0.01, 'relative difference: radius 1 %.0e, radius 2 %.3f' % (z1, z2))478 479 480def shift(x):481 return [0.0] + list(x[:-1])482 483 484def shift_c(x):485 return [x[-1]] + list(x[:-1])486 487 488n = 20489x = [random.gauss(0, 1) for _ in range(n)]490r = 2491com = axpy(-1, conv_pad(shift(x), wa, 'zero'), shift(conv_pad(x, wa, 'zero')))492supp = [i for i in range(n) if abs(com[i]) > 1e-12]493comc = norm(axpy(-1, conv_pad(shift_c(x), wa, 'circ'), shift_c(conv_pad(x, wa, 'circ'))))494check('N5 circular convolution commutes with translation; zero padding fails only within the kernel radius of the boundary',495 comc < 1e-12 and len(supp) > 0 and all(i == 0 or i >= n - r for i in supp), 'commutator supported on positions %s of 0..%d' % (supp, n - 1))496 497n = 32498one = [1.0] * n499MD = [[(1.0 if i == j else 0.0) - 0.25 * lap1(n, 'D')[i][j] for j in range(n)] for i in range(n)]500MN = [[(1.0 if i == j else 0.0) - 0.25 * lap1(n, 'N')[i][j] for j in range(n)] for i in range(n)]501yD, yN = one, [random.random() for _ in range(n)]502m0 = sum(yN)503mx0 = max(yN)504okmax = True505for _ in range(200):506 yD, yN2 = mv(MD, yD), mv(MN, yN)507 okmax = okmax and max(yN2) <= max(yN) + 1e-15 and min(yN2) >= min(yN) - 1e-15508 yN = yN2509surv = sum(yD) / n510pred = 0.0511for k in range(1, n + 1):512 ip = sum(math.sin(PI * k * j / (n + 1)) for j in range(1, n + 1))513 pred += (1 - math.sin(PI * k / (2 * (n + 1))) ** 2) ** 200 * ip * ip * 2 / (n + 1) / n514check('N6 a smoothing layer with reflect padding conserves mass and obeys the maximum principle; zero padding leaks mass',515 abs(sum(yN) - m0) < 1e-10 and okmax and close(surv, pred, 1e-10) and 0.3 < surv < 0.9,516 'after 200 layers of (1/4,1/2,1/4) on 32 points: reflect mass change %.0e; zero-padded mass left %.4f (spectral formula %.4f)' % (abs(sum(yN) - m0), surv, pred))517 518sec('2. Discretization: what commutes with resampling')519 520 521def dft(x):522 N = len(x)523 return [sum(x[j] * cmath.exp(-2j * PI * k * j / N) for j in range(N)) / N for k in range(N)]524 525 526def coef(x, k):527 N = len(x)528 return sum(x[j] * cmath.exp(-2j * PI * k * j / N) for j in range(N)) / N529 530 531K = 5532a = [random.gauss(0, 1) for _ in range(K + 1)]533b = [random.gauss(0, 1) for _ in range(K + 1)]534 535 536def u(x):537 return sum(a[k] * math.cos(k * x) + b[k] * math.sin(k * x) for k in range(1, K + 1))538 539 540def mult_exact(x):541 return sum((a[k] * math.cos(k * x) + b[k] * math.sin(k * x)) / (1 + k * k) for k in range(1, K + 1))542 543 544def lap_exact(x):545 return sum((a[k] * math.cos(k * x) + b[k] * math.sin(k * x)) * k * k for k in range(1, K + 1))546 547 548def apply_mult(N):549 xs = [2 * PI * j / N for j in range(N)]550 c = dft([u(t) for t in xs])551 out = []552 for j in range(N):553 s = 0554 for k in range(N):555 f = k if k <= N // 2 else k - N556 s += c[k] / (1 + f * f) * cmath.exp(2j * PI * k * j / N)557 out.append(s.real)558 return max(abs(out[j] - mult_exact(xs[j])) for j in range(N))559 560 561def apply_stencil(N):562 h = 2 * PI / N563 xs = [j * h for j in range(N)]