2016-08-19 7 views
-1

は、端末では例7-11 高性能のPythonのCythonとnumpyの

cython_np.pyx

#cython_np.pyx 
import numpy as np 
cimport numpy as np 
def calculate_z(int maxiter, double complex[:] zs, double complex[:] cs): 
    cdef unsigned int i, n 
    cdef double complex z, c 
    cdef int[:] output = np.empty(len(zs), dtype = np.int32) 
    for i in range(len(zs)): 
     n = 0 
     z = zs[i] 
     c = cs[i] 
     while n < maxiter and (z.real * z.real + z.imag * z.imag) < 4: 
      z = z * z + c 
      n += 1 
     output[i] = n 
    return output 

setup.py

from distutils.core import setup 
from distutils.extension import Extension 
from Cython.Distutils import build_ext 

setup(
     cmdclass = {'build_ext':build_ext}, 
     ext_modules = [Extension("calculate", ["cythonfn.pyx"])] 
) 

を実行してみます、ubuntu 16.04

python3 setup.py build_ext --inplace 

私はIpythonに利用機能calculate.calculate.zを実行しようとすると、いくつかの警告

running build_ext 
cythoning cythonfn.pyx to cythonfn.c 
building 'calculate' extension 
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.5m -c cythonfn.c -o build/temp.linux-x86_64-3.5/cythonfn.o 
In file included from /usr/include/python3.5m/numpy/ndarraytypes.h:1777:0, 
       from /usr/include/python3.5m/numpy/ndarrayobject.h:18, 
       from /usr/include/python3.5m/numpy/arrayobject.h:4, 
       from cythonfn.c:274: 
/usr/include/python3.5m/numpy/npy_1_7_deprecated_api.h:15:2: warning:   #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp] 
#warning "Using deprecated NumPy API, disable it by " \ 
^
In file included from /usr/include/python3.5m/numpy/ndarrayobject.h:27:0, 
       from /usr/include/python3.5m/numpy/arrayobject.h:4, 
       from cythonfn.c:274: 
/usr/include/python3.5m/numpy/__multiarray_api.h:1448:1: warning: ‘_import_array’ defined but not used [-Wunused-function] 
_import_array(void) 
^ 
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/cythonfn.o -o MY_DIR/calculate.cpython-35m-x86_64-linux-gnu.so 

を取得し、それが

TypeError: a bytes-like object is required, not 'list' 

detail of using calculate.z

についての任意のアイデアを語ります警告? http://docs.cython.org/en/latest/src/userguide/memoryviews.html

double complex[:] zs 

の例に基づいて

答えて

0

私が思うには、double complex DTYPEと1D memoryviewを定義します。これは、そのdtypeを持つnumpy配列に似ています。

あなたのコードは、この関数にリストを渡します。リストはPythonオブジェクトであり、CまたはCythonネイティブ配列としてアクセスすることはできません。

この機能の呼び出し方法に関する情報はありますか?私はその本にアクセスできない。

+0

本書では、関数の呼び出し方法については説明していません。 ** numpy.array()**オブジェクトを私が呼び出すときにリストオブジェクトではなく私の関数に渡します。それはついに復活、ありがとう!!! – zpoint

関連する問題