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]
Your options are:
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.
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 append
ing 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)
If you're set on using Python list
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With