specderiv module¶
- specderiv.bern_deriv(y_n: ndarray, t_n: ndarray, order: int, axis: int = 0, cutoff: int = None)[source]¶
Evaluate derivatives with Bernstein polynomials via series derivative rule. Warning: This function is relatively expensive, \(O(N^3)\) rather than \(O(N \log N)\). However, it comes with a neat uniform convergence guarantee.
- Parameters:
y_n (np.ndarray) – one-or-multi-dimensional array, values of a function.
t_n (np.ndarray) – 1D array, where the function \(y\) is sampled in the dimension of differentation. Note both endpoints are inclusive. A sampling that clusters at the domain edges is better for keeping down Runge phenomenon.
order (int) – The order of differentiation, also called \(\nu\). Must be \(\geq 1\).
axis (int, optional) – For multi-dimensional
y_n
, the dimension along which to take the derivative. Defaults to the first dimension (axis=0).cutoff (int, optional) – Bernstein fits work differently than the other bases, because each basis function looks like a little bump, effectively focusing its contribution to a particular part of the domain. As such, all modes have about the same frequency content, so we don’t filter higher modes; we instead decide to fit only
cutoff
\(<N\) of them, which makes each one broader and hence reduces its ability to fit noise.
- Returns:
(np.ndarray) –
dy_n
, shaped likey_n
, samples of the \(\nu^{th}\) derivative of the function
- specderiv.cheb_deriv(y_n: ndarray, t_n: ndarray, order: int, axis: int = 0, filter: callable = None)[source]¶
Evaluate derivatives with Chebyshev polynomials via series derivative rule.
- Parameters:
y_n (np.ndarray) – one-or-multi-dimensional array, values of a function, best sampled at cosine-spaced points in the dimension of differentiation.
t_n (np.ndarray) – 1D array, where the function \(y\) is sampled in the dimension of differentation. Use cosine-spaced points, i.e.
t_n = x_n * (b - a)/2 + (b + a)/2
for a domain between \(a\) and \(b\), wherex_n = np.cos(np.arange(N+1) * np.pi / N)
, to enable \(O(N \log N)\) transforms to and from the basis domain. Note the ordering of these points counts up in \(n\), which is right-to-left, from 1 to -1 in the \(x\) domain. If instead you want to use arbitrary sample points, this is allowed, but the code will warn that you are incurring \(O(N^3)\) cost. Note both endpoints are inclusive.order (int) – The order of differentiation, also called \(\nu\). Must be \(\geq 1\).
axis (int, optional) – For multi-dimensional
y_n
, the dimension along which to take the derivative. Defaults to the first dimension (axis=0).filter (callable, optional) – A function or
lambda
that takes the 1D array of Chebyshev polynomial numbers, \(k = [0, ... N]\), and returns a same-shaped array of weights, which get multiplied in to the initial frequency transform of the data, \(Y_k\). Can be helpful when taking derivatives of noisy data. The default is to apply #nofilter.
- Returns:
(np.ndarray) –
dy_n
, shaped likey_n
, samples of the \(\nu^{th}\) derivative of the function
- specderiv.fourier_deriv(y_n: ndarray, t_n: ndarray, order: int, axis: int = 0, filter: callable = None)[source]¶
Evaluate derivatives with complex exponentials via FFT. Caveats:
Only for use with periodic functions.
Taking the 1st derivative twice with a discrete method like this is not exactly the same as taking the second derivative.
- Parameters:
y_n (np.ndarray) – one-or-multi-dimensional array, values of a period of a periodic function, sampled at equispaced points in the dimension of differentiation.
t_n (np.ndarray) – 1D array, where the function \(y\) is sampled in the dimension of differentiation. If you’re using canonical Fourier points, this will be
th_n = np.arange(M) * 2*np.pi / M
(\(\theta \in [0, 2\pi)\)). If you’re sampling on a domain from \(a\) to \(b\), this needs to bet_n = np.arange(0, M)/M * (b - a) + a
. Note the lower, left bound is inclusive and the upper, right bound is exclusive.order (int) – The order of differentiation, also called \(\nu\). Can be positive (derivative) or negative (antiderivative, raises warning).
axis (int, optional) – For multi-dimensional
y_n
, the dimension along which to take the derivative. Defaults to the first dimension (axis=0).filter (callable, optional) – A function or
lambda
that takes the array of wavenumbers, \(k = [0, ... \frac{M}{2} , -\frac{M}{2} + 1, ... -1]\) for even \(M\) or \(k = [0, ... \lfloor \frac{M}{2} \rfloor, -\lfloor \frac{M}{2} \rfloor, ... -1]\) for odd \(M\), and returns a same-shaped array of weights, which get multiplied in to the initial frequency transform of the data, \(Y_k\). Can be helpful when taking derivatives of noisy data. The default is to apply #nofilter.
- Returns:
(np.ndarray) –
dy_n
, shaped likey_n
, samples of the \(\nu^{th}\) derivative of the function
- specderiv.legendre_deriv(y_n: ndarray, t_n: ndarray, order: int, axis: int = 0, filter: callable = None)[source]¶
Evaluate derivatives with Legendre polynomials via series derivative rule, completely analogous to the Chebyshev method with not-cosine-spaced sample points. Warning: This function is relatively expensive, \(O(N^3)\) rather than \(O(N \log N)\).
- Parameters:
y_n (np.ndarray) – one-or-multi-dimensional array, values of a function.
t_n (np.ndarray) – 1D array, where the function \(y\) is sampled in the dimension of differentation. Note both endpoints are inclusive. A sampling that clusters at the domain edges is better for keeping down Runge phenomenon.
order (int) – The order of differentiation, also called \(\nu\). Must be \(\geq 1\).
axis (int, optional) – For multi-dimensional
y_n
, the dimension along which to take the derivative. Defaults to the first dimension (axis=0).filter (callable, optional) – A function or
lambda
that takes the 1D array of mode numbers, \(k = [0, ... N]\), and returns a same-shaped array of weights, which get multiplied in to the initial frequency transform of the data, \(Y_k\). Can be helpful when taking derivatives of noisy data. The default is to apply #nofilter.
- Returns:
(np.ndarray) –
dy_n
, shaped likey_n
, samples of the \(\nu^{th}\) derivative of the function