[Python] Using numpy.array in cython

Discussão em 'Python' iniciado por Stack, Junho 5, 2025.

  1. Stack

    Stack Membro Participativo

    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


    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?

    Continue reading...

Compartilhe esta Página