Functions to take spectral derivatives with the Chebyshev and Fourier bases
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spectral_derivatives-0.10.tar.gz.
File metadata
- Download URL: spectral_derivatives-0.10.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abea9c6d4ce3cf6fbc3feec38074f0b38c6afbda70a01cc334254174d567010c
|
|
| MD5 |
51a210d4ae9cba75395ca3e18c5e3016
|
|
| BLAKE2b-256 |
329f585f7962d922b4750e73863456a9119e71413a767c6a7b57b295542324e6
|
File details
Details for the file spectral_derivatives-0.10-py3-none-any.whl.
File metadata
- Download URL: spectral_derivatives-0.10-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4addcc48cd908af38ecff87139c77c7e921f9c8daadbaf4a1671d24122015432
|
|
| MD5 |
bfef7af10f7ed7c7c8113a693ce87b10
|
|
| BLAKE2b-256 |
6baf8fbdb604a939a4679f3e382b090432624746f415e085cad46ec59d10352e
|