Derivative matrix

5.3. Derivative matrix#

Used in regularised inversion methods MFR and LAME. Various schemes for computation are used. Anisotropic derivatives can be computed using magnetics data.

5.3.1. Implementation#

Can compute derivative matrices, both isotropic and anisotropic based on magnetic flux values.

Handles computation of derivative matrices used for regularization in MFR algorithm.

tomotok.regularisations.derivatives.all_direction_derivative_matrices(grid: RegularGrid, scheme: str = 'forward', mask: ndarray | None = None, compensate_edges: bool = True, compensation_fraction: float = 1) list[csc_array]

Creates derivative matrices for all 8 directions using provided scheme.

Parameters:
gridRegularGrid
schemestr, optional

Selects numerical scheme to be used. Can be ‘forward’, ‘backward’, ‘central’, ‘second’. The default value is ‘forward’.

masknumpy.ndarray, optional

A bool mask determining nodes of regular grid to keep, by default None. Rows and columns representing False nodes are removed from derivative matrix. If mask is None, all rows and columns are returned.

compensate_edgesbool, optional

Subtracts from diagonal so that sum of each row is zero, by default False.

compensation_fractionfloat, optional

Fraction of diagonal elements to be subtracted, by default 1.

Returns:
list of scipy.sparse.csc_matrix

List of derivative matrices for all 8 directions with following order: right, top-right, top, top-left, left, bottom-left, bottom, bottom-right

tomotok.regularisations.derivatives.standard_anisotropic_derivative_matrices(grid: RegularGrid, flux: ndarray, mask: ndarray | None = None, compensate_edges: bool = True, compensation_fraction: float = 1) list[csc_array]

Creates isotropic derivative matrices for parallel and perpendicular directions.

Parameters:
gridRegularGrid
fluxesnumpy.ndarray

Matrix with magnetic flux values, shape has to match grid

schemestr, optional

Selects numerical scheme to be used. Can be ‘forward’, ‘backward’, ‘central’, ‘second’. The default value is ‘forward’.

masknumpy.ndarray, optional

A bool mask determining nodes of regular grid to keep, by default None. Rows and columns representing False nodes are removed from derivative matrix. If mask is None, all rows and columns are returned.

compensate_edgesbool, optional

Subtracts from diagonal so that sum of each row is zero, by default False.

compensation_fractionfloat, optional

Fraction of row sum to subtract from diagonal, by default 1.

Returns:
list of scipy.sparse.csc_matrix

List of derivative matrices for parallel and perpendicular directions in following order: parallel counter clockwise, perpendicular counter clockwise, parallel clockwise, perpendicular clockwise

tomotok.regularisations.derivatives.reduce_matrix(mat: sparray, mask: ndarray) sparray

Creates reduced matrix by cutting out rows and columns representing unwanted nodes.

Parameters:
matscipy.sparse.sparray

matrix to be reduced

masknumpy.ndarray of bool

mask array for selecting desired nodes, True for nodes to keep must be 1D or 2D, size must match grid size (# rows or # columns)

Returns:
scipy.sparse.sparray

reduced matrix

tomotok.regularisations.derivatives.compensate_matrix(mat: sparray, compensation_fraction: float = 1) sparray

Subtracts from diagonal so that sum of each row is zero.

Parameters:
matscipy.sparse.sparray

matrix to be compensated

compensation_fractionfloat, optional

Fraction of row sum to subtract from diagonal, by default 1. 1 corresponds to full compensation where row sum should be zero. However, the compensation may cause small negative values due to rounding errors. Using slightly smaller value (e.g. 0.9999) can prevent this issue while still effectively acting as a zero row sum constraint.

Returns:
scipy.sparse.sparray

compensated matrix

tomotok.regularisations.derivatives.derivative_matrix(grid: RegularGrid, direction: str, scheme: str = 'forward', mask: ndarray | None = None, compensate_edges: bool = True, compensation_fraction: float = 1) csc_array

Creates a derivative matrix using numerical differences

Parameters:
gridRegularGrid
directionstr

Determines one of the 8 possible directions that can be used. Specified using the over edge neighbor ‘right’, ‘top’, ‘left’, ‘bottom’ or combination over corner directions like ‘top-left’

schemestr, optional

Selects numerical scheme to be used. Can be ‘forward’, ‘backward’, ‘central’, ‘second’. The default value is ‘forward’.

masknumpy.ndarray, optional

A bool mask determining nodes of regular grid to keep, by default None. Rows and columns representing False nodes are removed from derivative matrix. If mask is None, all rows and columns are returned.

compensate_edgesbool, optional

Subtracts from diagonal so that sum of each row is zero, by default False.

compensation_fractionfloat, optional

Fraction of row sum to subtract from diagonal, by default 1.

Returns:
scipy.sparse.csc_array

See also

all_direction_derivative_matrices

creates derivative matrices for all 8 directions

reduce_matrix

reduces matrix by cutting out rows and columns representing unwanted nodes

compensate_matrix

subtracts from diagonal so that sum of each row is zero or nearly zero

tomotok.regularisations.derivatives.laplace_matrix(grid: RegularGrid, mask: ndarray | None = None, compensate_edges: bool = True, compensation_fraction: float = 1.0, diagonals: bool = True) csc_array

Creates sparse laplace matrix.

Parameters:
gridRegularGrid
masknumpy.ndarray, optional

A bool mask determining nodes of regular grid to keep, by default None. Rows and columns representing False nodes are removed from derivative matrix. If mask is None, all rows and columns are returned.

compensate_edgesbool, optional

Subtracts from diagonal so that sum of each row is zero, by default False.

diagonalsbool, optional

Selects whether to use diagonal neighbors in matrix, by default True.

Returns:
scipy.sparse.csc_matrix
tomotok.regularisations.derivatives.anisotropic_derivative_matrix(grid: RegularGrid, flux: ndarray, direction: str = 'parallel', scheme: str = 'forward', mask: ndarray | None = None, compensate_edges: bool = True, compensation_fraction: float = 1.0) csc_array

Computes derivative matrix with varying direction based on flux surfaces shapes.

Uses direction of gradient to distribute contribution to two neighboring nodes. These are designated as previous and next as the direction rotates. The weight of next node is equal to remnant of direction angle division by 1. The weight of previous node is equal to 1 minus the weight of next node.

Parameters:
gridRegularGrid

Reconstruction grid definition

fluxnumpy.ndarray

Matrix with magnetic flux values, shape has to match grid

masknumpy.ndarray of bool, optional

A bool mask determining nodes of regular grid to keep, by default None.

directionstr, optional

Direction of derivative computation, by default ‘parallel’

schemestr, optional
derivative scheme used, by default ‘forward’
  • forward is in the direction of positive angle i.e. counter clockwise

  • backward is in the direction of negative angle i.e. clockwise

Returns:
scipy.sparse.csc_matrix

See also

standard_anisotropic_derivative_matrices

creates derivative matrices for parallel and perpendicular directions

reduce_matrix

reduces matrix by cutting out rows and columns representing unwanted nodes

compensate_matrix

subtracts from diagonal so that sum of each row is zero or nearly zero