-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
ENH: Add __array_ufunc__
#8247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
ENH: Add __array_ufunc__
#8247
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
4fd7e84
ENH: Revert "Temporarily disable __numpy_ufunc__"
charris fcd11d2
ENH: Rename __numpy_ufunc__ to __array_ufunc__.
charris c7b25e2
ENH: Remove position arg from __array_ufunc__.
charris 8a9e790
MAINT: Put PyArray_GetAttrString_SuppressException in get_attr_string.h
njsmith 4dd5380
MAINT: dike out a bunch of weird old code implementing scalar power
njsmith 7d9bc2f
BUG/ENH: Switch to simplified __array_ufunc__/binop interaction
njsmith e4b5163
MAINT: allow __array_ufunc__ = None to force binops to defer.
mhvk 2e6d8c0
MAINT: Split out C code in ufunc_override.h to .c file.
mhvk d5c5ac1
MAINT: Add NPY_NO_EXPORT modifier to PyUFunc_CheckOverride.
charris 3124e96
MAINT: for __array_ufunc__ pass inputs as *args, ensure out is tuple.
mhvk 6a3ca31
DOC: describe current implementation of __array_ufunc__.
mhvk 79bb733
DOC: Style and sphinx fixes for arrays.classes.rst.
charris 7c3dc5a
TST: test that gufuncs are also overridden by __array_ufunc__.
mhvk 71201d2
DOC: Describe __array_func__ in subclassing
mhvk 3041710
ENH: implement ndarray.__array_ufunc__
mhvk 5fe6fc6
DOC Update NEP to reflect actual implementation.
mhvk e092823
MAINT: let ndarray.__array_ufunc__ bail if any overrides are in place.
mhvk 1147894
MAINT: Update array_ufunc NEP.
pv e325a10
DOC: Document behavior of ufuncs with default ndarray.__array_ufunc__
pv 39c2273
DOC: Update ndarray.__array_ufunc__ documentation vs. review comments
pv 6b41d11
DOC: clarify use of super and getattr
mhvk 0ede0e9
DOC: update NEP again.
mhvk 5f9252c
DOC: implement many smaller and bigger changes suggested in review.
mhvk 8cc2f71
BUG,MAINT: ensure out=None is never passed on to __array_ufunc__.
mhvk 856da73
DOC: remove left-over piece discussing binops
mhvk 2b6c7fd
REVERT: remove __array_ufunc__ override for np.dot and ndarray.dot.
mhvk 36e8494
REVERT: remove __array_ufunc__ override for np.matmul.
mhvk 55500b9
MAINT: simplify now that __array_ufunc__ overrides ufuncs only.
mhvk 25e973d
MAINT: split out umath-specific part of ufunc_override.
mhvk b1fa10a
BUG: ensure subclass of override class doesn't segfault.
mhvk 1de8f5a
DOC: Mention `__array_ufunc__` in the 1.13.0 release notes.
charris a460015
DOC: ufunc-overrides: sync the discussion vs. current implementation
pv cd2e42c
DOC: ufunc-overrides: revise hierarchy discussion
pv ff628f1
BUG: Add back removed elision code.
charris 1fc6e63
DOC,TST: clarify example of ndarray subclass using __array_ufunc__
mhvk a431743
BUG: Support nout == 0 and at method
eric-wieser 1e460b7
DOC,MAINT: small corrections to NEP following Stephan's comments.
mhvk 02600d3
ENH: Add NDArrayOperatorsMixin mixin class.
shoyer d3ff023
DOC: clarify recommendations for subclasses, deprecations.
mhvk b9359f1
MAINT: remove unnecessary checks, wrong code for 'outer', cleanup.
mhvk 256a8ae
BUG: Fix ArrayLike(NDArrayOperatorsMixin) operations with object()
shoyer 3272a86
ENH: Better error message for __array_ufunc__ not implemented
shoyer 32221df
ENH: NDArrayOperatorsMixin calls ufuncs directly, like ndarray
shoyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
ENH: Rename __numpy_ufunc__ to __array_ufunc__.
The first commit in changing __numpy_ufunc__ by removing the index argument and making the out argument value always a tuple. These changes were proposed in gh-5986 and have been accepted. Renaming before further changes avoids triggering tests in scipy and astropy while keeping the numpy tests working.
- Loading branch information
commit fcd11d2b0e30a3eb1ca8d391ee965431c4a1fdfd
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ Special attributes and methods | |
|
||
NumPy provides several hooks that classes can customize: | ||
|
||
.. method:: class.__numpy_ufunc__(ufunc, method, i, inputs, **kwargs) | ||
.. method:: class.__array_ufunc__(ufunc, method, i, inputs, **kwargs) | ||
|
||
.. versionadded:: 1.11 | ||
|
||
|
@@ -62,51 +62,51 @@ NumPy provides several hooks that classes can customize: | |
:obj:`NotImplemented` if the operation requested is not | ||
implemented. | ||
|
||
If one of the arguments has a :func:`__numpy_ufunc__` method, it is | ||
If one of the arguments has a :func:`__array_ufunc__` method, it is | ||
executed *instead* of the ufunc. If more than one of the input | ||
arguments implements :func:`__numpy_ufunc__`, they are tried in the | ||
arguments implements :func:`__array_ufunc__`, they are tried in the | ||
order: subclasses before superclasses, otherwise left to right. The | ||
first routine returning something else than :obj:`NotImplemented` | ||
determines the result. If all of the :func:`__numpy_ufunc__` | ||
determines the result. If all of the :func:`__array_ufunc__` | ||
operations return :obj:`NotImplemented`, a :exc:`TypeError` is | ||
raised. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we only check for |
||
|
||
If an :class:`ndarray` subclass defines the :func:`__numpy_ufunc__` | ||
If an :class:`ndarray` subclass defines the :func:`__array_ufunc__` | ||
method, this disables the :func:`__array_wrap__`, | ||
:func:`__array_prepare__`, :data:`__array_priority__` mechanism | ||
described below. | ||
|
||
.. note:: In addition to ufuncs, :func:`__numpy_ufunc__` also | ||
.. note:: In addition to ufuncs, :func:`__array_ufunc__` also | ||
overrides the behavior of :func:`numpy.dot` even though it is | ||
not an Ufunc. | ||
|
||
.. note:: If you also define right-hand binary operator override | ||
methods (such as ``__rmul__``) or comparison operations (such as | ||
``__gt__``) in your class, they take precedence over the | ||
:func:`__numpy_ufunc__` mechanism when resolving results of | ||
:func:`__array_ufunc__` mechanism when resolving results of | ||
binary operations (such as ``ndarray_obj * your_obj``). | ||
|
||
The technical special case is: ``ndarray.__mul__`` returns | ||
``NotImplemented`` if the other object is *not* a subclass of | ||
:class:`ndarray`, and defines both ``__numpy_ufunc__`` and | ||
:class:`ndarray`, and defines both ``__array_ufunc__`` and | ||
``__rmul__``. Similar exception applies for the other operations | ||
than multiplication. | ||
|
||
In such a case, when computing a binary operation such as | ||
``ndarray_obj * your_obj``, your ``__numpy_ufunc__`` method | ||
``ndarray_obj * your_obj``, your ``__array_ufunc__`` method | ||
*will not* be called. Instead, the execution passes on to your | ||
right-hand ``__rmul__`` operation, as per standard Python | ||
operator override rules. | ||
|
||
Similar special case applies to *in-place operations*: If you | ||
define ``__rmul__``, then ``ndarray_obj *= your_obj`` *will not* | ||
call your ``__numpy_ufunc__`` implementation. Instead, the | ||
call your ``__array_ufunc__`` implementation. Instead, the | ||
default Python behavior ``ndarray_obj = ndarray_obj * your_obj`` | ||
occurs. | ||
|
||
Note that the above discussion applies only to Python's builtin | ||
binary operation mechanism. ``np.multiply(ndarray_obj, | ||
your_obj)`` always calls only your ``__numpy_ufunc__``, as | ||
your_obj)`` always calls only your ``__array_ufunc__``, as | ||
expected. | ||
|
||
.. method:: class.__array_finalize__(obj) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is definitely not right :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, https://github.com/charris/numpy/pull/6/files#diff-38494a4fd388b5a497f4082aabd236e7 switches it to 1.13 though.