From bd0f9ed779040e1ff8a473c2a9b99d2c8307178f Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Tue, 19 Sep 2023 10:43:19 +0300 Subject: [PATCH] refactor(diff): rename Stencil to DiffStencil --- pycaputo/differentiation/finite_difference.py | 16 ++++++++-------- tests/test_diff.py | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pycaputo/differentiation/finite_difference.py b/pycaputo/differentiation/finite_difference.py index a6ab142..0eb2ea8 100644 --- a/pycaputo/differentiation/finite_difference.py +++ b/pycaputo/differentiation/finite_difference.py @@ -23,7 +23,7 @@ class Truncation(NamedTuple): @dataclass(frozen=True) -class Stencil: +class DiffStencil: r"""Approximation of a derivative by finite difference on a uniform grid. .. math:: @@ -60,7 +60,7 @@ def trunc(self) -> Truncation: return determine_stencil_truncation_error(self) -def apply_derivative(s: Stencil, f: Array, h: float = 1.0) -> Array: +def apply_derivative(s: DiffStencil, f: Array, h: float = 1.0) -> Array: """Apply the stencil to a function *f* and a step size *h*. Note that only interior points are correctly computed. Any boundary @@ -73,7 +73,7 @@ def apply_derivative(s: Stencil, f: Array, h: float = 1.0) -> Array: def determine_stencil_truncation_error( - s: Stencil, + s: DiffStencil, *, atol: float | None = None, ) -> Truncation: @@ -104,7 +104,7 @@ def determine_stencil_truncation_error( return Truncation(i - s.derivative, c) -def modified_wavenumber(s: Stencil, k: Array) -> Array: +def modified_wavenumber(s: DiffStencil, k: Array) -> Array: """Compute the modified wavenumber of the stencil *s* at each number *k*. :arg k: wavenumber at which to compute the derivative. @@ -123,7 +123,7 @@ def make_taylor_approximation( bounds: tuple[int, int], *, dtype: np.dtype[Any] | None = None, -) -> Stencil: +) -> DiffStencil: r"""Determine a finite difference stencil by solving a linear system from the Taylor expansion. @@ -143,8 +143,8 @@ def make_taylor_approximation( if bounds[0] > bounds[1]: bounds = (bounds[1], bounds[0]) - if bounds[0] >= 0 or bounds[1] <= 0: - raise ValueError(f"Bounds must be (smaller, bigger): {bounds}") + if bounds[0] > 0 or bounds[1] < 0: + raise ValueError(f"Bounds must be (smaller <= 0, bigger >= 0): {bounds}") if dtype is None: dtype = np.dtype(np.float64) @@ -162,7 +162,7 @@ def make_taylor_approximation( x = np.linalg.solve(A, b) assert np.allclose(np.sum(x), 0.0) - return Stencil( + return DiffStencil( derivative=derivative, coeffs=x, offsets=offsets, diff --git a/tests/test_diff.py b/tests/test_diff.py index 8e028ed..6462794 100644 --- a/tests/test_diff.py +++ b/tests/test_diff.py @@ -8,7 +8,7 @@ import numpy as np from pycaputo.differentiation.finite_difference import ( - Stencil, + DiffStencil, apply_derivative, determine_stencil_truncation_error, make_taylor_approximation, @@ -23,7 +23,7 @@ # {{{ test_finite_difference_taylor -def finite_difference_convergence(d: Stencil) -> EOCRecorder: +def finite_difference_convergence(d: DiffStencil) -> EOCRecorder: eoc = EOCRecorder() s = np.s_[abs(d.offsets[0]) + 1 : -abs(d.offsets[-1]) - 1] @@ -133,7 +133,7 @@ def test_finite_difference_taylor_stencil(*, visualize: bool = False) -> None: [-0.02651995, 0.18941314, -0.79926643, 0.0, 0.79926643, -0.18941314, 0.02651995] ) offsets = np.arange(-3, 4) - s = Stencil(derivative=1, coeffs=a, offsets=offsets) + s = DiffStencil(derivative=1, coeffs=a, offsets=offsets) order, c = determine_stencil_truncation_error(s, atol=1.0e-6) assert order == 4