rapidity.core.field module

Multi-dimensional fields on product grids.

This module defines Field, which represents a physical quantity defined on a product of Grid1D grids. A field always carries its grids alongside its values, ensuring that the discretization structure is never lost.

Key operations provided:

class rapidity.core.field.Field(values, grids)

Bases: object

apply(f)

Apply a function elementwise to the field values.

Parameters:

f (callable) – A function to apply elementwise to the values. Must accept and return a numpy array.

Returns:

A new Field with the same grids and transformed values.

Return type:

Field

Examples

>>> field.apply(np.exp)
>>> field.apply(np.log)
>>> field.apply(lambda x: np.log(1 + np.exp(-x)))
convolve(kernel, dim=None)

Convolve field with a general kernel along a dimension.

Parameters:
  • kernel (Field) – A 2D field representing the kernel K(theta, theta’).

  • dim (str | None) – Dimension to convolve along.

Returns:

A new Field with the same grids as self, with convolution performed along the specified dimension.

Return type:

Field

Raises:

ValueError – If kernel is not a 2D field defined on the same grid in both dimensions.

derivative(dim=None, order=1)

Compute the derivative of the field along a dimension.

Uses second-order accurate finite differences via numpy.gradient.

Parameters:
  • dim (str | None) – Dimension to differentiate along. Can be omitted for 1D fields.

  • order (int) – Order of the derivative, either 1 or 2. Default is 1.

Returns:

A new Field with the same grids containing the derivative values.

Return type:

Field

Raises:

ValueError – If order is not 1 or 2.

classmethod from_function(f, grids)

Construct a Field by evaluating a function on a product grid.

The function is evaluated at all combinations of grid points, producing a multi-dimensional array of values. The shape of the resulting array matches the sizes of the grids in the order they are provided.

Parameters:
  • f (callable) – A function to evaluate on the grid points. Must be vectorized, i.e. able to accept numpy arrays as arguments and return a numpy array. For a single grid, f should accept a 1D array. For multiple grids, f should accept one array per grid, as produced by np.meshgrid with indexing=’ij’.

  • grids (list[Grid1D]) – The grids defining the domain. The order determines the axis ordering of the resulting values array.

Returns:

A Field with values obtained by evaluating f on the product grid.

Return type:

Field

Examples

>>> grid = Grid1D.gauss_legendre(-10, 10, 200, "theta")
>>> field = Field.from_function(lambda theta: np.exp(-theta ** 2), [grid])
>>> grid_x = Grid1D.uniform(0.0, 1.0, 10, "x")
>>> grid_t = Grid1D.uniform(0.0, 1.0, 10, "t")
>>> field = Field.from_function(lambda x, t: x + t, [grid_x, grid_t])
classmethod from_values(values, grids)

Construct a Field from an array of values and a list of grids.

The shape of values must be consistent with the grids — axis i must have the same size as grids[i].

Parameters:
  • values (ndarray) – Array of values. Shape must match the sizes of the grids.

  • grids (list[Grid1D]) – Grids defining the domain. The order determines the axis ordering of the values array.

Returns:

A Field with the given values on the given grids.

Return type:

Field

Raises:

ValueError – If the shape of values is inconsistent with the grids.

Examples

>>> grid = Grid1D.gauss_legendre(-5.0, 5.0, 50, "theta")
>>> values = np.exp(-grid.points**2)
>>> field = Field.from_values(values, [grid])
grids: list[Grid1D]
integrate(dim=None)

Integrate the field along a dimension using quadrature weights.

Parameters:

dim (str | None) – Dimension to integrate over. Can be omitted for 1D fields.

Returns:

A new Field with the specified dimension removed.

Return type:

Field

Raises:

ValueError – If dim is not specified for a multi-dimensional field, or if the requested dimension is not found.

interpolate(new_grid)

Interpolate the field onto a new grid.

Currently supported for 1D fields only.

Parameters:

new_grid (Grid1D) – The grid to interpolate onto.

Returns:

A new Field with values interpolated onto new_grid.

Return type:

Field

Raises:

NotImplementedError – If the field is not 1D.

classmethod load(path)

Load a field from an HDF5 file.

Parameters:

path (str) – Path to the HDF5 file to load.

Returns:

The field stored in the file.

Return type:

Field

save(path)

Save the field to an HDF5 file.

Parameters:

path (str) – Path to the HDF5 file to create.

Return type:

None

values: ndarray