-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Add umath functions to wrap radian angles #2868
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
Conversation
@@ -913,6 +913,62 @@ def add_newdoc(place, name, doc): | |||
|
|||
""") | |||
|
|||
add_newdoc('numpy.core.umath', 'wrap_to_pi', | |||
""" | |||
Wrap angles to interval `[-PI, PI]`wrap_to_pi. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copy/paste error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Fixed.
|
The basic motivation behind these functions is the same as behind
But some more detailed description about the exact behavior has to be added for sure. As well as test cases, but please let me know what you think about it before I spend more time on this. |
That's a good point about I don't understand your point about You didn't answer the question about why you think both of these functions are needed. Though I guess if we have one, then we might want both, just because if we only have a single function named I guess I'm +0 on this idea given the increase in readability and the precedent set by |
@njsmith This is especially useful if you deal with some kind of polar or spherical coordinates. Here you don't want 360deg wrap to 0deg. |
Hmm, if what you're saying is that it is critical that you wrap to the closed interval [0, 2pi] rather than the half-open interval [0, 2pi), then this actually makes me more nervous about this code, not less. It seems very likely that there are users who will expect the half-open interval behaviour and find the closed-interval behaviour very confusing -- will we need to add another pair of functions later for them? And the actual behaviour you're worried about -- the handling of the floating point number which is exactly equal (in floating point) to 2pi -- is the sort of behaviour that you can't really handle correctly in floating point in the first place. It's entirely possible that where you expected 360 degrees your calculation instead produces 360.00000000000006 degrees (error of 1 unit-in-the-last-place) -- now what? Should that wrap around to 0? If the user was depending on the behaviour that you're talking about, then will their code break whenever this happens? This is more questions than I like to have about code that's proposed for merging into numpy, given that numpy is supposed to be the core set of operations that everyone relies on. |
Sorry for the late reply. I just had a look at what Matlab does and they seem to do the same as I implemented here. Nevertheless feel free to decide whether this is useful at all or if you wish any modified version of this. |
@type@ npy_wrap_to_pi@c@(@type@ x) | ||
{ | ||
if (x < -NPY_PI || x > NPY_PI) | ||
return npy_wrap_to_2pi(x + NPY_PI) - NPY_PI; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return remainder(x, NPY_PI)
is enough here, i think - although that needs C99
I think we should close this - both operations are easy to spell as Thanks for the suggestion anyway @ahojnnes! |
No description provided.