Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to define a list in Cython

I want to transform the below python code in Cython:

x_array = []

x_array.append(x_new)

I tried the following Cython codes but it gives error:

cdef np.ndarray[double, dim=1] x_array

x_array.append(x_new)

The error shows:

Cannot coerce list to type [double, dim=1]

like image 456
Michael Fan Zhang Avatar asked May 04 '18 09:05

Michael Fan Zhang


1 Answers

Your options are:

  1. cdef list x_array. This lets Cython know that the type of x_array is actually a list. You may get a small speed-up from this.

  2. Make x_array a numpy array instead. If all the elements in the list are the same simple, numeric type then this is probably a better option. Be aware that appending to numpy arrays is likely to be pretty slow, so you should calculate the size in advance.

    cdef np.array[double, dim=1] x_array = np.zeros((some_precomputed_size,))
    # or
    cdef double[:] x_array = np.zeros((some_precomputed_size,))
    

    Note that this will only give you a speed-up for some types of operations (mostly accessing individual elements in Cython)

  3. If you're set on using Python lists you can sometimes get a speed-up by accessing them through the Python C API in Cython. This answer provides an example of where that worked well. This works best when you know a size in advance and so you can pre-allocate the array (i.e. don't append!) and also avoid some Cython reference counting. It's very easy to go wrong and make reference counting errors with this method, so proceed carefully.

like image 115
DavidW Avatar answered Sep 30 '22 15:09

DavidW