Independent scholarship · Open accessThe monthly digest

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 401–500 of 1244. Source text is displayed literally.

401 402 403def conv_pad(x, w, mode):404    n = len(x)405    r = len(w) // 2406 407    def get(i):408        if 0 <= i < n:409            return x[i]410        if mode == 'zero':411            return 0.0412        if mode == 'circ':413            return x[i % n]414        i = i % (2 * n)415        return x[i] if i < n else x[2 * n - 1 - i]416    return [sum(w[j + r] * get(i + j) for j in range(-r, r + 1)) for i in range(n)]417 418 419n = 12420x = [random.gauss(0, 1) for _ in range(n)]421st = [-1.0, 2.0, -1.0]422e = max(norm(axpy(-1, conv_pad(x, st, m), mv(lap1(n, b), x))) for m, b in (('reflect', 'N'), ('zero', 'D'), ('circ', 'P')))423check('N1 the stencil (-1,2,-1) with reflect, zero and circular padding is the Neumann, Dirichlet and periodic Laplacian',424      e < 1e-12, 'max difference %.1e' % e)425 426res = 0.0427for k in range(n):428    v = [math.cos(PI * k * (j + 0.5) / n) for j in range(n)]429    res = max(res, norm(axpy(-4 * math.sin(PI * k / (2 * n)) ** 2, v, mv(lap1(n, 'N'), v))))430    v = [math.sin(PI * (k + 1) * (j + 1) / (n + 1)) for j in range(n)]431    res = max(res, norm(axpy(-4 * math.sin(PI * (k + 1) / (2 * (n + 1))) ** 2, v, mv(lap1(n, 'D'), v))))432    v = [math.cos(2 * PI * k * j / n) for j in range(n)]433    res = max(res, norm(axpy(-4 * math.sin(PI * k / n) ** 2, v, mv(lap1(n, 'P'), v))))434check('N2 the three paddings are diagonalized by the DCT-II, DST-I and DFT bases with eigenvalues 4 sin^2',435      res < 1e-12, 'max residual %.1e over 36 vectors' % res)436 437 438def even_ext(x):439    return list(x) + list(reversed(x))440 441 442def evenness(z):443    return norm([a - b for a, b in zip(z, reversed(z))]) / norm(z)444 445 446ws = [0.2, -0.5, 1.0, -0.5, 0.2]447wa = [0.2, -0.5, 1.0, 0.7, -0.3]448d_restr = max(norm(axpy(-1, conv_pad(even_ext(x), w, 'circ')[:n], conv_pad(x, w, 'reflect'))) for w in (ws, wa))449ev_s = evenness(conv_pad(even_ext(x), ws, 'circ'))450ev_a = evenness(conv_pad(even_ext(x), wa, 'circ'))451 452 453def two_layer_defect(w1, w2):454    a = conv_pad(conv_pad(x, w1, 'reflect'), w2, 'reflect')455    b = conv_pad(conv_pad(even_ext(x), w1, 'circ'), w2, 'circ')[:n]456    return norm(axpy(-1, a, b)) / norm(b)457 458 459c_s, c_a = two_layer_defect(ws, wa), two_layer_defect(wa, ws)460check('N3 half-sample reflect padding agrees with circular convolution on the doubled signal followed by restriction to the original positions',461      d_restr < 1e-12 and ev_s < 1e-12 and ev_a > 0.1 and c_s < 1e-12 and c_a > 0.01,462      'restriction %.0e; output odd part: symmetric kernel %.0e, asymmetric %.3f; two-layer defect %.0e vs %.3f' % (d_restr, ev_s, ev_a, c_s, c_a))463 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)]