我用从高斯分布中提取的值填充两个数组field_in_k_space_REALfield_in_k_space_IMAGINARY,当我对数组进行逆变换时,请注意尊重对称性以获得真实的场。这是代码:

field_in_k_space_REAL = zeros(n, float)
field_in_k_space_IMAGINARY = zeros(n, float)

field_in_k_space_REAL[0] = 0.0

for i in range(1, int(n/2+1)):
    field_in_k_space_REAL[i] = np.random.normal(mu, math.sqrt((1/2)*math.exp(-(2*math.pi*i*sigma/L)*(2*math.pi*i*sigma/L))))

x = range(int(n/2+1), int(n))
y = range(1, int(n/2))
zipped = zip(x, y)

for j, j2 in zipped:
    field_in_k_space_REAL[j] = field_in_k_space_REAL[j-2*j2]

field_in_k_space_IMAGINARY[0] = 0.0

for i in range(1, int(n/2)):
    field_in_k_space_IMAGINARY[i] = np.random.normal(mu, math.sqrt((1/2)*math.exp(-(2*math.pi*i*sigma/L)*(2*math.pi*i*sigma/L))))

field_in_k_space_IMAGINARY[n/2] = 0.0

for j, j2 in zipped:
    field_in_k_space_IMAGINARY[j] = - field_in_k_space_IMAGINARY[j-2*j2]

print 'field_k', field_in_k_space_REAL

但是我仍然遇到以下错误:
 field_in_k_space_REAL[0] = 0.0
IndexError: index 0 is out of bounds for axis 0 with size 0

有人可以解释为什么以及如何解决它吗?

最佳答案

我的猜测是,field_in_k_space_REAL数组的长度实际上为0,这很可能是因为您在代码中进一步设置了n = 0(您是否可能在循环中使用n?)。当我直接初始化长度为0的数组时,可以重现该错误。

关于python - 索引0超出尺寸0的轴0的范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29214017/

10-11 18:13