cherab.inversion.tools.derivative_matrix¶

cherab.inversion.tools.derivative_matrix(grid_shape: tuple[int, int], grid_step: float = 1.0, axis: int = 0, scheme: 'forward' | 'backward' | 'central' = 'forward', mask: ndarray | None = None) csc_arraySource¶

Generate derivative matrix.

This function computes the derivative matrix for a regular orthogonal coordinate grid. The grid points must be equally spaced along the given axis. The numerical scheme is based on the finite difference method of forward, backward, or central difference. The dirichlet boundary condition is applied to the edge of the grid.

Parameters:
grid_shape: tuple[int, int]¶

Shape of the grid (N0, N1), where N0 and N1 are the number of grid points along the axis 0 and 1 respectively.

grid_step: float = 1.0¶

Grid step size along the user-specified axis, by default 1.0.

axis: int = 0¶

Axis along which the derivative is taken. Default is 0. Choose from 0 or 1.

scheme: 'forward' | 'backward' | 'central' = 'forward'¶

Scheme of the derivative. Default is “forward”. Choose from “forward”, “backward”, or “central”.

mask: ndarray | None = None¶

Mask array. Default is None. If masking a certain grid point, the corresponding row and column is set to False in the mask array.

Returns:

(N, N) scipy.sparse.csc_array – Derivative Compressed Sparse Column matrix, where N is the number of grid points same as grid_shape[0] * grid_shape[1].

Raises:
  • ValueError – If grid_shape has non-positive integers, grid_step is non-positive, axis is not 0 or 1, or scheme is not one of “forward”, “backward”, or “central”.

  • TypeError – If mask is not None or numpy.ndarray, or if its shape is different from grid_shape.

Notes

The detailed explanation of the derivative matrix can be found in the theory of the derivative matrix.

Examples

>>> dmat = derivative_matrix((3, 3), 1, axis=0, scheme="forward")
>>> dmat.toarray()
array([[-1.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
       [ 0., -1.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  0., -1.,  0.,  0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0., -1.,  0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0., -1.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  0., -1.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  0., -1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0., -1.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0., -1.]])