numpy array C api
Posted
by wiso
on Stack Overflow
See other posts from Stack Overflow
or by wiso
Published on 2010-05-27T20:53:25Z
Indexed on
2010/05/27
21:21 UTC
Read the original article
Hit count: 261
I have a C++ function returning a std::vector and I want to use it in python, so I'm using the C numpy api:
static PyObject *
py_integrate(PyObject *self, PyObject *args){
...
std::vector<double> integral;
cpp_function(integral); // this change integral
npy_intp size = {integral.size()};
PyObject *out = PyArray_SimpleNewFromData(1, &size, NPY_DOUBLE, &(integral[0]));
return out;
}
when I call it from python, if I do
import matplotlib.pyplot as plt
a = py_integrate(parameters)
print a
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(a)
print a
the first print is ok, the values are correct, but when I plot a
they are not, and in particular in the second print I see very strange values like 1E-308 1E-308 ...
or 0 0 0 ...
as an unitialized memory. I don't understand why the first print is ok.
© Stack Overflow or respective owner