You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Does not specify the behavior if stop or start are out of range for the dtype
Says "If dtype is None, the output array data type must be the default floating-point data type." I think the default for arange should be int if all the arguments are integers.
"The length of the output array must be ceil((stop-start)/step)" should be caveated (stop and step provided, stop >= start for step > 0 and stop <= start for step < 0)
eye
May be worth explicitly saying elements with index i, j should be 1 if j - i = k and 0 otherwise.
Says "If dtype is None, the output array data type must be the default floating-point data type." I think the default should be a corresponding dtype to the input value (we don't have a notion of a "default" integer dtype).
linspace
It's a bit ambiguous whether it actually says this right now, but I think the stop value should not be required to be included (when endpoint=True). Consider
Thanks @asmeurer, good points. I agree with everything except the linspace point, that seems like a bug in numpy due to having a dtype keyword but not having a separate integer implementation for it.
Re: full_like. The dtype is inferred from the provided array, not the fill value.
Re: linspace. This is a bug in NumPy. If we are not going to support endpoint inclusion because of precision issues, then the endpoint keyword should be dropped altogether, as would not be possible to support.
Re: arange. The reason for floating-point as the default is that, even if the inputs are integers, we cannot guarantee that the computed increment will allow for evenly spaced numbers without precision loss. In this case, floating-point is the safest choice.
Re: other points. Seem reasonable. Will submit a follow-up PR.
Re: linspace. This is a bug in NumPy. If we are not going to support endpoint inclusion because of precision issues, then the endpoint keyword should be dropped altogether, as would not be possible to support.
Just to be clear, the spec doesn't explicitly say the endpoint should be exactly included. So if that is desired, we should probably state that.
Re: arange. The reason for floating-point as the default is that, even if the inputs are integers, we cannot guarantee that the computed increment will allow for evenly spaced numbers without precision loss. In this case, floating-point is the safest choice.
I'm not sure I follow here. I mean the case where start, stop, and step are integers. There should be no precision issues there because all the values will be exact integers between start and stop (assume start and stop are within the range of the dtype, which I mentioned as another point). I agree if any of them, including step is a float, then the result should be floating point. Note that converting to float could actually lose precision because many integer dtypes have values that are not exactly representable as a float:
Why allow shape arguments to be passed as keyword arguments, too? This is a pretty unambiguous name, and code can be clearer when it uses names.
Likewise, could num be allowed as a keyword for linespace and fill_value be allowed as a keyword for full/full_like?
empty and empty_like may not necessarily make sense for libraries that do not support mutation (e.g., JAX, Dask, TensorFlow) It might be worth calling this out.
empty and empty_like may not necessarily make sense for libraries that do not support mutation (e.g., JAX, Dask, TensorFlow) It might be worth calling this out.
Could these libraries simply alias empty to zeros?
Also, a common use of empty is to create size 0 arrays, like empty((0, 1)).
empty and empty_like may not necessarily make sense for libraries that do not support mutation (e.g., JAX, Dask, TensorFlow) It might be worth calling this out.
Could these libraries simply alias empty to zeros?
Fair enough -- in fact this is exactly what JAX already does.
The spec is maybe a little unclear, but I read it as saying the start should always be included. But the NumPy implementation clearly involves divisions which round back to integers, making this not guaranteed. It's not clear to me if this should be considered an implementation bug, of these sorts of subtleties in the implementation should be expected and hence should not be required. I opened numpy/numpy#18881 upstream about this.
Activity
rgommers commentedon Jan 9, 2021
Thanks @asmeurer, good points. I agree with everything except the
linspacepoint, that seems like a bug in numpy due to having adtypekeyword but not having a separate integer implementation for it.kgryte commentedon Jan 11, 2021
Re:
full_like. The dtype is inferred from the provided array, not the fill value.Re:
linspace. This is a bug in NumPy. If we are not going to support endpoint inclusion because of precision issues, then theendpointkeyword should be dropped altogether, as would not be possible to support.Re:
arange. The reason for floating-point as the default is that, even if the inputs are integers, we cannot guarantee that the computed increment will allow for evenly spaced numbers without precision loss. In this case, floating-point is the safest choice.Re: other points. Seem reasonable. Will submit a follow-up PR.
asmeurer commentedon Jan 12, 2021
Just to be clear, the spec doesn't explicitly say the endpoint should be exactly included. So if that is desired, we should probably state that.
I'm not sure I follow here. I mean the case where
start,stop, andstepare integers. There should be no precision issues there because all the values will be exact integers betweenstartandstop(assume start and stop are within the range of the dtype, which I mentioned as another point). I agree if any of them, includingstepis a float, then the result should be floating point. Note that converting to float could actually lose precision because many integer dtypes have values that are not exactly representable as a float:shoyer commentedon Jan 30, 2021
A few other suggestions:
shapearguments to be passed as keyword arguments, too? This is a pretty unambiguous name, and code can be clearer when it uses names.numbe allowed as a keyword forlinespaceandfill_valuebe allowed as a keyword forfull/full_like?emptyandempty_likemay not necessarily make sense for libraries that do not support mutation (e.g., JAX, Dask, TensorFlow) It might be worth calling this out.asmeurer commentedon Feb 1, 2021
Could these libraries simply alias
emptytozeros?Also, a common use of
emptyis to create size 0 arrays, likeempty((0, 1)).shoyer commentedon Feb 1, 2021
Fair enough -- in fact this is exactly what JAX already does.
Update specification for arange
Update the specification for `full` and `full_like`
Update specification for `linspace`
Update specification for `empty`, `ones`, `zeros`
Update specification for arange
Update the specification for `full` and `full_like`
Update specification for `linspace`
Update specification for `empty`, `ones`, `zeros`
Update API specification for creation and manipulation functions (#167)
asmeurer commentedon Apr 30, 2021
Turns out the linspace issue isn't just about the endpoint:
I think this is related to this NumPy issue numpy/numpy#16813.
asmeurer commentedon Apr 30, 2021
The spec is maybe a little unclear, but I read it as saying the
startshould always be included. But the NumPy implementation clearly involves divisions which round back to integers, making this not guaranteed. It's not clear to me if this should be considered an implementation bug, of these sorts of subtleties in the implementation should be expected and hence should not be required. I opened numpy/numpy#18881 upstream about this.rgommers commentedon May 1, 2021
That looks like a clear bug, not even a subtle one. The integer start point and step size calculation should just not use floats.