how can convert a 2d numpy array to an array of pointers in cython, without using memoryviews

Viewed 115

Using memoryviews for my 1d arrays seems to be significantly slower than using a raw pointer. Is there an easy way to convert a multidimensional numpy array (or memoryview) to an array of pointers?

This is what I do for 1d arrays:

cdef np.ndarray[dtype=double, ndim=1] a1
cdef double* a2 = &a1[0]

I need something for a 3d array:

cdef np.ndarray[dtype=double, ndim=3] b1
cdef double*** b2 = <something here>

Obviously I can flatten the 3d array and compute the necessary 1d indices.

I could also manually fill the double*** b2 arrays:

    b2 = <double ***> malloc(len(b1) * sizeof(double**))
    for i, a in enumerate(b1):
        b2[i] = <double **> malloc(len(a) * sizeof(double*))
        for j, b in enumerate(a):
            b2[i][j] = <double *> malloc(len(b) * sizeof(double))
            for k, c in enumerate(b):
                b2[i][j][k] = c

But is there a better built-in way to do that?

0 Answers
Related