I've searching a efficient and elegant way to do this. I'll hope the example will explain my concern.
We've got an np.array like this:
omega = np.array([1.03415121504, 1.29595060284, 1.55774999064, 1.81954937844,
...
2.08134876623, 2.37359445321, -2.11179506541, -1.84999567761])
And now I want to manipulate it, like
omega[omega < 0.0] = omega + 2 * np.pi
omega[omega >= 2 * np.pi] = omega - 2 * np.pi
The second statement may overwrite the computed values of the fist statement, and then there's an intersection. I found np.piecewise, but this doesn't provides such an behavior.
How can I achieve this (efficient)?
The corrent behavior is like that (but very unefficient/inelegant):
tmp = []
for o in omega:
if o < 0.0:
tmp.append(o + 2 * np.pi)
elif o >= (2 * np.pi):
tmp.append(o - 2 * np.pi)
else:
tmp.append(o)
omega = np.array(tmp)
Therefore someone made experiences with numpy's nditer for such purposes? (Especially about performance / efficency)
omega[omega < 0.0] = omega + 2 * np.pidoes something rather strange? You probably meanomega[omega < 0.0] += 2 * np.pi. (The first form adds2 * np.pito all elements ofomegaand then uses the first entry in this new array as the value for the first negative entry ofomega-- the values get shifted in a strange way.)