而在笨拙的数组中循环等价的函数/代码片段



我有一个函数,我想转换它,以便可以与笨拙的数组1一起使用。

函数following曾经适用于float,但由于已知的原因,它不适用于笨拙的数组。

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;

我试着把它转换成numpy/笨拙的兼容形式,新功能看起来像

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;

这个函数一直被困在while循环中,我找不到调试它的方法

函数的任务是将值保持在+-kPI之间的尴尬数组中,但这种逻辑并不能给出所需的结果。

例如

x=ak.Array([[0.7999999999999998, 1.0, -1.3], [], [-1.4], [-1.8000000000000003, -6.1000000000000005, -1.6000000000000005], [-4.6]])

然而((x<-kPI((,这给出了期望的输出。

>>> ak.to_list(x <= -kPI)
[[False, False, False], [], [False], [False, True, False], [True]]

但不是功能

根据while循环的逻辑,所需的输出应该是b/w+-kPI,有什么直接的或建议可以使用吗?

好的,明白了。你想把x中的每个标量值(都是角度(调整到-π和π之间。

你可以这样做:

def Phi_mpi_pi(x):
y = numpy.add(x, numpy.pi)
y = numpy.mod(y, 2*numpy.pi)
y = numpy.subtract(y, numpy.pi)
return y

或者,更简洁、可读性差得多:

def Phi_mpi_pi(x):
return numpy.subtract(numpy.mod(numpy.add(x, numpy.pi), 2*numpy.pi), numpy.pi)

它的作用是:

  1. 将π加到所有角度上(使它们指向相反的方向(
  2. 取所有角度上的模2π,使它们都是从0到2π(不包括2π(
  3. 再次从所有角度中减去π(因此它们再次指向正确的方向(。现在它们都是从-π到+π(不包括+π(

测试:

x = ak.Array([[0.3, 3.1, numpy.pi, -numpy.pi, -4 * numpy.pi,
200 * numpy.pi, 2 * numpy.pi, -400 * numpy.pi], 
[], [-1.4], [-1.8, -6, -1.6], [-4.6]])
y = Phi_mpi_pi(x)
print("Type of result:", type(y))
print("Result =", y)
# Check that the resulting range of each value is correct.
range_is_correct = (ak.all(y >= -numpy.pi) and ak.all(y < numpy.pi))
# Calculate the factors of the 2π adjustments.
factors = (x - y) / (2 * numpy.pi)
print("2π factors=", factors)
# Test that all factors of the 2π adjustmenst are approximate integers.
adjustments_are_correct = ak.all(numpy.abs(numpy.mod(factors, 1)) < 1e-15)
# Test that all values are correct, given the test input.
print("Result is correct:", range_is_correct and adjustments_are_correct)

给出以下输出:

Type of result: <class 'awkward1.highlevel.Array'>
Result = [[0.3, 3.1, -3.14, -3.14, 0, 1.78e-14, 0, 4.62e-14, ... [-1.8, 0.283, -1.6], [1.68]]
2π factors= [[2.65e-17, 7.07e-17, 1, 0, -2, 100, 1, -200], [], [0], [0, -1, 0], [-1]]
Result is correct: True

这证明了利用特定使用的测试数据正确地执行了操作。

那是你想要的吗?

我不太明白你想要什么,但这至少可以帮助你调试:

import numpy
import awkward1 as ak
def Phi_mpi_pi(x):
kPI=numpy.array(3.14159265)
kPI = kPI.repeat(len(x))
kTWOPI = 2 * kPI
print("kPI =", kPI)
print("kTWOPI =", kTWOPI)
while ((x >= kPI)):
print(x)
x = x - kTWOPI
while ((x < -kPI)):
print(x)
x = x + kTWOPI
return x
x=ak.Array([[0.7999999999999998, 1.0, -1.3], [], [-1.4], [-1.8000000000000003, -6.1000000000000005, -1.6000000000000005], [-4.6]])
print(Phi_mpi_pi(x))

评论太长了,所以我把它作为一个答案发布,尽管它当然不能直接代表一个解决方案。

最新更新