Say you have these angles:
>>> angles = np.linspace(-np.pi, np.pi, 10) >>> angles array([-3.14159265, -2.44346095, -1.74532925, -1.04719755, -0.34906585, 0.34906585, 1.04719755, 1.74532925, 2.44346095, 3.14159265])
There are two possible ways to do this ...
- Use
numpy expressions to search for negative values ββ...
Those that are less than zero must be converted to the correct value. Something like that:
>>> (2*np.pi + angles) * (angles < 0) + angles*(angles > 0) array([ 3.14159265, 3.83972435, 4.53785606, 5.23598776, 5.93411946, 0.34906585, 1.04719755, 1.74532925, 2.44346095, 3.14159265])
Remember that in numpy you can do boolean tests ... angles < 0 is a boolean array. However, 1*(angles < 0) is a numeric array where True values ββare mapped to 1 , and False values ββare mapped to 0 . You can combine two concepts to get an answer.
- You can simply admit that this is a mathematical expression that you are trying to solve:
Therefore, for this, just add 2*np.pi to everything and take the mod. This is exactly the same as finding "place units" or converting a number to an octal number, etc.
>>> (angles + 2 * np.pi) % (2 * np.pi) array([ 3.14159265, 3.83972435, 4.53785606, 5.23598776, 5.93411946, 0.34906585, 1.04719755, 1.74532925, 2.44346095, 3.14159265])
Hope this helps.