I have a function which I would like to convert so that i can use with awkward array 1.
Function following which used to work for float but not for awkward arrays for the known reasons.
def Phi_mpi_pi(x):
kPI=(3.14159265)
kTWOPI = 2 * kPI
#while ((x.any() >= kPI).any()): x = x - kTWOPI;
#while ((x.any() < -kPI).any()): x = x + kTWOPI;
while ((x >= kPI)): x = x - kTWOPI;
while ((x < -kPI)): x = x + kTWOPI;
return x;
I tried to convert it into numpy/awkward compatible form and new function look like
def Phi_mpi_pi(x):
kPI=numpy.array(3.14159265)
kPI = kPI.repeat(len(x))
kTWOPI = 2 * kPI
while ((x >= kPI)): x = x - kTWOPI;
while ((x < -kPI)): x = x + kTWOPI;
return x;
This function remains stuck in the while loop forever, I couldn't find a way to debug it.
Task of the function is to keep the values in an awkward array between +- kPI but this logic does not give the desired results.
e.g.
x=ak.Array([[0.7999999999999998, 1.0, -1.3], [], [-1.4], [-1.8000000000000003, -6.1000000000000005, -1.6000000000000005], [-4.6]])
However ((x < -kPI)) this give desired output.
>>> ak.to_list(x <= -kPI)
[[False, False, False], [], [False], [False, True, False], [True]]
but not the function
the desired output should be b/w +- kPI based on the logic of while loop, is there something straightforward or suggestion which can be used?
Okay, got it. You want to adjust every single scalar value in x (which are all angles) to be between -π and π.
You can do it like this:
Or, more terse and much less readable:
What it does is this:
Test:
gives this output:
which proves that the operation was correctly performed with the specifically used test data.
Was that what you wanted?