Skip to content

Commit

Permalink
fill in more of the numpy array ufunc interface
Browse files Browse the repository at this point in the history
  • Loading branch information
arahlin committed Oct 22, 2024
1 parent 8d97406 commit be84953
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions core/python/quatextensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@

__all__ = []

quat_types = (Quat, G3VectorQuat, G3TimestreamQuat)


def quat_ufunc(self, ufunc, method, *inputs, **kwargs):
"""Numpy ufunc interface for vectorized quaternion methods."""
if ufunc.__name__ in ["isinf", "isnan", "isfinite"] and len(inputs) == 1:
v = getattr(ufunc, method)(np.asarray(inputs[0]), **kwargs)
if ufunc.__name__ == "isfinite":
return np.all(v, axis=-1)
return np.any(v, axis=-1)
if ufunc.__name__.startswith("logical"):
args = []
for arg in inputs:
if isinstance(arg, quat_types):
arg = np.any(np.asarray(arg), axis=-1)
args.append(arg)
return getattr(ufunc, method)(*args, **kwargs)
if method != "__call__" or kwargs:
return NotImplemented
if len(inputs) == 1:
Expand Down

0 comments on commit be84953

Please sign in to comment.