[Numpy-discussion] NumPy Feature Request: Function to wrap angles to range [ 0, 2*pi] or [ -pi, pi ]
Daniele Nicolodi
daniele at grinta.net
Tue Nov 24 03:12:08 EST 2020
On 24/11/2020 02:49, Nathaniel Smith wrote:
> How would this proposed function compare to using the modulo operator,
> like 'arr % (2*pi)'?
I wrote almost the same word bu word reply, before realizing that taking
the modulo looses the sign. The correct operation is slightly more
complex (untested):
def wrap(alpha):
return (alpha + np.pi) % 2.0 * np.pi - np.pi
However, I don't think there is much value in adding something so
trivial as a function to numpy: I cannot think to any commonly used
algorithm that requires wrapping the phase, and it is going to be an
infinite source of bikesheeding whether the wrapped range should be
[-pi, pi) or (-pi, pi] or (0, 2*pi] or [0, 2*pi)
Cheers,
Dan
> On Mon, Nov 23, 2020, 16:13 Thomas <thomasbbrunner at gmail.com
> <mailto:thomasbbrunner at gmail.com>> wrote:
>
> Hi,
>
> I have a proposal for a feature and I hope this is the right place
> to post this.
>
> The idea is to have a function to map any input angle to the range
> of [ 0, 2*pi ] or [ - pi, pi ].
>
> There already is a function called 'unwrap' that does the opposite,
> so I'd suggest calling this function 'wrap'.
>
> Example usage:
> # wrap to range [ 0, 2*pi ]
> >>> np.wrap([ -2*pi, -pi, 0, 4*pi ])
> [0, pi, 0, 2*pi]
>
> There is some ambiguity regarding what the solution should be for
> the extremes. An example would be an input of 4*pi, as both 0 and
> 2*pi would be valid mappings.
>
> There has been interest for this topic in the community
> (see https://stackoverflow.com/questions/15927755/opposite-of-numpy-unwrap
> <https://stackoverflow.com/questions/15927755/opposite-of-numpy-unwrap>).
>
> Similar functions exist for Matlab
> (see https://de.mathworks.com/help/map/ref/wrapto2pi.html
> <https://de.mathworks.com/help/map/ref/wrapto2pi.html>). They solved
> the ambiguity by mapping "positive multiples of 2*pi map to 2*pi and
> negative multiples of 2*pi map to 0." for the 0 to 2*pi case.
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org <mailto:NumPy-Discussion at python.org>
> https://mail.python.org/mailman/listinfo/numpy-discussion
> <https://mail.python.org/mailman/listinfo/numpy-discussion>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list