cherab.phix.tools.derivative.compute_dmat#
- cherab.phix.tools.derivative.compute_dmat(voxel_map, kernel_type='laplacian4', kernel=None)#
Generate derivative sparse matrix.
- Parameters:
voxel_map (numpy.ndarray) – (N, M) voxel map matrix (negative value must be input into masked voxels) If the additional dimension size of the matrix is 1, then it is squeezed to a 2-D matrix.
kernel_type ({"x", "y", "r", "z", "laplacian4", "laplacian8", "custom"}, optional) – Derivative kernel type. Default is “laplacian8”.
"custom"is available only whenkernelis specified. “r” and “z” are the same as “y” and “x”, respectively.kernel (numpy.ndarray, optional) – (3, 3) custom kernel matrix. Default is None.
- Returns:
(N, N) derivative Compressed Sparse Column matrix (if N > M)
- Return type:
Notes
The derivative matrix is generated by the kernel convolution method. The kernel is a 3x3 matrix, and the convolution is performed as follows:
\[I_{x, y}' = \sum_{i=-1}^{1}\sum_{j=-1}^{1} K_{i,j} \times I_{x + i, y + j},\]where \(I_{x, y}\) is the 2-D image at the point \((x, y)\) and \(K_{i,j}\) is the kernel matrix. Using derivative kernel like a laplacian filter, the derivative matrix defined as follows is generated:
\[\mathbf{I}' = \mathbf{K} \cdot \mathbf{I},\]where \(\mathbf{I}\) is the vecotrized image and \(\mathbf{K}\) is the derivative matrix.
The implemented derivative kernels are as follows:
First derivative in x-direction (
kernel_type="x"or"z"): \(\mathbf{K} = \begin{bmatrix} 0 & 0 & 0 \\ -1 & 1 & 0 \\ 0 & 0 & 0 \end{bmatrix}\)First derivative in y-direction (
kernel_type="y"or"r"): \(\mathbf{K} = \begin{bmatrix} 0 & -1 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 0 \end{bmatrix}\)Laplacian-4 (
kernel_type="laplacian4"): \(\mathbf{K} = \begin{bmatrix} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0 \end{bmatrix}\)Laplacian-8 (
kernel_type="laplacian8"): \(\mathbf{K} = \begin{bmatrix} 1 & 1 & 1 \\ 1 & -8 & 1 \\ 1 & 1 & 1 \end{bmatrix}\)
Examples
from raysect.optical import World from cherab.phix.tools.raytransfer import import_phix_rtc from cherab.phix.tools import compute_dmat world = World() rtc = import_phix_rtc(world) laplacian = compute_dmat(rtc.voxel_map) laplacian.toarray() array([[-8., 1., 0., ..., 0., 0., 0.], [ 1., -8., 1., ..., 0., 0., 0.], [ 0., 1., -8., ..., 0., 0., 0.], ..., [ 0., 0., 0., ..., -8., 1., 0.], [ 0., 0., 0., ..., 1., -8., 1.], [ 0., 0., 0., ..., 0., 1., -8.]])