<div dir="ltr">Hi Benoit, <div><br></div><div>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: <a href="https://numpy.org/doc/1.18/user/c-info.ufunc-tutorial.html">https://numpy.org/doc/1.18/user/c-info.ufunc-tutorial.html</a>.</div><div><br></div><div>Regards,</div><div><br></div><div>Eric</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Mar 10, 2020 at 12:28 PM Benoit Gschwind <<a href="mailto:gschwind@gnu-log.net">gschwind@gnu-log.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello,<br>
<br>
I writing a python binding of one of our library. The binding intend to<br>
vectorize the function call. for exemple:<br>
<br>
double foo(double, double) will be bound to a python:<br>
<br>
<numpy.array of double> module.foo(<numpy.array>, <numpy.array>)<br>
<br>
and the function foo will be called like :<br>
<br>
for (int i = 0; i < size; ++i)<br>
outarr[i] = foo(inarr0[i], inarr[1]);<br>
<br>
My question is about how can I handle type conversion of input array,<br>
preferably in efficient manner, given that each input array may require<br>
different input type.<br>
<br>
Currently I basically enforce the type and no type conversion is<br>
performed. But I would like relax it. I thought of several possibility<br>
starting from the obvious solution consisting on recasting in the inner<br>
loop that would give:<br>
<br>
for (int i = 0; i < size; ++i)<br>
if (inarr[i] need recast)<br>
in0 =<br>
recast(inarr[i])<br>
else<br>
in0 = inarr[i]<br>
[... same for all<br>
inputs parameter ...]<br>
outarr[i] = foo(in0, in1, ...);<br>
<br>
This solution is memory efficient, but not actually computationally<br>
efficient.<br>
<br>
The second solution is to copy&recast the entire inputs arrays, but in<br>
that case it's not memory efficient. And my final thought is to mix the<br>
first and the second by chunking the second method, i.e. converting N<br>
input in a raw, then applying the function to them en so on until all<br>
the array is processed.<br>
<br>
Thus my questions are:<br>
- there is another way to do what I want?<br>
- there is an existing or recommended way to do it?<br>
<br>
And a side question, I use the PyArray_FROM_OTF, but I do not<br>
understand well it's semantic. If I pass a python list, it is converted<br>
to the desired type and requirement; when I pass a non-continuous array<br>
it is converted to the continuous one; but when I pass a numpy array of<br>
another type than the one specified I do not get the conversion. There<br>
is a function that do the conversion unconditionally? Did I missed<br>
something ?<br>
<br>
Thank you by advance for your help<br>
<br>
Best regards<br>
<br>
<br>
<br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>