Opposite of numpy.unwrap

numpy, python

Solution

phases = (phases + np.pi) % (2 * np.pi) - np.pi

Problem

In Python numpy, there is an unwrap function that: Unwrap radian phase p by changing absolute jumps greater than discont to their 2*pi complement along the given axis. Now, I'd like to do the opposite function. How can I wrap an array of phases? E.g. how to convert all angles to constraint them between -π and π? The obvious way would be to do something like: ``` for i, a in enumerate(phases): while a < pi: a += 2 * pi while a > pi: a -= 2 * pi phases[i] = a ``` but is there a simpler / faster way?

Original source