pysdic.numeric.assemble_finite_difference_matrix#

assemble_finite_difference_matrix(times, order, *, accuracy=2, boundary='one_sided', max_gap=None, n_dim=1, sparse=True, return_indices=False)[source]#

Assemble the finite difference operator of a derivative for a signal sampled at any strictly increasing times, with uniform or non-uniform steps.

Each row approximates the derivative of order \(n\) at one sampling time \(t_i\) from the values at the neighboring sampling times, with coefficients computed for the actual offsets (see compute_finite_difference_coefficients()):

  • centered stencil (\(2 \lfloor (n + p - 1) / 2 \rfloor + 1\) points) when the neighbors are uniformly spaced around \(t_i\);

  • otherwise (non-uniform steps around \(t_i\)) the most centered window of \(n + p\) consecutive samples, which keeps the error in \(O(h^p)\) without symmetry;

  • near the ends of the sampling, the window is shifted inside the sampling (one-sided stencil of \(n + p\) points) with boundary="one_sided", or the row is removed with boundary="valid".

The operator only uses the given samples: the coefficients of each row depend on the actual steps between them. For example, the frames of an analysis without displacement are simply not given: the step between the given frames around them is larger, and no value is interpolated or extrapolated for them.

With max_gap, the sampling is split into segments at the steps larger than max_gap, and each segment is processed independently (as if the step was an end of the sampling): no stencil spans such a step. The segments shorter than the stencil have no row.

The signal can have \(D\) components at each time (n_dim). The values are ordered time by time, \(F = [f_1(t_1), \dots, f_D(t_1), f_1(t_2), \dots]\), and the operator is \(T \otimes I_D\).

Parameters:
  • times (ArrayLike) – The strictly increasing sampling times, of shape \((N_t,)\).

  • order (Integral) – The order \(n \geq 1\) of the derivative.

  • accuracy (Integral, optional (default: 2)) – The order \(p\) of the error, a positive even integer.

  • boundary (str, optional (default: "one_sided")) – The treatment of the ends of the sampling (and of the segments): "one_sided" or "valid" (see above).

  • max_gap (Optional[Real], optional) – The largest step between two consecutive samples that a stencil can span. By default, no limit.

  • n_dim (Integral, optional (default: 1)) – The number \(D \geq 1\) of components of the signal at each time.

  • sparse (bool, optional (default: True)) – If True, the operator is a scipy.sparse.csr_matrix, otherwise a dense array.

  • return_indices (bool, optional (default: False)) – If True, also return the indices of the sampling times of the rows.

Returns:

  • matrix (Union[numpy.ndarray, scipy.sparse.csr_matrix]) – The operator of shape \((N_r D, N_t D)\), \(N_r\) being the number of evaluated times (\(N_t\) with boundary="one_sided" and no max_gap).

  • indices (numpy.ndarray) – Only with return_indices=True: the indices \((N_r,)\) of the sampling times of the rows.

Raises:

ValueError – If the times are not strictly increasing or an input has a wrong value.

Return type:

ndarray | csr_matrix | Tuple

Examples

Second derivative of a signal sampled at the times 0, 1, 2, 3, 6, 7, 8, 9 (a step of 3 between 3 and 6):

import numpy
from pysdic.numeric import assemble_finite_difference_matrix

times = numpy.array([0, 1, 2, 3, 6, 7, 8, 9], dtype=float)
D = assemble_finite_difference_matrix(times, 2)
D @ times**2  # 2 everywhere (exact for the polynomials of degree <= 3)