Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

index 0 is out of bounds for axis 0 with size 0

Tags:

python

arrays

I am filling two arrays, field_in_k_space_REAL and field_in_k_space_IMAGINARY, with values extracted from a Gaussian distribution, paying attention to respect the symmetry to get a real field when I inverse-transform the arrays. Here is the code:

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

But I keep having the following error:

 field_in_k_space_REAL[0] = 0.0
IndexError: index 0 is out of bounds for axis 0 with size 0

Can someone explain why and how to fix it?

like image 685
johnhenry Avatar asked Mar 23 '15 15:03

johnhenry


1 Answers

My guess is that the array field_in_k_space_REAL is actually of length 0, most likely because you set n = 0 further up in your code (do you use n in a loop maybe?). I can reproduce the error when I directly initialize an array of length 0.

like image 142
James Kelleher Avatar answered Sep 23 '22 05:09

James Kelleher