简体   繁体   English

Cython 和 numpy

[英]Cython and numpy

Try to run Example 7-11 of High Performance Python尝试运行高性能 Python示例 7-11

cython_np.pyx 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"])]
)

In the terminal , ubuntu 16.04在终端,ubuntu 16.04

python3 setup.py build_ext --inplace

get some warning得到一些警告

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

when I try to run use function calculate.calculate.z in Ipython, it says当我尝试在 Ipython 中运行 use function calculate.calculate.z时,它说

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

detail of using calculate.z使用calculate.z的细节

Any idea about the warning?关于警告的任何想法?

I think, based on examples inhttp://docs.cython.org/en/latest/src/userguide/memoryviews.html我认为,基于http://docs.cython.org/en/latest/src/userguide/memoryviews.html 中的示例

double complex[:] zs

defines a 1d memoryview with double complex dtype.定义一个具有double complex dtype 的一维内存视图。 This is similar to a numpy array with that dtype.这类似于具有该 dtype 的numpy数组。

Your code passes a list to this function.您的代码将列表传递给此函数。 Lists are Python objects, and can't be accessed as C or Cython native arrays.列表是 Python 对象,不能作为 C 或 Cython 原生数组访问。

Does your source give any information on how to call this function?您的消息来源是否提供有关如何调用此函数的任何信息? I don't have access to that book.我无权访问那本书。

The same issue happens in the 2nd edition.同样的问题发生在第 2 版中。 While np.asarray() could solve the problem, it may somewhat(based on my own experiment) hurt the code performance.虽然np.asarray()可以解决问题,但它可能会(基于我自己的实验)在某种程度上损害代码性能。 The reason could be that we need to allocate new space for the newly created array).原因可能是我们需要为新创建的数组分配新空间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM