From 2688f070b61ff0f2a94d2cfd21a6db801d3ab324 Mon Sep 17 00:00:00 2001 From: pavelkomarov Date: Tue, 21 Jan 2025 17:04:36 -0800 Subject: [PATCH] updated to spectral-derivatives 0.7 --- derivative/dglobal.py | 36 ++++++++++++++++++------------------ pyproject.toml | 2 +- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/derivative/dglobal.py b/derivative/dglobal.py index 0a8b564..5ffa0ad 100644 --- a/derivative/dglobal.py +++ b/derivative/dglobal.py @@ -12,36 +12,36 @@ @register("spectral") class Spectral(Derivative): - def __init__(self, order=1, axis=0, basis='chebyshev', **kwargs): + def __init__(self, order=1, axis=0, basis='fourier', filter=None): """ - Compute the numerical derivative by first computing the FFT. In Fourier space, derivatives are multiplication - by i*phase; compute the IFFT after. - - Args: + Compute the numerical derivative by spectral methods. In Fourier space, derivatives are multiplication + by i*phase; compute the inverse transform after. Use either Fourier modes of Chebyshev polynomials as + the basis. + + Keyword Args: order (int): order of the derivative, defaults to 1st order axis (int): the dimension of the data along which to differentiate, defaults to first dimension - basis (str): "chebyshev" or "fourier", the set of basis functions to use for differentiation - **kwargs: Optional keyword arguments. - - Keyword Args: - filter: Optional. A function that takes in frequencies and outputs weights to scale the coefficient at - the input frequency in Fourier space. Input frequencies are the discrete fourier transform sample - frequencies associated with the domain variable. Look into python signal processing resources in - scipy.signal for common filters. - + basis (str): 'fourier' or 'chebyshev', the set of basis functions to use for differentiation + Note `basis='fourier'` assumes your function is periodic and sampled over a period of its domain, + [a, b), and `basis='chebyshev'` assumes your function is sampled at cosine-spaced points on the + domain [a, b]. + filter: Optional. A function that takes in wavenumbers and outputs weights to scale the + corresponding mode in frequency space. Input wavenumbers are the k used in the discrete fourier + transform. Look into python signal processing resources in scipy.signal for common filters. """ - # Filter function. Default: Identity filter - self.filter = kwargs.get('filter', np.vectorize(lambda f: 1)) self.order = order + self.axis = axis if basis not in ['chebyshev', 'fourier']: raise ValueError("Only chebyshev and fourier bases are allowed.") self.basis = basis + self.filter = filter + @_memoize_arrays(1) # the memoization is 1 deep, as in only remembers the most recent args def _global(self, t, x): if self.basis == 'chebyshev': - return cheb_deriv(x, t, self.order, self.axis) + return cheb_deriv(x, t, self.order, self.axis, self.filter) else: # self.basis == 'fourier' - return fourier_deriv(x, t, self.order, self.axis) + return fourier_deriv(x, t, self.order, self.axis, self.filter) def compute(self, t, x, i): return next(self.compute_for(t, x, [i])) diff --git a/pyproject.toml b/pyproject.toml index 19b1870..f7e7629 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,7 +24,7 @@ numpy = ">=1.18.3" scipy = "^1.4.1" scikit-learn = "^1" importlib-metadata = ">=7.1.0" -spectral-derivatives = ">=0.6" +spectral-derivatives = ">=0.7" # docs sphinx = {version = "7.2.6", optional = true}