ndcube icon indicating copy to clipboard operation
ndcube copied to clipboard

NDCube does not support arithmetic via numpy ufuncs

Open Cadair opened this issue 2 years ago • 1 comments

Describe the bug

Numpy ufuncs are things like np.add and np.equal, currently we support arithmetic methods via the standard Python operands like + (through __add__) but not the equivalent ufunc.

I discovered this issue because the following doesn't work:

cube = NDCube(...)
arr = np.array([cube], dtype=object)
np.equal(arr, cube)

In addition some things like np.maximum and np.isinf might be quite nice to have working without having to access .data, also if we implement __array_ufunc__ we can have those things return quantities.


See Recommendations for implementing binary operations in the NEP for how I think we need to go about this.

The overview is:

  • Implement all the arithmetic methods in __array_ufunc__.
  • Have the Python operator methods __add__ etc, call the numpy ufunc, i.e. __add__ calls np.add.
  • Ensure that our operator methods return NotImplemented if the other operand has __array_ufunc__ = None.

Cadair avatar Feb 14 '23 17:02 Cadair

@Cadair suggests we need to think about this more before we consider adding it.

nabobalis avatar Jun 19 '25 10:06 nabobalis