Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

BUG cannot specify second output to ufunc as keyword argument #4752

Open
mhvk opened this Issue · 11 comments

4 participants

Marten van Kerkwijk Jaime Nathaniel J. Smith Charles Harris
Marten van Kerkwijk

Currently, it does not seem possible to pass on an array to catch the second output of a ufunc via a keyword parameter:

a1 = np.array([1.])
a2 = np.array([1.])
np.modf(1.333, out=a1, out2=a2)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-9ca83f6d80bd> in <module>()
----> 1 np.modf(1.333, out=a1, out2=a2)

ValueError: cannot specify 'out' as both a positional and keyword argument

One can do np.modf(1.333, out=a1), which works as expected. If one gives instead np.modf(1.333, out2=a2), the first part is assigned to a2. (Note that the documentation suggests to use out1, out2, but any characters after out simply seem to be skipped.)

It would seem good to have the option of giving these as keyword arguments. One solution might be to have it be consistent with the usage in __numpy_ufunc__, i.e., one would give np.modf(values, out=(a1, a2)).

Jaime
Owner

Currently the only way to specify multiple output arguments is as positional arguments. Using your example:

>>> np.modf(1.3333, a1, a2)
(array([ 0.3333]), array([ 1.]))
>>> a1
array([ 0.3333])
>>> a2
array([ 1.])

To add a different behavior (such as the tuple of arrays passed as a kwarg with out=) would require discussion on the list.

Nathaniel J. Smith
Owner
Jaime
Owner

I'm sure someone will not like the color of the bike shed... Just pinged the list, lets see if anyone cares. We should take advantage of me having the code of ufunc_object.c still fresh in memory.

Marten van Kerkwijk

@jaimefrio - this has been quite a while, but maybe you are still well-versed enough in ufunc_object.c to make the dual-output case work? I ilke how your suggestion of ufunc(<inputs>, out=tuple(outputs)) jives with what one gets passed on with __numpy_ufunc__.

Jaime
Owner

It is certainly no longer in the L1 cache, but I will try to find time to take a look again this weekend. If I can make sense of it I'll try pinging the list again: if I remember correctly, there wasn't a single response from the list last time. :-(

Nathaniel J. Smith
Owner
Charles Harris
Owner

Seems reasonable to me also.

Jaime
Owner

I have it working, but am getting this error when I run the tests:

ERROR: test_out_subok (test_umath.TestOut)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/jaimefrio/open_source/numpy/build/testenv/lib/python2.7/site-packages/numpy/core/tests/test_umath.py", line 65, in test_out_subok
    r1, r2 = np.frexp(d, out=o1, subok=b)
TypeError: 'out' must be an array or tuple of arrays

The way I have it set up, it only accepts a single array to out (as opposed to a tuple) if it only has one return. I guess I'll have to modify it so that you can set the first output array regardless of how many returns the ufunc has. Question is, do we want to keep this behavior forever? Or should we deprecate it?

Nathaniel J. Smith
Owner
Marten van Kerkwijk

Agreed with @njsmith - converting to out=(array, None) would seem most logical. I'm not even sure it is worth deprecating -- most important might be to ensure the documentation is clear.

Jaime
Owner

I'll make a PR even if it's half cooked only, and we can have the discussion over the code, which will probably explain itself better than me. My current implementation already does what @njsmith proposes: tuple of nout elements, but None is a perfectly valid entry of that tuple.

Jaime jaimefrio referenced this issue from a commit in jaimefrio/numpy
Jaime jaimefrio ENH: ufuncs can take a tuple of arrays as 'out' kwarg
Closes #4752
8eac157
Jaime jaimefrio referenced this issue from a commit in jaimefrio/numpy
Jaime jaimefrio ENH: ufuncs can take a tuple of arrays as 'out' kwarg
Closes #4752
4a09df1
Jaime jaimefrio referenced this issue from a commit in jaimefrio/numpy
Jaime jaimefrio ENH: ufuncs can take a tuple of arrays as 'out' kwarg
Closes #4752
2929b5e
Jaime jaimefrio referenced this issue from a commit in jaimefrio/numpy
Jaime jaimefrio ENH: ufuncs can take a tuple of arrays as 'out' kwarg
Closes #4752
284c65a
Jaime jaimefrio referenced this issue from a commit in jaimefrio/numpy
Jaime jaimefrio ENH: ufuncs can take a tuple of arrays as 'out' kwarg
Closes #4752
b876c2d
Jaime jaimefrio referenced this issue from a commit in jaimefrio/numpy
Jaime jaimefrio ENH: ufuncs can take a tuple of arrays as 'out' kwarg
Closes #4752
019360e
Jaime jaimefrio referenced this issue from a commit in jaimefrio/numpy
Jaime jaimefrio ENH: ufuncs can take a tuple of arrays as 'out' kwarg
Closes #4752
590900a
Jaime jaimefrio referenced this issue from a commit in jaimefrio/numpy
Jaime jaimefrio ENH: ufuncs can take a tuple of arrays as 'out' kwarg
Closes #4752
cb61691
Jaime jaimefrio referenced this issue from a commit in jaimefrio/numpy
Jaime jaimefrio ENH: ufuncs can take a tuple of arrays as 'out' kwarg
Closes #4752
f931459
Jaime jaimefrio referenced this issue from a commit in jaimefrio/numpy
Jaime jaimefrio ENH: ufuncs can take a tuple of arrays as 'out' kwarg
Closes #4752
c82cf07
Jaime jaimefrio referenced this issue from a commit in jaimefrio/numpy
Jaime jaimefrio ENH: ufuncs can take a tuple of arrays as 'out' kwarg
Closes #4752
854a974
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.