cherab.inversion.tools.laplacian_matrix

cherab.inversion.tools.laplacian_matrix(grid_shape: tuple[int, int], grid_steps: tuple[float, float] = (1.0, 1.0), diagonal: bool = True, mask: ndarray | None = None) csc_arraySource

Generate laplacian matrix.

This function computes the laplacian 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. 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_steps: tuple[float, float] = (1.0, 1.0)

Step size of the grid (h0, h1), where h0 and h1 are the step size along the axis 0 and 1 respectively, by default (1.0, 1.0).

diagonal: bool = True

Whether to include the diagonal term or not. Default is True.

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 – Laplacian 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 laplacian matrix can be found in the theory of the laplacian matrix.

Examples

>>> lmat = laplacian_matrix((3, 3), (1, 1), diagonal=False)
>>> lmat.toarray()
array([[-4.,  1.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
       [ 1., -4.,  1.,  0.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  1., -4.,  0.,  0.,  1.,  0.,  0.,  0.],
       [ 1.,  0.,  0., -4.,  1.,  0.,  1.,  0.,  0.],
       [ 0.,  1.,  0.,  1., -4.,  1.,  0.,  1.,  0.],
       [ 0.,  0.,  1.,  0.,  1., -4.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  1.,  0.,  0., -4.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  1.,  0.,  1., -4.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  1., -4.]])
>>> lmat2 = laplacian_matrix((3, 3), (1, 1), diagonal=True)
>>> lmat2.toarray()
array([[-6. ,  1. ,  0. ,  1. ,  0.5,  0. ,  0. ,  0. ,  0. ],
       [ 1. , -6. ,  1. ,  0.5,  1. ,  0.5,  0. ,  0. ,  0. ],
       [ 0. ,  1. , -6. ,  0. ,  0.5,  1. ,  0. ,  0. ,  0. ],
       [ 1. ,  0.5,  0. , -6. ,  1. ,  0. ,  1. ,  0.5,  0. ],
       [ 0.5,  1. ,  0.5,  1. , -6. ,  1. ,  0.5,  1. ,  0.5],
       [ 0. ,  0.5,  1. ,  0. ,  1. , -6. ,  0. ,  0.5,  1. ],
       [ 0. ,  0. ,  0. ,  1. ,  0.5,  0. , -6. ,  1. ,  0. ],
       [ 0. ,  0. ,  0. ,  0.5,  1. ,  0.5,  1. , -6. ,  1. ],
       [ 0. ,  0. ,  0. ,  0. ,  0.5,  1. ,  0. ,  1. , -6. ]])