I would like to convert numpy array to some double* or stl vector on the c++ side. I'm actually using PyCXX for this and I can't figure out the way to access the data.
I'm currently able to access and return the data buffer like this :
Py::Object arrayShape(const Py::Tuple& args ){
Py::Object array= args[0];
return array.getAttr("data");
}
But I don't know what to do with it. My final goal, is to obtain a gsl_vector out of it. Ideally, I wouldn't have to recopy the memory. But maybe it is too much to ask ;)
When I am searching for a solution and I can find only others posting the same, long unanswered question, I post the solution once I discover it. Yours is such a question.
Firstly, strongly consider using Cython as your glue and proceed no further down this dangerous path.
That out of the way, using PyArray_FromAny will give you a view of the underlying data if possible and a copy otherwise. A very simple example (build with -std=c++11 if you are an honest and good person, or VS2013 if you are a Windows user):
Note the true argument as the second parameter to Py::Object constructors for "owned" objects! An example of a cpython3 extension that uses the Numpy C API in combination with PyCXX with cmake for building. The link is to a specific commit because I am considering switching this extension back to using Cython.