Saturday, 15 June 2013

python - Can not put the result of Bessel function into numpy's array -



python - Can not put the result of Bessel function into numpy's array -

following post:

how set complex numpy's array?

it seems work me.

but why did error in case?

1 #!/usr/bin/env python 2 import scipy.special 3 import numpy np 4 5 deg = 10 6 nodes = np.polynomial.legendre.leggauss(deg)[0] 7 = np.zeros((deg, deg), dtype=complex) 8 in range(0, deg): 9 j in range(0, deg): 10 a[i, j] = scipy.special.sph_yn(0, nodes[i]*nodes[j])[0] machine:desktop users$ ./aa.py traceback (most recent phone call last): file "./aa.py", line 10, in <module> a[i, j] = scipy.special.sph_yn(0, nodes[i]*nodes[j])[0] typeerror: can't convert complex float

additional: did sph_yn if comment line 10 , print scipy.special.sph_yn(0, nodes[i]*nodes[j])[0] out in nest loop

[-0.61456112] [-0.79004531] [-1.19235662] [-2.16125343] [-6.82467416] [ 6.82467416+0.j] [ 2.16125343+0.j] [ 1.19235662+0.j] [ 0.79004531+0.j] [ 0.61456112+0.j] ... , on

special.sph_yn(0, nodes[i]*nodes[j])[0] returns numpy array containing 1 element. want assign value within array a, not array itself. single value out of array, utilize item() method:

a[i, j] = special.sph_yn(0, nodes[i]*nodes[j])[0].item()

note using list comprehension:

a = np.array([[special.sph_yn(0, nodes[i]*nodes[j])[0].item() j in range(deg)] in range(deg) ])

would work, , (if have memory) faster assigning values numpy array 1 element @ time.

python numpy scipy

No comments:

Post a Comment