C-extension -- how do I accept a vector of both type double and type int?

Hi Everyone, I finally build a C extension. The one problem I found is that it is too picky about the input. For example, it accepts array([1.0,2.0,3.0]) with no problem, but when I pass in array([1,2,3]), since the dtype of the array is now int, my extension does not like it. How do I handle this situation? Is there any way to access any data type that can be converted into a double? Thanks, cg

Geoffrey Zhu wrote:
Hi Everyone,
I finally build a C extension. The one problem I found is that it is too picky about the input. For example, it accepts array([1.0,2.0,3.0]) with no problem, but when I pass in array([1,2,3]), since the dtype of the array is now int, my extension does not like it.
Okay. Show us the code that you are using, and we can help you find a better way.
How do I handle this situation? Is there any way to access any data type that can be converted into a double?
I usually use PyArray_FROM_OTF(). That handles the usual cases. It's pretty much like starting off a pure Python function with asarray(x, dtype=whatever). -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco

On 7/26/07, Robert Kern <robert.kern@gmail.com> wrote:
Geoffrey Zhu wrote:
Hi Everyone,
I finally build a C extension. The one problem I found is that it is too picky about the input. For example, it accepts array([1.0,2.0,3.0]) with no problem, but when I pass in array([1,2,3]), since the dtype of the array is now int, my extension does not like it.
Okay. Show us the code that you are using, and we can help you find a better way.
How do I handle this situation? Is there any way to access any data type that can be converted into a double?
I usually use PyArray_FROM_OTF(). That handles the usual cases. It's pretty much like starting off a pure Python function with asarray(x, dtype=whatever).
That is going to make a copy of the memory every time and might slow down things a lot?

Geoffrey Zhu wrote:
On 7/26/07, Robert Kern <robert.kern@gmail.com> wrote:
Geoffrey Zhu wrote:
Hi Everyone,
I finally build a C extension. The one problem I found is that it is too picky about the input. For example, it accepts array([1.0,2.0,3.0]) with no problem, but when I pass in array([1,2,3]), since the dtype of the array is now int, my extension does not like it. Okay. Show us the code that you are using, and we can help you find a better way.
How do I handle this situation? Is there any way to access any data type that can be converted into a double? I usually use PyArray_FROM_OTF(). That handles the usual cases. It's pretty much like starting off a pure Python function with asarray(x, dtype=whatever).
That is going to make a copy of the memory every time and might slow down things a lot?
Not if you pass it an array with the requested properties. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco

How do I handle this situation? Is there any way to access any data type that can be converted into a double? I usually use PyArray_FROM_OTF(). That handles the usual cases. It's pretty much like starting off a pure Python function with asarray(x, dtype=whatever).
That is going to make a copy of the memory every time and might slow down things a lot?
Not if you pass it an array with the requested properties.
Neat. Do you know if PyArray_FROM_OTF() increments the reference count of the returned object? The documentation does not say. My guess is yes.

Geoffrey Zhu wrote:
How do I handle this situation? Is there any way to access any data type that can be converted into a double? I usually use PyArray_FROM_OTF(). That handles the usual cases. It's pretty much like starting off a pure Python function with asarray(x, dtype=whatever).
That is going to make a copy of the memory every time and might slow down things a lot? Not if you pass it an array with the requested properties.
Neat. Do you know if PyArray_FROM_OTF() increments the reference count of the returned object? The documentation does not say. My guess is yes.
Yes. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco

On 7/26/07, Robert Kern <robert.kern@gmail.com> wrote:
Geoffrey Zhu wrote:
How do I handle this situation? Is there any way to access any data type that can be converted into a double? I usually use PyArray_FROM_OTF(). That handles the usual cases. It's pretty much like starting off a pure Python function with asarray(x, dtype=whatever).
That is going to make a copy of the memory every time and might slow down things a lot? Not if you pass it an array with the requested properties.
Neat. Do you know if PyArray_FROM_OTF() increments the reference count of the returned object? The documentation does not say. My guess is yes.
Yes.
Hi Robert, This is probably off the topic. Do you know such a function for regular python objects? For example, I know a PyObject is a number, but I don't know the exact type. Is there any quick way to convert it to a C double type? Thanks, cg

Geoffrey Zhu wrote:
This is probably off the topic. Do you know such a function for regular python objects? For example, I know a PyObject is a number, but I don't know the exact type. Is there any quick way to convert it to a C double type?
I just answered your question on the python-list. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (2)
-
Geoffrey Zhu
-
Robert Kern