unyt
unyt copied to clipboard
Inconsistent behaviour between np.arange and np.linspace
- unyt version: 1.0.6
- Python version: 3.6.6
- Operating System: MacOS 10.13.6
Description
When using np.arange
on quantities, I'd expect the result to be a unyt_array, just like when using np.linspace
. The result, however, is a standard numpy array.
What I Did
In [310]: type(np.linspace(5*s,6*s,100))
Out[310]: unyt.array.unyt_array
In [311]: type(np.arange(5*s,6*s,100*u.ms))
Out[311]: numpy.ndarray
I doubt there’s anything we can do about this without defining a wrapper around arange that behaves as you’re expecting. It may be the case that in the future we could override this with __array_function__
which is being actively discussed on NumPy’s github.
Reference: https://github.com/numpy/numpy/issues/12028 and https://www.numpy.org/neps/nep-0018-array-function-protocol.html
np.linspace
is documented as accepting scalars for start
/stop
rather than arrays/array-like. So following the rule that __array_function__
is only called on array-like arguments, it currently (on master) it is not slated for __array_function__
support. But perhaps this is worth revisiting?
Yet another case where arrays-with-units are special: we want scalars with units to work seamlessly too :)
I am not sure if this is the right place to bring this up, but it would great to be able to define custom dispatching for function like np.arange
or np.empty
.
I guess one option would be for to NumPy to dispatch on scalars as well as arrays. Is this incompatible with the NEP? Would anything break with this change?
EDIT: Moved here https://github.com/numpy/numpy/issues/12028#issuecomment-479568946
I'm pretty sure that's a question for the numpy issue tracker or mailing list, not for the unyt bug tracker :)
@ngoldbaum Sorry! I was clicking around on issues and didn't realize I had switched repos :)