findiff

raw JSON →
0.13.1 verified Mon Apr 27 auth: no python

A Python package for finite difference derivatives in any number of dimensions. Current version 0.13.1, released on PyPI. Provides tools to compute partial derivatives of discrete functions on uniform and non-uniform grids with automatic stencil generation. Supports periodic boundary conditions and symbolic capabilities. Active development with periodic releases.

pip install findiff
error ModuleNotFoundError: No module named 'findiff.fin_diff'
cause The module path changed in v0.11.0. Old code using `from findiff.fin_diff import FinDiff` will fail.
fix
Change import to from findiff import FinDiff.
error ValueError: The dimension parameter must be an integer.
cause When initializing `FinDiff` with a tuple for grid coords (like `FinDiff(0, x, 1)`), the grid coords were passed incorrectly if x is a 1D array but you intended to use a multi-dimensional grid.
fix
Ensure the second argument is the list of grid coordinates for each dimension. For 1D, FinDiff(0, x, 1) is correct. For higher dimensions, pass a list of arrays, e.g., FinDiff(0, [x, y], 1).
error AttributeError: 'Coefficient' object has no attribute 'eval'
cause In older versions (<0.10.0), the Coefficient class did not have an eval method. Symbolic capabilities were introduced in v0.10.0.
fix
Upgrade findiff to 0.10.0 or later: pip install --upgrade findiff.
error TypeError: __init__() got an unexpected keyword argument 'acc'
cause In v0.11.0, the `acc` parameter was renamed to `accuracy` in some contexts, or the Diff class expects different arguments.
fix
Use accuracy instead of acc when initializing FinDiff or Diff. For example: FinDiff(0, x, 1, accuracy=2).
breaking In v0.11.0, the API was significantly restructured. The old `FinDiff` initialization with positional arguments may break if you relied on old defaults. The `Diff` class was introduced; stencils are now lazy.
fix Update import to `from findiff import FinDiff, Diff`. If using old positional arguments, ensure accuracy is passed correctly. Review migration guide in changelog.
gotcha The `to_sparse()` method returns a sparse matrix only if SciPy is installed. Without it, the method raises an ImportError.
fix Install scipy: `pip install scipy` before calling to_sparse().
gotcha When using non-uniform grids, the accuracy parameter may not behave as expected for high-order derivatives. The stencil generation assumes uniform spacing by default.
fix For non-uniform grids, consider using the `Diff` class with explicit grid coordinates. Check the documentation for `FinDiff` on non-uniform grids.
deprecated The old import path `from findiff.fin_diff import FinDiff` is deprecated since v0.11.0 and may be removed in future versions.
fix Use `from findiff import FinDiff`.

Compute the first derivative of sin(x) on a uniform grid with second-order accuracy.

import numpy as np
from findiff import FinDiff

# Define a grid and a function
x = np.linspace(0, 1, 5)
f = np.sin(x)

# First derivative (second order accuracy)
d_dx = FinDiff(0, x, 1, acc=2)
df_dx = d_dx(f)
print(df_dx)