Multiple returns

0 views
Skip to first unread message

Gideon Simpson

unread,
Apr 28, 2017, 1:31:38 PM4/28/17
to Numba Public Discussion - Public
I'm trying to use @vectorize on a function which takes two arguments and returns two arguments, i.e.:
@numba.vectorize(nopython=True)    
def test(x,y):
    
    f = np.cos(x+y)
    
    g = np.sin(x*y)
    
    return f,g
however, when I try to use it, I get:
NotImplementedError: (float64 x 2) cannot be represented as a Numpy dtype

How can I get the desired behavior?  I'm using 0.32

Gil Forsyth

unread,
Apr 28, 2017, 4:04:06 PM4/28/17
to Numba Public Discussion - Public
Hi Gideon,

When you create a ufunc using vectorize, Numba looks at the inputs and output as scalars and then generates the loop to allow it to operate on NumPy arrays. I don't think you can have multiple outputs in this case, as various ufunc builtins won't make a lot of sense (like accumulate, etc...)

These will work find as two separate calls, one for the cos and one for the sin

Stanley Seibert

unread,
Apr 28, 2017, 4:33:18 PM4/28/17
to Numba Public Discussion - Public
NumPy's vectorize decorator can handle a tuple of returns, but Numba's cannot yet.

I think the only workaround in this case is to write a @jit function for now.

--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users+unsubscribe@continuum.io.
To post to this group, send email to numba...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/f7a28bf0-a2d0-47fd-8fe6-8662dabfcaf1%40continuum.io.

Gideon Simpson

unread,
Apr 28, 2017, 5:01:49 PM4/28/17
to Numba Public Discussion - Public
How would the @jit formulation work?


On Friday, April 28, 2017 at 4:33:18 PM UTC-4, Stanley Seibert wrote:
NumPy's vectorize decorator can handle a tuple of returns, but Numba's cannot yet.

I think the only workaround in this case is to write a @jit function for now.
On Fri, Apr 28, 2017 at 3:04 PM, Gil Forsyth <gilfo...@gmail.com> wrote:
Hi Gideon,

When you create a ufunc using vectorize, Numba looks at the inputs and output as scalars and then generates the loop to allow it to operate on NumPy arrays. I don't think you can have multiple outputs in this case, as various ufunc builtins won't make a lot of sense (like accumulate, etc...)

These will work find as two separate calls, one for the cos and one for the sin

On Friday, April 28, 2017 at 1:31:38 PM UTC-4, Gideon Simpson wrote:
I'm trying to use @vectorize on a function which takes two arguments and returns two arguments, i.e.:
@numba.vectorize(nopython=True)    
def test(x,y):
    
    f = np.cos(x+y)
    
    g = np.sin(x*y)
    
    return f,g
however, when I try to use it, I get:
NotImplementedError: (float64 x 2) cannot be represented as a Numpy dtype

How can I get the desired behavior?  I'm using 0.32

--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users...@continuum.io.

To post to this group, send email to numba...@continuum.io.

Rutger Kassies

unread,
May 1, 2017, 7:47:09 AM5/1/17
to Numba Public Discussion - Public
You only have to change the decorator, for example:

@numba.njit()    
def test(x,y):
   
    f
= np.cos(x+y)    
    g
= np.sin(x*y)
   
   
return f,g


But you will (of course) lose the benefits of 'vectorize'. For example, if you have reasonably large arrays, and compile with target 'parallel', having two separate vectorized functions would probably be faster.


Regards,
Rutger

cristi purdel

unread,
Jul 12, 2018, 10:35:12 AM7/12/18
to Numba Public Discussion - Public
Any chance of getting this as a feature request?
I've opened an issue on github:
Reply all
Reply to author
Forward
0 new messages