iwopy.utils.discretization

Classes

RegularDiscretizationGrid

A lightweight regular grid in n dimensions,

Module Contents

class iwopy.utils.discretization.RegularDiscretizationGrid(origin: numpy.typing.ArrayLike, deltas: numpy.typing.ArrayLike, n_steps: numpy.typing.ArrayLike, interpolation: 'nearest' | 'linear' | None = None, tol: numpy.typing.ArrayLike | None = None, digits: int = 12)[source]

A lightweight regular grid in n dimensions, without points storage.

Parameters:
origin: numpy.typing.ArrayLike

The origin point, len: n_dims

deltas: numpy.typing.ArrayLike

The step sizes, len: n_dims.

n_steps: numpy.typing.ArrayLike

The number of steps, len: n_dims. Use INT_INF for infinite.

interpolation: 'nearest' | 'linear' | None = None

The interpolation method: None, nearest, linear

tol: numpy.typing.ArrayLike | None = None

The tolerances for grid bounds, default is 0, shape: (n_dims,)

digits: int = 12

The grid point precision

all_gridpoints(pts: numpy.ndarray, allow_outer: bool = True) → bool[source]

Checks if all points are on grid.

Parameters:
pts: numpy.ndarray

The points space, shape: (n_pts, n_dims)

allow_outer: bool = True

Allow outermost point indices, else reduce those to lower-left cell corner

Returns:

all_on_grid – True if all points on grid

apply_tol(p: numpy.ndarray) → numpy.ndarray[source]

Get tolerance corrected point

Parameters:
p: numpy.ndarray

The point, shape: (n_dims,)

Returns:

q – The corrected point, shape: (n_dims,)

apply_tols(pts: numpy.ndarray) → numpy.ndarray[source]

Get tolerance corrected points

Parameters:
pts: numpy.ndarray

The points, shape: (n_pts, n_dims)

Returns:

q – The corrected points, shape: (n_pts, n_dims)

deriv_coeffs(pts: numpy.ndarray, var: int, order: int = 2, orderb: int = 1) → tuple[numpy.ndarray, numpy.ndarray][source]

Calculates the derivative coefficients at points.

Parameters:
pts: numpy.ndarray

The evaluation points, shape: (n_pts, n_dims)

var: int

The dimension representing the variable wrt which to differentiate

order: int = 2

The finite difference order, 1 = forward, -1 = backward, 2 = centre

orderb: int = 1

The finite difference order at boundary points

Returns:

  • gpts – The grid points relevant for coeffs, shape: (n_gpts, n_dims)

  • coeffs – The gradient coefficients, shape: (n_pts, n_gpts)

deriv_coeffs_gridpoints(inds: numpy.ndarray, var: int, order: int = 2, orderb: int = 1) → tuple[numpy.ndarray, numpy.ndarray][source]

Calculates the derivative coefficients at grid points.

Parameters:
inds: numpy.ndarray

The integer grid point indices, shape: (n_inds, n_dims)

var: int

The dimension representing the variable wrt which to differentiate

order: int = 2

The finite difference order, 1 = forward, -1 = backward, 2 = centre

orderb: int = 1

The finite difference order at boundary points

Returns:

  • gpts – The grid points relevant for coeffs, shape: (n_gpts, n_dims)

  • coeffs – The gradient coefficients, shape: (n_inds, n_gpts)

find_grid_inds(inds: numpy.ndarray) → numpy.ndarray[source]

Finds indices that are on grid

Parameters:
inds: numpy.ndarray

The grid point index candidates, shape: (n_inds, n_dims)

Returns:

sel_grid – Subset selection of on-grid indices, shape: (n_inds, n_dims)

find_gridpoints(pts: numpy.ndarray, allow_outer: bool = True, ret_inds: False = False) → numpy.ndarray[source]
find_gridpoints(pts: numpy.ndarray, allow_outer: bool, ret_inds: True) → tuple[numpy.ndarray, numpy.ndarray]
find_gridpoints(pts: numpy.ndarray, allow_outer: bool, ret_inds: bool) → numpy.ndarray | tuple[numpy.ndarray, numpy.ndarray]

Finds points that are on grid.

Parameters:
pts: numpy.ndarray

The points, shape: (n_pts, n_dims)

allow_outer: bool = True
allow_outer: bool

Allow outermost point indices, else reduce those to lower-left cell corner

ret_inds: False = False
ret_inds: True
ret_inds: bool

Additionally return indices

Returns:

  • sel_grid – Subset selection of points that are on grid, shape: (n_pts, n_dims)

  • inds – The grid point indices, shape: (n_gpts, n_dims)

find_ingrid(pts: numpy.ndarray) → numpy.ndarray[source]

Finds points that are on grid.

Parameters:
pts: numpy.ndarray

The points, shape: (n_pts, n_dims)

Returns:

sel_grid – Subset selection of points that are in grid, shape: (n_pts, n_dims)

get_cell(p: numpy.ndarray) → numpy.ndarray[source]

Get the grid cell that contains a point.

Parameters:
p: numpy.ndarray

The point, shape: (n_dims,)

