-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: np.clip
as currently implemented (numpy 2.0.0) is quite hard to use
#26759
Comments
Branching off from the other issue. In principle, if clip was a ufunc, doing such an implementation would be a bit tedious. But from the not ridiculously purist side of things, it is actually very easy
That is so easy, we could possibly even consider it a bug fix, but not sure it should be. |
I agree we could start by just doing this on the python side -- the C side would be good mostly as a way to generalize what we already do for comparisons, thus serving as an example on how to do things like this more generally. But really, no reason not to just do the simple thing... |
This is somewhat tied to the proposed change to remove type promotion #24976 (which is also necessary for array API compatibility, FYI), since not promoting Python scalars erroring is basically the same issue, since ideally you'd want I think @seberg mentioned that NumPy already has similar logic somewhere for comparing int64 and uint64. For example, this gives the right answer, where a naive cast would not: >>> np.asarray([-100], dtype=np.int64) < np.asarray([18446744073709551516 - 1], dtype=np.uint64)
array([ True])
>>> np.asarray([-100], dtype=np.int64).astype(np.uint64) < np.asarray([18446744073709551516 - 1], dtype=np.uint64)
array([False]) |
I just want to add a voice to this thread that
np.clip
as currently implemented (numpy 2.0.0) is quite hard to use:I think most users in this scenario would expect to be able to use arrays of any type and Python ints and just have it work — there is nothing unsatisfiable about this expression, including keeping the input dtype.
When you start dealing with numpy scalars and so on, the story is indeed more complicated as noted in the above discussion, but the Python int scenario seems like an easy fix (as noted by @mhvk above) that would unlock a lot of uses already.
Sorry about the noise, I expect there will be lots in this repo this coming week. 😅 Thank you all! 🙏
Originally posted by @jni in #24976 (comment)
The text was updated successfully, but these errors were encountered: