Source code for sequince.utils.bounds
from __future__ import annotations
from typing import Any
from ..array_api import namespace
from ..typing import Array
[docs]
def in_bounds(particles: Array, bounds: Array, xp: Any | None = None) -> Array:
"""Check if particles are within the specified bounds.
Bounds are assumed to be rectilinear, with shape (2, n_dimensions).
Parameters
----------
particles
Array of shape (n_particles, n_dimensions) representing the particles.
bounds
Array of shape (2, n_dimensions) where bounds[0] are the lower bounds
and bounds[1] are the upper bounds for each dimension.
Returns
-------
Array
Boolean array of shape (n_particles,) indicating whether each particle
is within bounds.
"""
if xp is None:
xp = namespace(particles, bounds)
if bounds.shape[0] != 2:
raise ValueError("Bounds array must have shape (2, n_dimensions).")
if bounds.shape[1] != particles.shape[-1]:
raise ValueError(
"Bounds array must have the same number of dimensions as particles."
)
return xp.all((particles >= bounds[0]) & (particles <= bounds[1]), axis=-1)