Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cython with array of pointers

I have a list of numpy.ndarrays (with different length) in python and need to have very fast access to those in python. I think an array of pointers would do the trick. I tried:

float_type_t* list_of_arrays[no_of_arrays]
for data_array in python_list_of_arrays:
    list_of_arrays[0] = data_array

But cython complains:

no_of_arrays < Not allowed in a constant expression

I have tried several ways to constify this variable:

cdef extern from *:
    ctypedef int const_int "const int"

(there have been more creative attempts) - however it unfortunatley doesn't work.

Please help.

like image 604
Wolfgang Kerzendorf Avatar asked Dec 21 '12 02:12

Wolfgang Kerzendorf


1 Answers

Why don't you use a numpy object array instead of a list of arrays?

I think the problem you're having is because you are declaring list_of_arrays in the stack, and its size must be known at compile-time. You can try some dynamic allocation, like this:

from libc.stdlib cimport malloc, free

cdef float_type_t *list_of_arrays = \
    <float_type_t *>malloc(no_of_arrays * sizeof(float_type_t*))

for i in range(no_of_arrays):
    list_of_arrays[i] = &(data_array[i].data)

# don't forget to free list_of_arrays!

(This assumes data_array is a numpy array.)

But this is still guessing a bit what you want to do.

like image 147
tiago Avatar answered Oct 09 '22 00:10

tiago