Stencils and PDE steps
A finite-difference stencil is arithmetic on shifted copies of an array, and an implicit time step is a tridiagonal solve. These are the pieces the PDE solvers are built from. Run a cell to see its output beside the expected one.
A periodic second difference
roll(u, k) moves element i to i + k, wrapping around, so roll(u, 1)[i] is u[i - 1]. The second difference of i squared is exactly 2; the two ends show the wrap.
roll along one axis of a grid
A third argument picks the axis: 0 shifts rows, 1 shifts along each row. A five-point Laplacian is four of these.
A tridiagonal solve
tridiag_solve(sub, diag, super, rhs) is the Thomas algorithm, O(n). Here the matrix is the 1-D Laplacian (-1, 2, -1), and the right-hand side is chosen so the answer is all ones. sub[0] and super[n-1] are never read.
One implicit heat step
Backward Euler for u_t = u_xx with dt / h^2 = 1: solve (I - r D2) u_new = u_old. The spike spreads symmetrically, and no time step is too large for it.
The max-norm error
max and min of ONE array reduce it to its largest or smallest entry. With two arguments they stay elementwise.
An array to a power
pow(a, s) raises every entry to the same scalar power.
Negating an array
Unary minus works on arrays, real or complex.