I think you're right: the function `stack`, as you've defined it, is a
gufunc.

Here's an implementation using np.vectorize
<https://numpy.org/doc/stable/reference/generated/numpy.vectorize.html>
(rather than nb.vectorize
<https://numba.readthedocs.io/en/stable/user/vectorize.html>). Since the
signature is not "()->()" or "(),()->()" (or similar with a different
number of scalar inputs and scalar outputs), it's a generalized ufunc.

>>> def stack(a, b):

...     broadcasts = np.broadcast_arrays(a, b)

...     return np.stack(broadcasts, axis=-1)

...

>>> stacky = np.vectorize(stack, signature="(),()->(2)")

>>> stacky(np.arange(5), np.arange(5))

array([[0, 0],

       [1, 1],

       [2, 2],

       [3, 3],

       [4, 4]])

>>> stacky(np.arange(5), np.array([[1], [2], [3], [4], [5]]))

array([[[0, 1],

        [1, 1],

        [2, 1],

        [3, 1],

        [4, 1]],


       [[0, 2],

        [1, 2],

        [2, 2],

        [3, 2],

        [4, 2]],


       [[0, 3],

        [1, 3],

        [2, 3],

        [3, 3],

        [4, 3]],


       [[0, 4],

        [1, 4],

        [2, 4],

        [3, 4],

        [4, 4]],


       [[0, 5],

        [1, 5],

        [2, 5],

        [3, 5],

        [4, 5]]])



On Tue, Dec 31, 2024 at 2:44 AM john.a.dawson--- via NumPy-Discussion <
numpy-discussion@python.org> wrote:

> Is the function `stack` above a gufunc?
> _______________________________________________
> 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: jpivar...@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

Reply via email to