tags:

views:

39

answers:

1

Hi, I would like to convert the numpy double array to numpy float array in C(Swig). I am trying to use

PyObject *object = PyArray_FROM_OT(input,NPY_FLOAT)

or

PyObject *object = PyArray_FROMANY(input,NPY_FLOAT,0,0,NPY_DEFAULT)

or

PyObject *object = PyArray_FromObject(input,NPY_FLOAT,0,0)

or

PyObject *object = PyArray_ContiguousFromAny(input,NPY_FLOAT,0,0)

But all of them return NULL? Am I missing anything?

+1  A: 

Your approach is correct, yet your assumption about they numpy C API is not. NPY_FLOAT is just an integral constant, yet the functions you posted require the type parameter to be *a poitner to a PyArray_Descr struct*.

In order to get a type description from a mere type, you can call PyArray_DescrFromType, so your call could look like this:

PyArrayObject* float_array = (PyArrayObject*)PyArray_FromAny(input,PyArray_DescrFromType(NPY_FLOAT64), 0,0, flags);

...with flags being whatever flags you deem meaningful when converting - please have a look at the numpy API, both for correct API invocation and for the meaning of different flags and values.

Jim Brissom