derivative_matrix#
- cherab.inversion.derivative.derivative_matrix(grid_shape: tuple[int, int], grid_step: float = 1.0, axis: int = 0, scheme: Literal['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,optional Grid step size along the user-specified axis, by default 1.0.
- axis
int,optional Axis along which the derivative is taken. Default is 0. Choose from 0 or 1.
- scheme{“forward”, “backward”, “central”},
optional Scheme of the derivative. Default is “forward”. Choose from “forward”, “backward”, or “central”.
- mask
ndarray,optional Mask array. Default is None. If masking a certain grid point, the corresponding row and column is set to
Falsein the mask array.
- grid_shape
- 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].
- (
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.]])