How to convert python array to cython array?

后端 未结 5 843
失恋的感觉
失恋的感觉 2020-12-17 22:14

I have an array of float values that is created in regular Python, that I want to pass to a cython function that fronts for an underlying C function. The C function requires

5条回答
  •  悲&欢浪女
    2020-12-17 22:29

    I believe you can do this by iterating over the python list of floats and putting them in a C array.

    cimport cython
    from libc.stdlib cimport malloc, free
    
    cdef:
        float * cfloats
        int i
    
    cfloats =  malloc(len(pyfloats)*cython.sizeof(float))
    if cfloats is NULL:
      raise MemoryError()
    for i in xrange(len(pyfloats)):
      cfloats[i] = pyfloats[i]
    setOverlays(cfloats)
    free(cfloats)
    

提交回复
热议问题