diff options
author | Marten van Kerkwijk <mhvk@astro.utoronto.ca> | 2017-05-12 10:13:39 -0400 |
---|---|---|
committer | Marten van Kerkwijk <mhvk@astro.utoronto.ca> | 2017-05-17 21:13:06 -0400 |
commit | ca49f0b11bc88056bf4767ac439b298cfda16fed (patch) | |
tree | f6109a40f862a9cb604d9328af9c74db6dec41d7 | |
parent | 18bac353f667da6969716b60f3d3315ffd0b7aaa (diff) | |
download | numpy-ca49f0b11bc88056bf4767ac439b298cfda16fed.tar.gz |
DOC: update documentation allowing tuple of one in reduce, etc.
-rw-r--r-- | doc/neps/ufunc-overrides.rst | 3 | ||||
-rw-r--r-- | doc/release/1.13.0-notes.rst | 8 | ||||
-rw-r--r-- | doc/source/reference/ufuncs.rst | 9 | ||||
-rw-r--r-- | numpy/add_newdocs.py | 39 | ||||
-rw-r--r-- | numpy/core/code_generators/ufunc_docstrings.py | 8 | ||||
-rw-r--r-- | numpy/core/tests/test_umath.py | 3 | ||||
-rw-r--r-- | numpy/doc/subclassing.py | 2 |
7 files changed, 53 insertions, 19 deletions
diff --git a/doc/neps/ufunc-overrides.rst b/doc/neps/ufunc-overrides.rst index 451b55a62..d71717524 100644 --- a/doc/neps/ufunc-overrides.rst +++ b/doc/neps/ufunc-overrides.rst @@ -166,7 +166,8 @@ Hence, the arguments are normalized: only the required input arguments passed on as a dict of keyword arguments (``kwargs``). In particular, if there are output arguments, positional are otherwise, that are not :obj:`None`, they are passed on as a tuple in the ``out`` keyword -argument. +argument (even for the ``reduce``, ``accumulate``, and ``reduceat`` methods +where in all current cases only a single output makes sense). The function dispatch proceeds as follows: diff --git a/doc/release/1.13.0-notes.rst b/doc/release/1.13.0-notes.rst index a712c6949..21ae533b7 100644 --- a/doc/release/1.13.0-notes.rst +++ b/doc/release/1.13.0-notes.rst @@ -536,3 +536,11 @@ The ABCPolyBase class, from which the convenience classes are derived, sets ``__array_ufun__ = None`` in order of opt out of ufuncs. If a polynomial convenience class instance is passed as an argument to a ufunc, a ``TypeError`` will now be raised. + +Output arguments to ufuncs can be tuples also for ufunc methods +--------------------------------------------------------------- +For calls to ufuncs, it was already possible, and recommended, to use an +``out`` argument with a tuple for ufuncs with multiple outputs. This has now +been extended to output arguments in the ``reduce``, ``accumulate``, and +``reduceat`` methods. This is mostly for compatibility with ``__array_ufunc``; +there are no ufuncs yet that have more than one output. diff --git a/doc/source/reference/ufuncs.rst b/doc/source/reference/ufuncs.rst index b3fb4d384..9a8d8e20e 100644 --- a/doc/source/reference/ufuncs.rst +++ b/doc/source/reference/ufuncs.rst @@ -426,8 +426,8 @@ Methods All ufuncs have four methods. However, these methods only make sense on ufuncs that take two input arguments and return one output argument. Attempting to call these methods on other ufuncs will cause a -:exc:`ValueError`. The reduce-like methods all take an *axis* keyword -and a *dtype* keyword, and the arrays must all have dimension >= 1. +:exc:`ValueError`. The reduce-like methods all take an *axis* keyword, a *dtype* +keyword, and an *out* keyword, and the arrays must all have dimension >= 1. The *axis* keyword specifies the axis of the array over which the reduction will take place and may be negative, but must be an integer. The *dtype* keyword allows you to manage a very common problem that arises @@ -443,7 +443,10 @@ mostly up to you. There is one exception: if no *dtype* is given for a reduction on the "add" or "multiply" operations, then if the input type is an integer (or Boolean) data-type and smaller than the size of the :class:`int_` data type, it will be internally upcast to the :class:`int_` -(or :class:`uint`) data-type. +(or :class:`uint`) data-type. Finally, the *out* keyword allows you to provide +an output array (for single-output ufuncs, which are currently the only ones +supported; for future extension, however, a tuple with a single argument +can be passed in). If *out* is given, the *dtype* argument is ignored. Ufuncs also have a fifth method that allows in place operations to be performed using fancy indexing. No buffering is used on the dimensions where diff --git a/numpy/add_newdocs.py b/numpy/add_newdocs.py index 449196efb..7cf61c4d0 100644 --- a/numpy/add_newdocs.py +++ b/numpy/add_newdocs.py @@ -5444,9 +5444,11 @@ add_newdoc('numpy.core', 'ufunc', ---------- *x : array_like Input arrays. - out : ndarray or tuple of ndarray, optional + out : ndarray, None, or tuple of ndarray and None, optional Alternate array object(s) in which to put the result; if provided, it - must have a shape that the inputs broadcast to. + must have a shape that the inputs broadcast to. A tuple of arrays + (possible only as a keyword argument) must have length equal to the + number of outputs; use `None` for outputs to be allocated by the ufunc. where : array_like, optional Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone. @@ -5667,9 +5669,14 @@ add_newdoc('numpy.core', 'ufunc', ('reduce', The type used to represent the intermediate results. Defaults to the data-type of the output array if this is provided, or the data-type of the input array if no output array is provided. - out : ndarray, optional - A location into which the result is stored. If not provided, a - freshly-allocated array is returned. + out : ndarray, None, or tuple of ndarray and None, optional + A location into which the result is stored. If not provided or `None`, + a freshly-allocated array is returned. For consistency with + :ref:`ufunc.__call__`, if given as a keyword, this may be wrapped in a + 1-element tuple. + + .. versionchanged:: 1.13.0 + Tuples are allowed for keyword argument. keepdims : bool, optional If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, @@ -5741,9 +5748,14 @@ add_newdoc('numpy.core', 'ufunc', ('accumulate', The data-type used to represent the intermediate results. Defaults to the data-type of the output array if such is provided, or the the data-type of the input array if no output array is provided. - out : ndarray, optional - A location into which the result is stored. If not provided a - freshly-allocated array is returned. + out : ndarray, None, or tuple of ndarray and None, optional + A location into which the result is stored. If not provided or `None`, + a freshly-allocated array is returned. For consistency with + :ref:`ufunc.__call__`, if given as a keyword, this may be wrapped in a + 1-element tuple. + + .. versionchanged:: 1.13.0 + Tuples are allowed for keyword argument. keepdims : bool Has no effect. Deprecated, and will be removed in future. @@ -5820,9 +5832,14 @@ add_newdoc('numpy.core', 'ufunc', ('reduceat', The type used to represent the intermediate results. Defaults to the data type of the output array if this is provided, or the data type of the input array if no output array is provided. - out : ndarray, optional - A location into which the result is stored. If not provided a - freshly-allocated array is returned. + out : ndarray, None, or tuple of ndarray and None, optional + A location into which the result is stored. If not provided or `None`, + a freshly-allocated array is returned. For consistency with + :ref:`ufunc.__call__`, if given as a keyword, this may be wrapped in a + 1-element tuple. + + .. versionchanged:: 1.13.0 + Tuples are allowed for keyword argument. Returns ------- diff --git a/numpy/core/code_generators/ufunc_docstrings.py b/numpy/core/code_generators/ufunc_docstrings.py index 7beda59f2..c5172f6a8 100644 --- a/numpy/core/code_generators/ufunc_docstrings.py +++ b/numpy/core/code_generators/ufunc_docstrings.py @@ -19,9 +19,11 @@ def get(name): # common parameter text to all ufuncs _params_text = textwrap.dedent(""" - out : ndarray or tuple of ndarray, optional - Alternate array object(s) in which to put the result; if provided, it - must have a shape that the inputs broadcast to. + out : ndarray, None, or tuple of ndarray and None, optional + A location into which the result is stored. If provided, it must have + a shape that the inputs broadcast to. If not provided or `None`, + a freshly-allocated array is returned. A tuple (possible only as a + keyword argument) must have length equal to the number of outputs. where : array_like, optional Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone. diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 120844c82..97c9c25d5 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -2010,7 +2010,8 @@ class TestSpecialMethods(TestCase): assert_raises(ValueError, inner1d, a, a, out=()) def test_ufunc_override_with_super(self): - + # NOTE: this class is given as an example in doc/subclassing.py; + # if you make any changes here, do update it there too. class A(np.ndarray): def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): args = [] diff --git a/numpy/doc/subclassing.py b/numpy/doc/subclassing.py index 36d8ff97d..51d9dc120 100644 --- a/numpy/doc/subclassing.py +++ b/numpy/doc/subclassing.py @@ -489,6 +489,8 @@ following. return NotImplemented if method == 'at': + if isinstance(inputs[0], A): + inputs[0].info = info return if ufunc.nout == 1: |