[Numpy-discussion] Handle type convertion in C API
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: module.foo(, ) 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@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Handle type convertion in C API
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: https://numpy.org/doc/1.18/user/c-info.ufunc-tutorial.html. Regards, Eric On Tue, Mar 10, 2020 at 12:28 PM Benoit Gschwind wrote: > 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: > > module.foo(, ) > > 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@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion > ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] NumPy Development Meeting - Triage Focus
Hi all, (Note that the US has switched to daylight saving time) Our bi-weekly triage-focused NumPy development meeting is tomorrow (Wednesday, Februrary 26) at 11 am Pacific Time. Everyone is invited to join in and edit the work-in-progress meeting topics and notes: https://hackmd.io/68i_JvOYQfy9ERiHgXMPvg I encourage everyone to notify us of issues or PRs that you feel should be prioritized or simply discussed briefly. Just comment on it so we can label it, or add your PR/issue to this weeks topics for discussion. Best regards Sebastian signature.asc Description: This is a digitally signed message part ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion