unyt icon indicating copy to clipboard operation
unyt copied to clipboard

Inconsistent behaviour between np.arange and np.linspace

Open dangom opened this issue 6 years ago • 7 comments

  • 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

dangom avatar Aug 15 '18 21:08 dangom

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.

ngoldbaum avatar Aug 15 '18 21:08 ngoldbaum

Reference: https://github.com/numpy/numpy/issues/12028 and https://www.numpy.org/neps/nep-0018-array-function-protocol.html

astrojuanlu avatar Oct 24 '18 08:10 astrojuanlu

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?

shoyer avatar Oct 25 '18 15:10 shoyer

Yet another case where arrays-with-units are special: we want scalars with units to work seamlessly too :)

ngoldbaum avatar Oct 25 '18 15:10 ngoldbaum

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

saulshanabrook avatar Apr 03 '19 16:04 saulshanabrook

I'm pretty sure that's a question for the numpy issue tracker or mailing list, not for the unyt bug tracker :)

ngoldbaum avatar Apr 03 '19 16:04 ngoldbaum

@ngoldbaum Sorry! I was clicking around on issues and didn't realize I had switched repos :)

saulshanabrook avatar Apr 03 '19 16:04 saulshanabrook