Reputation: 1919
In my simulation i compute multiple values for a phase, for example
phi = np.linspace(-N,N,1000)
where N can be large.
Is there an easy way to map the values to the intervall [0,2pi) ?
Upvotes: 3
Views: 20696
Reputation: 509
Another possible way:
First, convert to the [-pi, pi] interval using np.arctan2(np.sin(angle), np.cos(angle))
. Then, you still need to transform the negative values. A final function like this would work:
def convert_angle_to_0_2pi_interval(angle):
new_angle = np.arctan2(np.sin(angle), np.cos(angle))
if new_angle < 0:
new_angle = abs(new_angle) + 2 * (np.pi - abs(new_angle))
return new_angle
To confirm, run:
angles = [10, 200, 365, -10, -180]
print(np.rad2deg([convert_angle_to_0_2pi_interval(np.deg2rad(a)) for a in angles]))
...which prints: [ 10., 200., 5., 350., 180.]
Upvotes: 1
Reputation: 882
It sounds like you are looking for np.interp. Scipy offers an interpolate function too.
For a usage example, to map the values of phi (which are between -N and N) to [0, 2π] try
np.interp(phi, (-N, N), (0, 2*np.pi))
To exclude 2π you could either change upper bound so no value maps onto 2π.
np.interp(phi, (-N, N + 1), (0, 2*np.pi))
Or reduce the largest value you include in phi
phi = np.linspace(-N, N, 1000, endpoint=False)
I believe it would be easier to just ask for the values directly.
For example, 1000 points over the range [0, 2π] can be given by
np.linspace(0, 2*np.pi, 1000)
And for the range [0, 2π) which excludes the value 2π
np.linspace(0, 2*np.pi, 1000, endpoint=False)
Upvotes: 2
Reputation: 1256
Does that work ?
import numpy as np
import math
N=10
phi = np.linspace(-N,N,1000)
phi = phi%(2*math.pi)
print(phi)
Output
[2.56637061 2.58639063 ... 3.69679467 3.71681469]
Upvotes: 7