2014-06-19 2 views
1

에 베셀 함수의 결과를 넣을 수 없습니다 :이 게시물에 다음 NumPy와의 배열

How to put complex into a numpy's array?

나를 위해 잘 작동 보인다.

하지만이 경우 왜이 오류가 발생 했습니까? 추가

1 #!/usr/bin/env python 
    2 import scipy.special 
    3 import numpy as np 
    4 
    5 deg = 10 
    6 nodes = np.polynomial.legendre.leggauss(deg)[0] 
    7 A = np.zeros((deg, deg), dtype=complex) 
    8 for i in range(0, deg): 
    9  for 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 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 to float 

는 : 나는 루프 둥지에서 라인 (10) 및 인쇄 scipy.special.sph_yn(0, nodes[i]*nodes[j])[0]을 언급하면 ​​내가

[-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] 
... and so on 
+0

'scipy.special.sph_yn (0, nodes [i] * nodes [j]) [0]'을 A에 할당하지 않고 실행하여 먼저 작동하는지 확인하십시오. 그런 다음'scipy.special.sph_yn (0, nodes [i] * nodes [j]) [0]'의 결과를 확인하십시오. – brechmos

답변

2

special.sph_yn(0, nodes[i]*nodes[j])[0] 1 개 요소를 포함하는 NumPy와 배열을 반환 sph_yn에서 무엇을 얻었 는가. 이 배열 안의 값을 배열 자체가 아닌 A에 할당하려고합니다.

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

참고 지능형리스트를 사용하는 것이 : 당신이 메모리가있는 경우

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

도 (작동, 그리고 것을 배열에서 하나의 값을 얻으려면, item() 방법을 사용)는 NumPy 배열에 한 번에 하나의 요소를 할당하는 것보다 빠릅니다.