The Array class facilitates the generalized Discrete Fourier transform
and all operations on its values while retaining coordinate grid
information.
Parameters:
values (Any) – The values array with respect to the dimensions.
dims (Tuple[Dimension, ...]) – Dimensions defining the coordinates of the values.
spaces (Tuple[Space, ...]) – Space of the given values per Dimension.
eager (Tuple[bool, ...]) – Whether the factors should be applied to the internal values after
performing a space transition. This mainly influences the behavior of
into_space. It also acts as a tie breaker on addition and subtraction
when factors_applied=False and factors_applied=True as a result
would cost equally many operations.
factors_applied (Tuple[bool, ...]) – Whether the lazy factors have been applied to the given values.
xp (Any) – The array API namespace.
Examples
First, we initialize the dimensions using fa.dim_from_constraints:
Select specific values along respective dimension names by
label/coordinate.
Inspired by xarray.DataArray.sel
Parameters:
indexers (Optional[Mapping[str, Union[float, slice]]], optional) – Mappping from Dimension name to the coordinate/label which can be a
float or a slice, by default None
missing_dims (Literal['raise', 'warn', 'ignore']) – Behavior, if selected Dimensions are not part of the Array, by
default ‘raise’
Using xp to make a conversion between array libraries relies on non-standardized behavior.
Therefore this function only supports conversions between libraries where the interaction
is specifically allowed. It currently supports NumPy, JAX, PyTorch and array-api-strict
and is not optimal for performance because it always goes the safe route via NumPy.
Parameters:
space (Union[Space, Iterable[Space]]) – The space in which the values should be returned. Can be specified
per dimension.
xp (Any | None) – The array API namespace to use for the returned values. If it is
None, the array namespace from self is used.
dtype (Any | None) – The dtype to use for the returned values. If it is None, the dtype
of self is used.
This class encapsulates all the properties of the position and frequency
coordinate grids for one dimension.
Note that properties associated with the position grid are denoted by pos,
whereas the frequency grid properties are denoted with freq.
Frequencies are rotational frequencies in cycles as opposed to angular frequencies.
dynamically_traced_coords (bool) – Only relevant for use with JAX tracing. Whether the coordinate values
should be dynamically traced such that the grid can be altered inside a
jitted function. See also Working with JAX.
Notes
Implementation details
The grid in both spaces (position and frequency) goes from min to max
including both points. Therefore d_pos=(pos_max-pos_min)/(n-1).
In the case of even n, pos_middle is the sample on the right hand side of the exact center of
the grid.
For odd n the definitions for pos_middle and freq_middle change
to ensure that they and the minimum and maximum position and frequency are
actually sampled and not in between two samples.:
For the Dimension, retrieve the index corresponding to a given
coordinate in a specified space.
Parameters:
coord (Union[float, slice]) – The coordinate or range of coordinates for which to find the index.
If a slice is provided, the function will handle it accordingly.
space (Space) – The space in which the coordinate is defined. It can be either “pos” or “freq”.
method (Optional[Literal["nearest", "pad", "ffill", "backfill", "bfill"]], optional) – The method to use for finding the index when the exact coordinate is not found.
Supported methods are:
- “nearest”: Find the index representing the nearest coordinate.
- “pad” or “ffill”: Forward fill to the next smallest index.
- “backfill” or “bfill”: Backward fill to the next highest index.
Default is None, which requires an exact match.
Returns:
The index or range of indices corresponding to the given coordinate(s).
If a float coord is provided, the function returns an integer index.
If a slice object is provided, the function returns a slice object.
Returns the Dimension values for the respective space.
Parameters:
self (Dimension) – Dimension providing the parameters.
space (Space) – The space for which the values are returned.
xp (Optional[Any], optional) – The Array API namespace to use for the returned values. If it is
None, the default namespace from get_default_xp() is used.
dtype (Optional[Any], optional) – The dtype to use for the returned values. If it is None, the
default real floating point dtype of the determined xp is used.
Construct a new instance of Array from raw values.
Parameters:
values – The values to initialize the Array with.
They can be of any Python Arrray API v2023.12 compatible library.
By default they are copied to make sure an external alias cannot influence the created Array.
dim (Union[Dimension, Iterable[Dimension]]) – The Dimension(s) for each dimension of the passed in values.
space (Union[Space, Iterable[Space]]) – Specify the space(s) of the values with which the returned Array intialized.
xp (Optional[Any]) – The Array API namespace to use for the created Array.
If it is None, array_api_compat.array_namespace(values) is used.
If that fails the default namespace from get_default_xp() is used.
dtype (Optional[Any]) – Directly passed on to the xp.asarray of the determined xp.
If None the dtype of values or the defaults for the passed in scalar of the underlying
array library are used.
defensive_copy (bool) – If True the values array is always copied in order to ensure no external alias to it exists.
This ensures the immutability of the created Array.
If this is unnecessary, this defensive copy can be prevented by setting this argument to False.
In this case it has to be ensured that the passed in array is not used externally after creation.
Constructs an array filled with the coordinates of the specified dimension
while keeping all other attributes (Array API namespace, eager) of the
specified array.
Parameters:
x (Array) – The array from which to construct the coordinate array.
space (Space) – Specify the space of the returned Array is intialized.
dim_name (str) – The name of the dimension from which to construct the coordinate array.
xp (Optional[Any]) – The array namespace to use for the returned Array. None uses the array namespace of x.
dtype (Optional[Any]) – The dtype to use for the created coordinate array.
None uses a real floating point type with the same precision as x.
The grid coordinates of the chosen space packed into an Array with the dimension of name dim_name.
eager of the created array is the same as eager in the selected dimension of x.
dynamically_traced_coords (bool, optional) – Only relevant for use with JAX tracing. Whether the coordinate values
should be dynamically traced such that the grid can be altered inside a
jitted function, by default False. See also Working with JAX.
Creates a Dimension from an arbitrary subset of all possible grid
parameters using the z3 constraint solver. Note that the specified grid
parameters must lead to a unique solution that fulfill the following
constraints:
If n is not directly specified an exact solution of this constraint system
leads in general to a n which is not a natural number.
In that case n is rounded up according to the rounding mode.
In order to do this some other constraint has to be improved.
The constraints which are allowed to change for rounding up are given in
loose_params. The value of d_pos, d_freq, pos_min and freq_min would
be made smaller while the value of pos_max, pos_extent, freq_max and
freq_extent would be made larger. pos_middle and freq_middle do not
influence n and are therefore not allowed as parameters in loose_prams.
n (int | Literal['power_of_two', 'even']) – Number of grid points, either a natural number or the rounding mode, by default “power_of_two”
d_pos (float | None) – Distance between two neighboring position grid points, by default None
d_freq (float | None) – Distance between two neighboring frequency grid points, by default None
pos_min (float | None) – Smallest position grid point, by default None
pos_max (float | None) – Largest position grid point, by default None
pos_middle (float | None) – Middle of the position grid, by default None
pos_extent (float | None) – Length of the position grid, by default None
freq_min (float | None) – Smallest frequency grid point, by default None
freq_max (float | None) – Largest frequency grid point, by default None
freq_extent (float | None) – Length of the frequency grid, by default None
freq_middle (float | None) – Middle of the frequency grid, by default None
loose_params (str | List[str] | None) – List of loose grid parameters (parameters that can be changed by the
constraint solver when rounding up n to be even or a power of two), by
default None
dynamically_traced_coords (bool) – Only relevant for use with JAX tracing. Whether the coordinate values
should be dynamically traced such that the grid can be altered inside
a jitted function, for more details see Working with JAX,
by default False
Returns:
Dimension initialized using the constraints solved via the z3
constraint solver.
dim (Union[Dimension, Iterable[Dimension]]) – The dimension(s) of the created array. They also imply the shape.
space (Union[Space, Iterable[Space]]) – Specify the space(s) in which the returned Array is intialized.
xp (Any | None) – The Array API namespace to use for the created Array.
If it is None, array_api_compat.array_namespace(fill_value) is used.
If that fails the default namespace from get_default_xp() is used.
dtype (Optional[Any]) – The dtype to use for the returned Array.
If the value is None, the dtype is inferred from fill_value
according to the rules of the underlying Array API.
Computes the integral along the specified Dimension(s). The integration
is performed using simple rectangle rule integration, also known as Riemann
summation [1]. The integration is equivalent to summing up the values along
the specified Dimension(s) and multipling them with the integration element
given by d_pos or d_freq of the respective Dimension. The
integration for each Dimension is performed in the space of the input Array.
The actual summation is forwarded to [2].
dim_name (Optional[Union[str, Iterable[str]]], optional) – Dimension name(s) along which the integration is performed. The default,
dim_name=None, will reduce over all Dimensions.
dtype (Any, optional) – The dtype of the returned Array, by default None.
Returns:
New Array with the specified dimensions reduced by computing the integration of the elements along those dimensions.
Computes the maximum of all values along the specified Dimension(s).
This is a thin wrapper around [3], refer to its documentation and
the documentation of the used array library for detailed
information about its semantics.
dim_name (Optional[Union[str, Iterable[str]]], optional) – Dimension name(s) along which the maximum is computed. The default,
dim_name=None, will reduce over all Dimensions.
Returns:
New Array with the specified dimensions reduced by computing the maximum of the elements along those dimensions.
Computes the mean of all values along the specified Dimension(s).
This is a thin wrapper around [4], refer to its documentation and
the documentation of the used array library for detailed
information about its semantics.
dim_name (Optional[Union[str, Iterable[str]]], optional) – Dimension name(s) along which the mean is computed. The default,
dim_name=None, will reduce over all Dimensions.
Returns:
New Array with the specified dimensions reduced by computing the mean of the elements along those dimensions.
Computes the minimum of all values along the specified Dimension(s).
This is a thin wrapper around [5], refer to its documentation and
the documentation of the used array library for detailed
information about its semantics.
dim_name (Optional[Union[str, Iterable[str]]], optional) – Dimension name(s) along which the minimum is computed. The default,
dim_name=None, will reduce over all Dimensions.
Returns:
New Array with the specified dimensions reduced by computing the minimum of the elements along those dimensions.
Computes the product of all values over the specified Dimension(s).
This is a thin wrapper around [6], refer to its documentation and
the documentation of the used array library for detailed
information about its semantics.
dim_name (Optional[Union[str, Iterable[str]]], optional) – Dimension name(s) along which the products are computed. The default,
dim_name=None, will reduce over all Dimensions.
dtype (Any, optional) – The dtype of the returned Array, by default None.
Returns:
New Array with the specified dimensions reduced by computing the product of the elements along those dimensions.
Set the default value for the eager argument of array creation functions.
If this is set within a context manager, it gets reset by the context manager
on leaving to the value it had before the context manager was entered.
Set the default value for the xp argument of array creation functions.
If this is set within a context manager, it gets reset by the context manager
on leaving to the value it had before the context manager was entered.
Parameters:
xp – Array API namespace to set as default.
It gets automatically wrapped with array-api-compat
if necessary.
Computes the sum of all values over the specified Dimension(s).
This is a thin wrapper around [7], refer to its documentation and
the documentation of the used array library for detailed
information about its semantics.
dim_name (Optional[Union[str, Iterable[str]]], optional) – Dimension name(s) over which the sum is performed. The default,
dim_name=None, will reduce over all Dimensions.
dtype (Any, optional) – The dtype of the returned Array, by default None.
Returns:
New Array with the specified dimensions reduced by computing the sum of the elements along those dimensions.