I have 1 dimensional float array (from C space) which I want to read inside python space with zero copy. So far what I have done (reading SO mostly) is:
// wrap c++ array as numpy array
//From Max http://stackoverflow.com/questions/10701514/how-to-return-numpy-array-from-boostpython
boost::python::object exposeNDarray(float * result, long size) {
npy_intp shape[1] = { size }; // array size
PyObject* obj = PyArray_SimpleNewFromData(1, shape, NPY_FLOAT, result);
/*PyObject* obj = PyArray_New(&PyArray_Type, 1, shape, NPY_FLOAT, // data type
NULL, result, // data pointer
0, NPY_ARRAY_CARRAY_RO, // NPY_ARRAY_CARRAY_RO for readonly
NULL);*/
handle<> array( obj );
return object(array);
}
The PyArray_New
commented part is equivalent in functionality to the PyArray_SimpleNewFromData
one.
My problem is that this 1 dimensional array should actually be a 3 dimensional ndarray. I can control how my result
float array is constructed and I want if possible for that continuous block of memory to be interpreted as 3 Dimensional array.
I think this can be done by specifying the shape
variable but, I can't find any reference to how the memory is going to interpreted.
Say i need my array to look like: np.empty((x,y,z))
. When i specify that in the shape
variable, what section of my result
array would make up the first dimension, what section the second and so on?