Direct and iterative solvers with preconditioner support
Calculate raw finite difference coefficients for any derivative and accuracy order
Generate differential operators for arbitrary stencils
Symbolic representation of finite difference schemes
Estimate truncation error by comparing accuracy orders
Solve time-dependent PDEs via Method of Lines (Forward Euler, RK4, Backward Euler, Crank-Nicolson), including wave equations (Stoermer-Verlet), time-dependent boundary values and source terms
New in version 0.11: More comfortable API (keeping the old API available)
New in version 0.12: Periodic boundary conditions for differential operators and PDEs.
New in version 0.13: Compact (implicit) finite differences with spectral-like resolution.
New in version 0.14: Time-dependent PDE solving via Method of Lines. GPU / JAX / CuPy backend support for operator application. Eigenvalue problems, iterative solvers and Robin boundary conditions for PDEs. Error estimation via accuracy order comparison.
New in version 0.15: Compact finite differences on non-uniform grids, optimal (Lele) and spectral-like compact schemes, much faster compact schemes.
New in version 0.16: Wave equations (second order in time), time-dependent boundary values and source terms for time-dependent PDEs, stable boundary treatment for high-order operators (boundary="centered"), much faster implicit time stepping.
Installation
pip install --upgrade findiff
For GPU / JAX support, install JAX separately (findiff detects it automatically):
findiff allows to easily define derivative operators that you can apply to numpy arrays of
any dimension. JAX and CuPy arrays work too — see GPU / JAX Support below.
Consider the simple 1D case of a equidistant grid
with a first derivative $\displaystyle \frac{\partial}{\partial x}$ along the only axis (0):
import numpy as np
from findiff import Diff
# define the grid:
x = np.linspace(0, 1, 100)
# the array to differentiate:
f = np.sin(x) # as an example
# Define the derivative:
d_dx = Diff(0, x[1] - x[0])
# Apply it:
df_dx = d_dx(f)
Similarly, you can define partial derivatives along other axes, for example, if $z$ is the 2-axis, we can write
$\frac{\partial}{\partial z}$ as:
Diff(2, dz)
Diff always creates a first derivative. For higher derivatives, you simply exponentiate them, for example for $\frac{\partial^2}{\partial_x^2}$
d2_dx2 = Diff(0, dx)**2
and apply it as before.
You can also define more general differential operators intuitively, like
All operators (Diff, Gradient, Divergence, Curl, Laplacian) accept JAX and CuPy arrays
directly. Just pass the array — findiff detects the backend automatically:
import jax
import jax.numpy as jnp
from findiff import Diff, Laplacian
jax.config.update("jax_enable_x64", True)
x = jnp.linspace(0, 2 * jnp.pi, 1000)
dx = x[1] - x[0]
f = jnp.sin(x)
d_dx = Diff(0, dx)
df_dx = d_dx(f) # returns a JAX array
For best performance, wrap operators with jax.jit to fuse all slice operations into a single
compiled kernel:
d_dx_jit = jax.jit(d_dx)
df_dx = d_dx_jit(f) # first call compiles, subsequent calls are fast
# Works for any operator, including higher-order and vector calculus:
lap = Laplacian(h=[dx, dy, dz])
lap_jit = jax.jit(lap)
result = lap_jit(f_3d)
Note: The matrix-based path (.matrix(), PDE solving, eigenvalue problems) remains
NumPy/SciPy only. GPU support applies to the operator application path (operator(array)).
Accuracy Control
When constructing an instance of Diff, you can request the desired accuracy
order by setting the keyword argument acc. For example:
d_dx = Diff(0, dx, acc=4)
df_dx = d_dx(f)
Alternatively, you can also split operator definition and configuration:
which comes in handy if you have a complicated expression of differential operators, because then you
can specify it on the whole expression and it will be passed down to all basic operators.
If not specified, second order accuracy will be taken by default.
Compact Finite Differences
Standard finite differences only use function values to approximate a derivative. Compact (or implicit)
finite differences also couple derivative values at neighboring points, which gives you spectral-like
resolution from small stencils. The tradeoff is that applying the operator requires solving a banded linear
system — but for tridiagonal systems that's $O(N)$ and very fast.
You can define a compact scheme explicitly by specifying the left-hand side coefficients and right-hand side offsets:
Here compact=3 sets the LHS bandwidth (must be odd). findiff determines the LHS and RHS
coefficients jointly (as in Lele, 1992) and picks the narrowest RHS stencil that reaches the
requested accuracy — e.g. compact=3, acc=6 gives Lele's classic tridiagonal scheme and
compact=5, acc=10 his pentadiagonal 10th-order scheme. Higher derivatives work the same as usual:
d2_dx2 = d_dx ** 2
Non-periodic grids are handled automatically — findiff uses one-sided compact stencils near the
boundaries. Non-uniform grids work as well: pass the coordinates instead of the spacing, e.g.
Diff(0, x, compact=3, acc=6). For multi-scale problems, spectral=True gives Lele's
spectral-like schemes, which resolve short waves much better. The matrix() method is also supported. For more details, see the
compact finite differences documentation.
Finite Difference Coefficients
Sometimes you may want to have the raw finite difference coefficients.
These can be obtained for any derivative and accuracy order
using findiff.coefficients(deriv, acc). For instance,
findiff uses standard stencils (patterns of grid points) to evaluate the derivative.
However, you can design your own stencil. A picture says more than a thousand words, so
look at the following example for a standard second order accurate stencil for the
2D Laplacian $\displaystyle \frac{\partial^2}{\partial x^2} + \frac{\partial^2}{\partial y^2}$:
As of version 0.10, findiff can also provide a symbolic representation of finite difference schemes suitable for using in conjunction with sympy. The main use case is to facilitate deriving your own iteration schemes.
from findiff import SymbolicMesh, SymbolicDiff
mesh = SymbolicMesh("x, y")
u = mesh.create_symbol("u")
d2_dx2, d2_dy2 = [SymbolicDiff(mesh, axis=k, degree=2) for k in range(2)]
(
d2_dx2(u, at=(m, n), offsets=(-1, 0, 1)) +
d2_dy2(u, at=(m, n), offsets=(-1, 0, 1))
)