Returns:

cell – The min and max values of each dimension. Shape: (n_dims, 2)

get_cells(pts: numpy.ndarray) → numpy.ndarray[source]

Get the grid cells that contain the given points, one cell per point.

Parameters:
pts: numpy.ndarray

The points, shape: (n_pts, n_dims)

Returns:

cells – The min and max values of each dimension. Shape: (n_pts, n_dims, 2)

get_corner(p: numpy.ndarray, allow_outer: bool = True) → numpy.ndarray[source]

Get the lower-left grid corner of a point.

Parameters:
p: numpy.ndarray

The point, shape: (n_dims,)

allow_outer: bool = True

Allow outermost point indices, else reduce those to lower-left cell corner

Returns:

p0 – The lower-left grid corner point, shape: (n_dims,)

get_corners(pts: numpy.ndarray, allow_outer: bool = True) → numpy.ndarray[source]

Get the lower-left grid corners of points.

Parameters:
pts: numpy.ndarray

The points space, shape: (n_pts, n_dims)

allow_outer: bool = True

Allow outermost point indices, else reduce those to lower-left cell corner

Returns:

p0 – The lower-left grid corner points, shape: (n_pts, n_dims)

gp2i(gp: numpy.ndarray, allow_outer: bool = True, error: True = True) → numpy.ndarray[source]
gp2i(gp: numpy.ndarray, allow_outer: bool, error: False) → numpy.ndarray | None
gp2i(gp: numpy.ndarray, allow_outer: bool, error: bool) → numpy.ndarray | None

Get grid index of a grid point

Parameters:
gp: numpy.ndarray

The point, shape: (n_dims,)

allow_outer: bool = True
allow_outer: bool

Allow outermost point indices, else reduce those to lower-left cell corner

error: True = True
error: False
error: bool

Flag for throwing error if off-grid, else return None in that case

Returns:

inds – The lower-left grid corner point indices, shape: (n_dims,)

gpts2inds(gpts: numpy.ndarray, allow_outer: bool = True, error: True = True) → numpy.ndarray[source]
gpts2inds(gpts: numpy.ndarray, allow_outer: bool, error: False) → numpy.ndarray | None
gpts2inds(gpts: numpy.ndarray, allow_outer: bool, error: bool) → numpy.ndarray | None

Get grid indices of grid points.

Parameters:
gpts: numpy.ndarray

The grid points, shape: (n_gpts, n_dims)

allow_outer: bool = True
allow_outer: bool

Allow outermost point indices, else reduce those to lower-left cell corner

error: True = True
error: False
error: bool

Flag for throwing error if off-grid, else return None in that case

Returns:

inds – The lower-left grid corner indices, shape: (n_gpts, n_dims)

grad_coeffs(pts: numpy.ndarray, vars: collections.abc.Sequence[int] | numpy.ndarray | None, order: int | collections.abc.Sequence[int] | numpy.ndarray = 2, orderb: int | collections.abc.Sequence[int] | numpy.ndarray = 1) → tuple[numpy.ndarray, numpy.ndarray][source]

Calculates the gradient coefficients at grid points.

Parameters:
pts: numpy.ndarray

The evaluation points, shape: (n_pts, n_dims)

vars: collections.abc.Sequence[int] | numpy.ndarray | None

The dimensions representing the variables wrt which to differentiate, shape: (n_vars,). Default is all dimensions

order: int | collections.abc.Sequence[int] | numpy.ndarray = 2

The finite difference order, 1 = forward, -1 = backward, 2 = centre

orderb: int | collections.abc.Sequence[int] | numpy.ndarray = 1

The finite difference order at boundary points

Returns:

  • gpts – The grid points relevant for coeffs, shape: (n_gpts, n_dims)

  • coeffs – The gradient coefficients, shape: (n_pts, n_vars, n_gpts)

grad_coeffs_gridpoints(inds: numpy.ndarray, vars: collections.abc.Sequence[int] | numpy.ndarray | None, order: int | collections.abc.Sequence[int] | numpy.ndarray = 2, orderb: int | collections.abc.Sequence[int] | numpy.ndarray = 1) → tuple[numpy.ndarray, numpy.ndarray][source]

Calculates the gradient coefficients at grid points.

Parameters:
inds: numpy.ndarray

The integer grid point indices, shape: (n_inds, n_dims)

vars: collections.abc.Sequence[int] | numpy.ndarray | None

The dimensions representing the variables wrt which to differentiate, shape: (n_vars,). Default is all dimensions

order: int | collections.abc.Sequence[int] | numpy.ndarray = 2

The finite difference order, 1 = forward, -1 = backward, 2 = centre

orderb: int | collections.abc.Sequence[int] | numpy.ndarray = 1

The finite difference order at boundary points

Returns:

  • gpts – The grid points relevant for coeffs, shape: (n_gpts, n_dims)

  • coeffs – The gradient coefficients, shape: (n_inds, n_vars, n_gpts)

i2gp(inds: numpy.ndarray, error: True = True) → numpy.ndarray[source]
i2gp(inds: numpy.ndarray, error: False) → numpy.ndarray | None
i2gp(inds: numpy.ndarray, error: bool) → numpy.ndarray | None

