from cython.parallel import prange
import numpy as np
cimport numpy as np
def calculate_z(int maxiter, double complex[:] zs,double complex[:] cs):
"""
Calculate output list using Julia update rule
"""
cdef unsigned int i, length
cdef double complex z, c
cdef int[:] output = np.empty(len(zs),dtype=np.int32)
length = len(zs)
with nogil:
for i in prange(length, schedule="guided"):
z = zs[i]
c = cs[i]
output[i] = 0
while (z.real * z.real + z.imag * z.imag) < 4 and output[i] < maxiter:
z = z * z + c
output[i] += 1
return output
此时用python setup.py build_ext --inplace编译时报错如下:
解决方法:
添加 include_dirs=[np.get_include()]
from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext import numpy as np setup( cmdclass={'build_ext': build_ext}, ext_modules=[Extension('calculate', ['cythonfnS.pyx'])], include_dirs=[np.get_include()])