create ndarray out of c++ pointer

138 views Asked by At

I created a module in c++ and need to use the results in python.

Already wrote a wrapper and it is working with this code

a = np.empty([r, hn])
    for i in xrange(r):
        for j in xrange(hn):
            a[i,j]=self.thisptr.H[i*hn+j]
return a

The code is working, but I think there should be an easier and faster way to handle the pointer data. Sadly I am not used to python and cython and can't figure it out myself.

Any help would be appreciated. :)

2

There are 2 answers

1
DavidW On BEST ANSWER

Typed memoryviews (http://docs.cython.org/src/userguide/memoryviews.html) are your friend here.

a = np.empty([r,hn])
# interpret the array as a typed memoryview of shape (r, hn)
# and copy into a
# I've assumed the array is of type double* for the sake of answering the question
a[...] = <double[:r,:hn]>self.thisptr.H

It well may not be a huge amount faster (internally it's a loop pretty similar to what your wrote), but it is easier.

Alternatively, even simpler, just using the example from the documentation (http://docs.cython.org/src/userguide/memoryviews.html#coercion-to-numpy)

a = np.asarray(<double[:r,:hn]>self.thisptr.H)
0
Joachim Wagner On

A possible approach is to manually write the wrapper in C. The struct of your Python object can contain a pointer to the C++ object. Looking at my code (I did this is 2005), I see that I tested for NULL in C functions that need the C++ object and created it on the fly.

Nice side effect is that you don't have to expose all C++ methods 1:1 to Python and you can adjust the interface to make it more Pythonic. In my wrapper, I stored some additional information in the struct to be able to emulate Python list behaviour and to make loading data into the C++ object more efficient.