rapidity.core package¶
Submodules¶
Module contents¶
Core data structures for the rapidity package.
This subpackage defines the two fundamental building blocks:
Both are re-exported at this level for convenience:
from rapidity.core import Grid1D, Field
- class rapidity.core.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:
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:
- 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:
- 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:
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:
- 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])
- 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:
- 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.
- 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:
- save(path)¶
Save the field to an HDF5 file.
- Parameters:
path (
str) – Path to the HDF5 file to create.- Return type:
None
- values: ndarray¶
- class rapidity.core.Grid1D(points, weights, label)¶
Bases:
object- classmethod gauss_hermite(n, label)¶
Construct a Gauss-Hermite quadrature grid on (-inf, inf).
Approximates integrals of rapidly decaying functions f(x) on the full real line, without assuming any explicit Gaussian factor in the integrand.
- Parameters:
n (
int) – Number of quadrature points.label (
str) – Dimension label.
- Returns:
A Gauss-Hermite quadrature grid.
- Return type:
- classmethod gauss_legendre(a, b, n, label)¶
Constructs a Gauss-Legendre quadrature grid on [a, b] of n points
- Parameters:
a (
float) – The grid boundaries.b (
float) – The grid boundaries.n (
int) – Number of quadrature points.label (
str) – Dimension label
- Returns:
A Gauss-Legendre quadrature grid.
- Return type:
- label: str¶
- points: ndarray¶
- classmethod uniform(a, b, n, label)¶
Constructs a uniform quadrature grid on[a, b] of n points
- Parameters:
a (
float) – The grid boundaries.b (
float) – The grid boundaries.n (
int) – Number of quadrature points.label (
str) – Dimension label
- Returns:
A uniform quadrature grid.
- Return type:
- weights: ndarray¶