Array Operations¶
fdtdx.core.misc.expand_matrix(matrix, grid_points_per_voxel, add_channels=True)
¶
Expands a matrix by repeating values along spatial dimensions and optionally adding channels.
Used to upsample a coarse grid to a finer simulation grid by repeating values. Can also add vector field components as channels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
matrix
|
Array
|
Input matrix to expand |
required |
grid_points_per_voxel
|
tuple[int, ...]
|
Number of grid points to expand each voxel into along each dimension |
required |
add_channels
|
bool
|
If True, adds and tiles 3 channels for vector field components |
True
|
Returns:
Type | Description |
---|---|
Array
|
jax.Array: Expanded matrix with repeated values and optional channels |
Source code in src/fdtdx/core/misc.py
fdtdx.core.misc.ensure_slice_tuple(t)
¶
Ensures that all elements of the input sequence are converted to slices.
This function takes a sequence of elements that can be slices, integers, or tuples of integers and returns a tuple of slices. Integers are converted to slices that select a single item, and tuples are converted to slices that select a range of items.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t
|
Sequence[slice | int | Tuple[int, int]]
|
A sequence of elements where each element is either a slice, an integer, or a tuple of two integers representing the start and end of a slice range. |
required |
Returns:
Type | Description |
---|---|
Tuple[slice, ...]
|
A tuple of slices corresponding to the input sequence. |
Source code in src/fdtdx/core/misc.py
fdtdx.core.misc.index_1d_array(arr, val)
¶
Finds the first index where a 1D array equals a given value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr
|
Array
|
1D input array to search |
required |
val
|
Array
|
Value to find in the array |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
Array
|
Index of first occurrence of val in arr |
Raises:
Type | Description |
---|---|
Exception
|
If input array is not 1D |
Source code in src/fdtdx/core/misc.py
fdtdx.core.misc.index_by_slice(arr, start, stop, axis, step=1)
¶
Indexes an array along a specified axis using slice notation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr
|
Array
|
Input array to slice |
required |
start
|
int | None
|
Starting index |
required |
stop
|
int | None
|
Stopping index |
required |
axis
|
int
|
Axis along which to slice |
required |
step
|
int
|
Step size between elements |
1
|
Returns:
Type | Description |
---|---|
Array
|
jax.Array: Sliced array |
Source code in src/fdtdx/core/misc.py
fdtdx.core.misc.index_by_slice_take_1d(arr, slice, axis)
¶
Takes elements from an array along one axis using a slice and JAX's take operation.
Optimized version of array slicing that uses JAX's take operation for better performance when taking elements along a single axis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr
|
Array
|
Input array |
required |
slice
|
slice
|
Slice object specifying which elements to take |
required |
axis
|
int
|
Axis along which to take elements |
required |
Returns:
Type | Description |
---|---|
Array
|
jax.Array: Array with selected elements |
Raises:
Type | Description |
---|---|
Exception
|
If slice would result in empty array |
Source code in src/fdtdx/core/misc.py
fdtdx.core.misc.index_by_slice_take(arr, slices)
¶
Takes elements from an array using multiple slices and JAX's take operation.
Optimized version of array slicing that uses JAX's take operation for better performance when taking elements along multiple axes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr
|
Array
|
Input array |
required |
slices
|
Sequence[slice]
|
Sequence of slice objects, one for each dimension |
required |
Returns:
Type | Description |
---|---|
Array
|
jax.Array: Array with selected elements |
Raises:
Type | Description |
---|---|
Exception
|
If any slice would result in empty array |
Source code in src/fdtdx/core/misc.py
fdtdx.core.misc.mask_1d_from_slice(s, axis_size)
¶
Creates a boolean mask array from a slice specification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
slice
|
Slice object defining which elements should be True |
required |
axis_size
|
int
|
Size of the axis to create mask for |
required |
Returns:
Type | Description |
---|---|
Array
|
jax.Array: Boolean mask array with True values where slice selects elements |
Source code in src/fdtdx/core/misc.py
fdtdx.core.misc.assimilate_shape(arr, ref_arr, ref_axes, repeat_single_dims=False)
¶
Reshapes array to match reference array's dimensions for broadcasting.
Inserts new dimensions of size 1 such that arr has same dimensions as ref_arr and can be broadcasted. Optionally repeats single dimensions to match ref_arr's shape.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr
|
Array
|
Array to reshape |
required |
ref_arr
|
Array
|
Reference array whose shape to match |
required |
ref_axes
|
tuple[int, ...]
|
Tuple mapping arr's axes to ref_arr's axes |
required |
repeat_single_dims
|
bool
|
If True, repeats size-1 dimensions to match ref_arr |
False
|
Returns:
Type | Description |
---|---|
Array
|
jax.Array: Reshaped array that can be broadcasted with ref_arr |
Raises:
Type | Description |
---|---|
Exception
|
If shapes are incompatible or axes mapping is invalid |
Source code in src/fdtdx/core/misc.py
fdtdx.core.misc.linear_interpolated_indexing(point, arr)
¶
Performs linear interpolation at a point in an array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
point
|
Array
|
Coordinates at which to interpolate |
required |
arr
|
Array
|
Array to interpolate from |
required |
Returns:
Type | Description |
---|---|
Array
|
jax.Array: Interpolated value at the specified point |
Raises:
Type | Description |
---|---|
Exception
|
If point dimensions don't match array dimensions |
Source code in src/fdtdx/core/misc.py
fdtdx.core.misc.advanced_padding(arr, padding_cfg)
¶
Pads the input array with configurable padding modes and widths.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr
|
Array
|
Input array to pad |
required |
padding_cfg
|
PaddingConfig
|
Configuration object containing: - widths: Padding widths for each edge - modes: Padding modes (constant, edge, reflect etc) - values: Values to use for constant padding |
required |
Returns:
Type | Description |
---|---|
tuple[Array, tuple[slice, ...]]
|
tuple[jax.Array, tuple[slice, ...]]: Tuple containing: - Padded array - Slice tuple to extract original array region |