[Numpy-discussion] Re: Tricky ufunc implementation question

2025-06-27 Thread Benjamin Root via NumPy-Discussion
Warren,

I'm fine with implementing it in C. I just didn't think gufuncs were for
me. I couldn't tell from the description if it would be for my usecase
since I wasn't looping over subarrays, and I didn't see any good examples.
Maybe the documentation could be clearer. I'll have a look at your examples.

I did try that signature with np.vectorize() with the signature keyword
argument, but it didn't seem to work. Maybe it didn't work for the reasons
in that open issue.

Thank you,
Ben Root

On Fri, Jun 27, 2025 at 8:03 PM Warren Weckesser via NumPy-Discussion <
numpy-discussion@python.org> wrote:

> On Fri, Jun 27, 2025 at 5:29 PM Benjamin Root via NumPy-Discussion
>  wrote:
> >
> > I'm looking at a situation where I like to wrap a C++ function that
> takes two doubles as inputs, and returns an error code, a position vector,
> and a velocity vector so that I essentially would have a function signature
> of (N), (N) -> (N), (N, 3), (N, 3). When I try to use np.vectorize() or
> np.frompyfunc() on the python version of this function, I keep running into
> issues where it wants to make the outputs into object arrays of tuples. And
> looking at utilizing PyUFunc_FromFuncAndData, it isn't clear to me how I
> can tell it to expect those two output arrays to have a size 3 outer
> dimension.
> >
> > Are ufuncs the wrong thing here? How should I go about this? Is it even
> possible?
>
> Ben,
>
> It looks like the simplest signature for your core operation would be
> (),()->(),(3),(3), with broadcasting taking care of higher dimensional
> inputs.  Because not all the core shapes are scalars, that would
> require a *generalized* ufunc (gufunc).  There is an open issue
> (https://github.com/numpy/numpy/issues/14020) with a request for a
> function to generate a gufunc from a Python function.
>
> numba has the @guvectorize decorator, but I haven't use it much, and
> in my few quick attempts just now, it appeared to not accept fixed
> integer sizes in the output shape.  But wait to see if any numba gurus
> respond with a definitive answer about whether or not it can handle
> the shape signature (),()->(),(3),(3).
>
> You could implement the gufunc in a C or C++ extension module, if you
> don't mind the additional development effort and packaging hassle.  I
> know that works--I've implemented quite a few gufuncs in ufunclab
> (https://github.com/WarrenWeckesser/ufunclab).
>
> Warren
>
>
> >
> > Thanks in advance,
> > Ben Root
> > ___
> > NumPy-Discussion mailing list -- numpy-discussion@python.org
> > To unsubscribe send an email to numpy-discussion-le...@python.org
> > https://mail.python.org/mailman3//lists/numpy-discussion.python.org
> > Member address: warren.weckes...@gmail.com
> ___
> NumPy-Discussion mailing list -- numpy-discussion@python.org
> To unsubscribe send an email to numpy-discussion-le...@python.org
> https://mail.python.org/mailman3//lists/numpy-discussion.python.org
> Member address: ben.v.r...@gmail.com
>
___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3//lists/numpy-discussion.python.org
Member address: arch...@mail-archive.com


[Numpy-discussion] Tricky ufunc implementation question

2025-06-27 Thread Benjamin Root via NumPy-Discussion
I'm looking at a situation where I like to wrap a C++ function that takes
two doubles as inputs, and returns an error code, a position vector, and a
velocity vector so that I essentially would have a function signature of
(N), (N) -> (N), (N, 3), (N, 3). When I try to use np.vectorize() or
np.frompyfunc() on the python version of this function, I keep running into
issues where it wants to make the outputs into object arrays of tuples. And
looking at utilizing PyUFunc_FromFuncAndData, it isn't clear to me how I
can tell it to expect those two output arrays to have a size 3 outer
dimension.

Are ufuncs the wrong thing here? How should I go about this? Is it even
possible?

Thanks in advance,
Ben Root
___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3//lists/numpy-discussion.python.org
Member address: arch...@mail-archive.com


[Numpy-discussion] Re: Tricky ufunc implementation question

2025-06-27 Thread Warren Weckesser via NumPy-Discussion
On Fri, Jun 27, 2025 at 5:29 PM Benjamin Root via NumPy-Discussion
 wrote:
>
> I'm looking at a situation where I like to wrap a C++ function that takes two 
> doubles as inputs, and returns an error code, a position vector, and a 
> velocity vector so that I essentially would have a function signature of (N), 
> (N) -> (N), (N, 3), (N, 3). When I try to use np.vectorize() or 
> np.frompyfunc() on the python version of this function, I keep running into 
> issues where it wants to make the outputs into object arrays of tuples. And 
> looking at utilizing PyUFunc_FromFuncAndData, it isn't clear to me how I can 
> tell it to expect those two output arrays to have a size 3 outer dimension.
>
> Are ufuncs the wrong thing here? How should I go about this? Is it even 
> possible?

Ben,

It looks like the simplest signature for your core operation would be
(),()->(),(3),(3), with broadcasting taking care of higher dimensional
inputs.  Because not all the core shapes are scalars, that would
require a *generalized* ufunc (gufunc).  There is an open issue
(https://github.com/numpy/numpy/issues/14020) with a request for a
function to generate a gufunc from a Python function.

numba has the @guvectorize decorator, but I haven't use it much, and
in my few quick attempts just now, it appeared to not accept fixed
integer sizes in the output shape.  But wait to see if any numba gurus
respond with a definitive answer about whether or not it can handle
the shape signature (),()->(),(3),(3).

You could implement the gufunc in a C or C++ extension module, if you
don't mind the additional development effort and packaging hassle.  I
know that works--I've implemented quite a few gufuncs in ufunclab
(https://github.com/WarrenWeckesser/ufunclab).

Warren


>
> Thanks in advance,
> Ben Root
> ___
> NumPy-Discussion mailing list -- numpy-discussion@python.org
> To unsubscribe send an email to numpy-discussion-le...@python.org
> https://mail.python.org/mailman3//lists/numpy-discussion.python.org
> Member address: warren.weckes...@gmail.com
___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3//lists/numpy-discussion.python.org
Member address: arch...@mail-archive.com