specderiv module

specderiv.cheb_deriv(y_n: ndarray, t_n: ndarray, order: int, axis: int = 0, filter: callable = None, dct_type=1, calc_endpoints=True)[source]

Evaluate derivatives with Chebyshev polynomials via discrete cosine and sine transforms. Caveats:

  • Taking the 1st derivative twice with a discrete method like this is not exactly the same as taking the second derivative.

  • For derivatives over the 4th, this method presently returns NaN at the edges of the domain. Be cautious if passing the result to another function.

Parameters:
  • y_n (np.ndarray) – one-or-multi-dimensional array, values of a function, 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. If you’re using canonical Chebyshev points with the DCT-I, this will be x_n = np.cos(np.arange(N+1) * np.pi / N) (\(x \in [1, -1]\)). With the DCT-II, x_n = np.concatenate(([1], np.cos((np.arange(N+1) + 0.5) * np.pi/(N+1)), [-1])). In either case, on a domain from \(a\) to \(b\), this is stretched to t_n = x_n * (b - a)/2 + (b + a)/2. Note the order is high-to-low in the \(x\) or \(t\) domain, but low-to-high in \(n\). Also 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.

  • dct_type (int, optional) – 1 or 2, whether to use DCT-I or DCT-II. Defaults to DCT-I.

  • calc_endpoints (bool, optional) – Whether to calculate the endpoints of the answer, in case they are unnecessary for a particular use case. Defaults to True. False silences the NaN warning for order \(> 4\).

Returns:

(np.ndarray) – dy_n, shaped like y_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 be t_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 like y_n, samples of the \(\nu^{th}\) derivative of the function