[Numpy-discussion] Handle type convertion in C API

Eric Moore ewm at redtetrahedron.org
Tue Mar 10 13:13:09 EDT 2020

Hi Benoit,

Since you have a function that takes two scalars to one scalar, it sounds
to me as though you would be best off creating a ufunc.  This will then
handle the conversion to and looping over the arrays, etc for you.  The
documentation is available here:



On Tue, Mar 10, 2020 at 12:28 PM Benoit Gschwind <gschwind at gnu-log.net>

> Hello,
> I writing a python binding of one of our library. The binding intend to
> vectorize the function call. for exemple:
> double foo(double, double) will be bound to a python:
> <numpy.array of double> module.foo(<numpy.array>, <numpy.array>)
> and the function foo will be called like :
> for (int i = 0; i < size; ++i)
>         outarr[i] = foo(inarr0[i], inarr[1]);
> My question is about how can I handle type conversion of input array,
> preferably in efficient manner, given that each input array may require
> different input type.
> Currently I basically enforce the type and no type conversion is
> performed. But I would like relax it. I thought of several possibility
> starting from the obvious solution consisting on recasting in the inner
> loop that would give:
> for (int i = 0; i < size; ++i)
>         if (inarr[i] need recast)
>                 in0 =
> recast(inarr[i])
>         else
>                 in0 = inarr[i]
>         [... same for all
> inputs parameter ...]
>         outarr[i] = foo(in0, in1, ...);
> This solution is memory efficient, but not actually computationally
> efficient.
> The second solution is to copy&recast the entire inputs arrays, but in
> that case it's not memory efficient. And my final thought is to mix the
> first and the second by chunking the second method, i.e. converting N
> input in a raw, then applying the function to them en so on until all
> the array is processed.
> Thus my questions are:
>  - there is another way to do what I want?
>  - there is an existing or recommended way to do it?
> And a side question, I use the PyArray_FROM_OTF, but I do not
> understand well it's semantic. If I pass a python list, it is converted
> to the desired type and requirement; when I pass a non-continuous array
> it is converted to the continuous one; but when I pass a numpy array of
> another type than the one specified I do not get the conversion. There
> is a function that do the conversion unconditionally? Did I missed
> something ?
> Thank you by advance for your help
> Best regards
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20200310/74597eb2/attachment.html>

More information about the NumPy-Discussion mailing list