I now have python code to create a list of ndarrays, and these arrays are not equal length. The piece of code snippet that looks like this:
import numpy as np
from mymodule import list_size, array_length # list_size and array_length are two lists of ints, and the len(array_length) == list_size
ndarray_list = []
for i in range(list_size):
ndarray_list.append(np.zeros(array_length[i]))
Now, I need to convert this to Cython, but do not know how. I tried to create a 2-d dynamically allocated array, like this:
import numpy as np
cimport numpy as np
from mymodule import list_size, array_length
cdef int i
ndarray_list = <double **>malloc(list_size * sizeof(double*))
for i in range(list_size):
ndarray_list[i] = <double *>malloc(array_length[i] * sizeof(double))
However, this method only creates a double pointer in ndarray_list[i]. I cannot pass it to other functions which requires some of the ndarray method.
What should I do?
malloc()is orders of magnitudes faster, so you should consider themalloc()-based answer...