<div dir="ltr">There are a variety of ways to resolve your issues.  You can try using the optional arguments casting, dtype or signature that work for all ufuncs see <a href="https://numpy.org/doc/1.18/reference/ufuncs.html#optional-keyword-arguments">https://numpy.org/doc/1.18/reference/ufuncs.html#optional-keyword-arguments</a>  This will allow you to override the default type checks.  What you'll find for many ufuncs in, for instance, scipy.special, is that the underlying function in cython, C or Fortran is only defined for doubles, but the function has a signature for f->f, which just casts the input and output in the loop.  Generally, downcasting is not permitted without explicit opt in, e.g. int64 to int32, since this is not a safe cast as there are many values that an int64 can hold that an int32 cannot.  Generally speaking, you do have to manage the types of your arrays when the defaults aren't what you want.  There really isn't anyway around it.<div><br></div><div>Eric </div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 11, 2020 at 4:43 AM 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 Eric,<br>
<br>
Thank you for pointing out ufunc, I implemented my binding using it,<br>
it's working and it's simpler than my previous implementation, but I<br>
still not have the flexibility to dynamically recast input array, i.e.<br>
using a int64 array as input for a int32. For instance for testing my<br>
code I use numpy.random that provide int64 array, I have to convert the<br>
array manually before calling my ufunc, which is somehow annoying.<br>
<br>
For function that have 1 or 2 parameters it's practical to have 4<br>
variant of the function, but in case of 6-8 parameters it's becoming<br>
more difficult.<br>
<br>
Best regards<br>
<br>
Le mardi 10 mars 2020 à 13:13 -0400, Eric Moore a écrit :<br>
> Hi Benoit, <br>
> <br>
> Since you have a function that takes two scalars to one scalar, it<br>
> sounds to me as though you would be best off creating a ufunc.  This<br>
> will then handle the conversion to and looping over the arrays, etc<br>
> for you.  The documentation is available here: <br>
> <a href="https://numpy.org/doc/1.18/user/c-info.ufunc-tutorial.html" rel="noreferrer" target="_blank">https://numpy.org/doc/1.18/user/c-info.ufunc-tutorial.html</a>.<br>
> <br>
> Regards,<br>
> <br>
> Eric<br>
> <br>
> On Tue, Mar 10, 2020 at 12:28 PM Benoit Gschwind <<br>
> <a href="mailto:gschwind@gnu-log.net" target="_blank">gschwind@gnu-log.net</a>> wrote:<br>
> > Hello,<br>
> > <br>
> > I writing a python binding of one of our library. The binding<br>
> > 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<br>
> > array,<br>
> > preferably in efficient manner, given that each input array may<br>
> > 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<br>
> > possibility<br>
> > starting from the obvious solution consisting on recasting in the<br>
> > 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<br>
> > in<br>
> > that case it's not memory efficient. And my final thought is to mix<br>
> > the<br>
> > first and the second by chunking the second method, i.e. converting<br>
> > N<br>
> > input in a raw, then applying the function to them en so on until<br>
> > 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<br>
> > converted<br>
> > to the desired type and requirement; when I pass a non-continuous<br>
> > array<br>
> > it is converted to the continuous one; but when I pass a numpy<br>
> > array of<br>
> > another type than the one specified I do not get the conversion.<br>
> > 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>
> <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>
<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>