3

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
  • No answers yet? Can anyone tell me how to convert types using API in general? Commented Jul 19, 2010 at 10:15

1 Answer 1

5

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 pointer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.