在cython中使用numpy.array [英] Using numpy.array in cython

查看:759
本文介绍了在cython中使用numpy.array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以 cython 格式重写一个类,并将其另存为demo.pyx。该类的输入参数可以是形状为 Nx2 2D np.array ,例如 a = np.array([[0.2,-0.8],[3.7,0.02],..,[-0.92,-3.33]])或列表 instance a = [0.1,2.7]

I want to rewrite a class in cython format and save it as demo.pyx. The input parameter for the class would be either a 2D np.array with an Nx2 shape, e.g. a=np.array([[0.2,-0.8],[3.7,0.02],..,[-0.92,-3.33]]), or a list for instance a=[0.1,2.7].

cimport numpy as np
DTYPE = np.float64
ctypedef np.float64_t DTYPE_t
cdef class halo_positions(object):
    cdef unsigned double *x 
    cdef unsigned double *y 
    def __init__(self, np.ndarray[np.float64_t,ndim=2,mode='c'] positions):
        self.x = &positions[:,0]
        self.y = &positions[:,1]

我尝试编写的内容导致错误消息如下:

What I have tried to write causes error message as following:

running build_ext
cythoning demo.pyx to demo.c

Error compiling Cython file:
------------------------------------------------------------
...
cimport numpy as np
DTYPE = np.float64
ctypedef np.float64_t DTYPE_t
cdef class halo_positions(object):
    cdef unsigned double *x 
        ^
------------------------------------------------------------

demo.pyx:5:9: Unrecognised type modifier combination

Error compiling Cython file:
------------------------------------------------------------
...
cimport numpy as np
DTYPE = np.float64
ctypedef np.float64_t DTYPE_t
cdef class halo_positions(object):
    cdef unsigned double *x 
    cdef unsigned double *y 
        ^
------------------------------------------------------------

demo.pyx:6:9: Unrecognised type modifier combination

Error compiling Cython file:
------------------------------------------------------------
...
ctypedef np.float64_t DTYPE_t
cdef class halo_positions(object):
    cdef unsigned double *x 
    cdef unsigned double *y 
    def __init__(self, np.ndarray[np.float64_t,ndim=2,mode='c'] positions):
        self.x = &positions[:,0]
                ^
------------------------------------------------------------

demo.pyx:8:17: Cannot take address of Python variable

Error compiling Cython file:
------------------------------------------------------------
...
cdef class halo_positions(object):
    cdef unsigned double *x 
    cdef unsigned double *y 
    def __init__(self, np.ndarray[np.float64_t,ndim=2,mode='c'] positions):
        self.x = &positions[:,0]
        self.y = &positions[:,1]
                ^
------------------------------------------------------------

demo.pyx:9:17: Cannot take address of Python variable

我知道我使用指针的方式存在问题,但是如果我想保留 x y 不明确,我需要使用它。如何使我的工作?

I know there is a problem with the way I have used pointers, but if I want to keep the type of x and y ambiguous, I need to use this. How could I make my class work?

推荐答案

我从cython.group在Google中获得的答案非常有效:

An answer which I got from cython.group in google works perfectly:

import cython
cimport cython

import numpy as np
cimport numpy as np

DTYPE = np.float64
ctypedef np.float64_t DTYPE_t

cdef class halo_positions(object):

    cdef double [:] _x
    property x:
        def __get__(self):
            return np.array(self._x)
        def __set__(self, np.ndarray[DTYPE_t, ndim=1] x):
            self._x = x

    cdef double [:] _y
    property y:
        def __get__(self):
            return np.array(self._y)
        def __set__(self, np.ndarray[DTYPE_t, ndim=1] y):
            self._y = y

    def __init__(self, np.ndarray[DTYPE_t,ndim=2] positions):
        self._x = positions[:,0]
        self._y = positions[:,1]

    def debug(self):
        print self.x, self.y

这篇关于在cython中使用numpy.array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