Hello Eric, Thank you for pointing out ufunc, I implemented my binding using it, it's working and it's simpler than my previous implementation, but I still not have the flexibility to dynamically recast input array, i.e. using a int64 array as input for a int32. For instance for testing my code I use numpy.random that provide int64 array, I have to convert the array manually before calling my ufunc, which is somehow annoying.
For function that have 1 or 2 parameters it's practical to have 4 variant of the function, but in case of 6-8 parameters it's becoming more difficult. Best regards Le mardi 10 mars 2020 à 13:13 -0400, Eric Moore a écrit : > 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 < > gschw...@gnu-log.net> 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: > > > > <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@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 mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion