Spectral Derivatives
This repository is home to Python code that can take spectral derivatives using the Chebyshev, Fourier, Legendre, and Bernstein bases, grounded in some pretty elegant, deep math. That is, given a vector representing samples of a smooth function, the code returns numerical derivatives, indicating slope, curvature, etc. at the sample points of an interpolation built from the basis functions.
When using the Fourier basis, spectral derivatives require periodic boundaries, but the Chebyshev basis allows arbitrary boundaries, extending the method to a much wider class of functions (albeit via nonuniform sampling).
This package can be useful any time you want to take a derivative numerically, such as for doing PDE simulations. For taking derivatives of noisy data, spectral methods naturally enable global filtering by weighting basis function contributions.
Note these methods are best for situations when you don't know the generating function of your data; if the generator is known, then autodifferentiation tools like JAX are your friend.
Installation and Usage
The package is a single module containing derivative functions. To install, execute:
python3 -m pip install spectral-derivatives
or from the source code
python3 -m pip install .
You should now be able to
>>> from specderiv import *
>>> import numpy as np
>>>
>>> x_n = np.cos(np.arange(21) * np.pi / 20) # cosine-spaced, includes last point
>>> y_n = np.sin(x_n) # can be periodic or aperiodic on domain [a, b]
>>> dy_n = cheb_deriv(y_n, x_n, 1)
>>>
>>> th_n = np.arange(20) * 2*np.pi / 20 # equispaced, excludes last point
>>> y_n = np.sin(th_n) # must be periodic on domain [a, b)
>>> dy_n = fourier_deriv(y_n, th_n, 1)
For further usage examples, including in higher dimension, see the Jupyter notebooks: Chebyshev, Fourier, Legendre, and Bernstein.
Note that for fastest and most accurate results you should use equispaced samples on an open periodic interval with fourier and cosine-spaced points with chebyshev. legendre and bernstein are slower. For examples which use arbitrary domains, see this notebook.
References
- Trefethen, N., 2000, Spectral Methods in Matlab, https://epubs.siam.org/doi/epdf/10.1137/1.9780898719598.ch8
- Johnson, S., 2011, Notes on FFT-based differentiation, https://math.mit.edu/~stevenj/fft-deriv.pdf
- Kutz, J.N., 2023, Data-Driven Modeling & Scientific Computation, Ch. 11, https://faculty.washington.edu/kutz/kutz_book_v2.pdf
Metadata
Release files for spectral-derivatives 0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| spectral_derivatives-0.10.tar.gz | 7.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| spectral_derivatives-0.10-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.9 kB
Release files / spectral_derivatives-0.10.tar.gz
| Download URL | spectral_derivatives-0.10.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
abea9c6d4ce3cf6fbc3feec38074f0b38c6afbda70a01cc334254174d567010c
|
|
BLAKE2b-256 checksum How to use checksums |
329f585f7962d922b4750e73863456a9119e71413a767c6a7b57b295542324e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|
Release files / spectral_derivatives-0.10-py3-none-any.whl
| Download URL | spectral_derivatives-0.10-py3-none-any.whl |
|---|---|
| Size | 8.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4addcc48cd908af38ecff87139c77c7e921f9c8daadbaf4a1671d24122015432
|
|
BLAKE2b-256 checksum How to use checksums |
6baf8fbdb604a939a4679f3e382b090432624746f415e085cad46ec59d10352e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|