Translates grid point indices to grid point.

Parameters:
inds: numpy.ndarray

The grid point indices, shape: (n_dims,)

error: True = True
error: False
error: bool

Flag for throwing error if off-grid, else return None in that case

Returns:

gp – The grid point, shape: (n_dims,)

in_grid(p: numpy.ndarray) → bool[source]

Checks if a point is located within the grid.

Parameters:
p: numpy.ndarray

The point, shape: (n_dims,)

Returns:

in_grid – True if within grid

inds2gpts(inds: numpy.ndarray) → numpy.ndarray[source]

Translates grid point indices to grid points.

Parameters:
inds: numpy.ndarray

The integer grid point indices, shape: (n_gpts, dims)

Returns:

gpts – The grid points, shape: (n_gpts, n_dims)

interpolation_coeffs_point(p: numpy.ndarray) → tuple[numpy.ndarray, numpy.ndarray][source]

Get the interpolation coefficients for a point.

Examples

>>> import numpy as np
>>> from iwopy.utils import RegularDiscretizationGrid
>>> grid = RegularDiscretizationGrid(
...     [0.0], [1.0], [2], interpolation="linear"
... )
>>> points, coefficients = grid.interpolation_coeffs_point(
...     np.array([0.5])
... )
>>> points.tolist()
[[0.0], [1.0]]
>>> coefficients.tolist()
[0.5, 0.5]
Parameters:
p: numpy.ndarray

The point, shape: (n_dims,)

Returns:

  • gpts – The grid points relevant for coeffs, shape: (n_gpts, n_dims)

  • coeffs – The interpolation coefficients, shape: (n_gpts,)

interpolation_coeffs_points(pts: numpy.ndarray, ret_pmap: False = False) → tuple[numpy.ndarray, numpy.ndarray][source]
interpolation_coeffs_points(pts: numpy.ndarray, ret_pmap: True) → tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]
interpolation_coeffs_points(pts: numpy.ndarray, ret_pmap: bool) → tuple[numpy.ndarray, numpy.ndarray] | tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]

Get the interpolation coefficients for a set of points.

Examples

>>> import numpy as np
>>> from iwopy.utils import RegularDiscretizationGrid
>>> grid = RegularDiscretizationGrid(
...     [0.0], [1.0], [2], interpolation="linear"
... )
>>> points, coefficients, point_map = grid.interpolation_coeffs_points(
...     np.array([[0.5], [1.5]]), ret_pmap=True
... )
>>> points.tolist()
[[0.0], [1.0], [2.0]]
>>> coefficients.tolist()
[[0.5, 0.5, 0.0], [0.0, 0.5, 0.5]]
>>> point_map.tolist()
[[0, 1], [1, 2]]
Parameters:
pts: numpy.ndarray

The points, shape: (n_pts, n_dims)

ret_pmap: False = False
ret_pmap: True
ret_pmap: bool

Additionally return the map from pts to gpts

Returns:

  • gpts – The grid points relevant for coeffs, shape: (n_gpts, n_dims)

  • coeffs – The interpolation coefficients, shape: (n_pts, n_gpts)

  • pmap – The map from pts to gpts, shape: (n_pts, n_gp)

is_gridi(inds: numpy.ndarray) → bool[source]

Checks if grid indices are valid

Parameters:
inds: numpy.ndarray

The grid point indices, shape: (n_dims,)

Returns:

on_grid – True if on grid

is_gridpoint(p: numpy.ndarray, allow_outer: bool = True, ret_inds: False = False) → bool[source]
is_gridpoint(p: numpy.ndarray, allow_outer: bool, ret_inds: True) → tuple[bool, numpy.ndarray]
is_gridpoint(p: numpy.ndarray, allow_outer: bool, ret_inds: bool) → bool | tuple[bool, numpy.ndarray]

Checks if a point is on grid.

Parameters:
p: numpy.ndarray

The point, shape: (n_dims,)

allow_outer: bool = True
allow_outer: bool

Allow outermost point indices, else reduce those to lower-left cell corner

ret_inds: False = False
ret_inds: True
ret_inds: bool

Additionally return indices

Returns:

  • on_grid – True if on grid

  • inds – The grid point indices, shape: (n_dims,)

print_info(spaces: int = 0) → None[source]

Prints basic information

Parameters:
spaces: int = 0

The prepending spaces

INT_INF : ClassVar[int] = -999999
deltas
digits = 12
interpolation = None
property n_dims : int

The number of dimensions

Returns:

n_dims – The number of dimensions

property n_points : numpy.ndarray

The number of points in each dimension

Returns:

n_points – The number of points in each dimension, shape: (n_dims,)

n_steps
origin
property p_max : numpy.ndarray

The maximal grid point values

Returns:

p_max – The maximal grid point values, shape: (n_dims,)

property p_min : numpy.ndarray

The minimal grid point values

Returns:

p_min – The minimal grid point values, shape: (n_dims,)

tol : numpy.ndarray | None