ndcube
ndcube copied to clipboard
NDCube does not support arithmetic via numpy ufuncs
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__callsnp.add. - Ensure that our operator methods return
NotImplementedif the other operand has__array_ufunc__ = None.
@Cadair suggests we need to think about this more before we consider adding it.