8000 ENH: Add `__array_ufunc__` by charris · Pull Request #8247 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

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 43 commits into from
Apr 27, 2017
Merged
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 Nov 6, 2016
fcd11d2
ENH: Rename __numpy_ufunc__ to __array_ufunc__.
charris Nov 9, 2016
c7b25e2
ENH: Remove position arg from __array_ufunc__.
charris Nov 12, 2016
8a9e790
MAINT: Put PyArray_GetAttrString_SuppressException in get_attr_string.h
njsmith Jun 24, 2015
4dd5380
MAINT: dike out a bunch of weird old code implementing scalar power
njsmith Jun 24, 2015
7d9bc2f
BUG/ENH: Switch to simplified __array_ufunc__/binop interaction
njsmith Jun 22, 2015
e4b5163
MAINT: allow __array_ufunc__ = None to force binops to defer.
mhvk Mar 12, 2017
2e6d8c0
MAINT: Split out C code in ufunc_override.h to .c file.
mhvk Mar 15, 2017
d5c5ac1
MAINT: Add NPY_NO_EXPORT modifier to PyUFunc_CheckOverride.
charris Mar 16, 2017
3124e96
MAINT: for __array_ufunc__ pass inputs as *args, ensure out is tuple.
mhvk Mar 14, 2017
6a3ca31
DOC: describe current implementation of __array_ufunc__.
mhvk Mar 14, 2017
79bb733
DOC: Style and sphinx fixes for arrays.classes.rst.
charris Mar 23, 2017
7c3dc5a
TST: test that gufuncs are also overridden by __array_ufunc__.
mhvk Mar 25, 2017
71201d2
DOC: Describe __array_func__ in subclassing
mhvk Mar 15, 2017
3041710
ENH: implement ndarray.__array_ufunc__
mhvk Mar 13, 2017
5fe6fc6
DOC Update NEP to reflect actual implementation.
mhvk Mar 31, 2017 8000
e092823
MAINT: let ndarray.__array_ufunc__ bail if any overrides are in place.
mhvk Apr 2, 2017
1147894
MAINT: Update array_ufunc NEP.
pv Apr 1, 2017
e325a10
DOC: Document behavior of ufuncs with default ndarray.__array_ufunc__
pv Apr 1, 2017
39c2273
DOC: Update ndarray.__array_ufunc__ documentation vs. review comments
pv Apr 2, 2017
6b41d11
DOC: clarify use of super and getattr
mhvk Apr 2, 2017
0ede0e9
DOC: update NEP again.
mhvk Apr 2, 2017
5f9252c
DOC: implement many smaller and bigger changes suggested in review.
mhvk Apr 4, 2017
8cc2f71
BUG,MAINT: ensure out=None is never passed on to __array_ufunc__.
mhvk Apr 4, 2017
856da73
DOC: remove left-over piece discussing binops
mhvk Apr 5, 2017
2b6c7fd
REVERT: remove __array_ufunc__ override for np.dot and ndarray.dot.
mhvk Apr 6, 2017
36e8494
REVERT: remove __array_ufunc__ override for np.matmul.
mhvk Apr 6, 2017
55500b9
MAINT: simplify now that __array_ufunc__ overrides ufuncs only.
mhvk Apr 6, 2017
25e973d
MAINT: split out umath-specific part of ufunc_override.
mhvk Apr 6, 2017
b1fa10a
BUG: ensure subclass of override class doesn't segfault.
mhvk Apr 8, 2017
1de8f5a
DOC: Mention `__array_ufunc__` in the 1.13.0 release notes.
charris Apr 8, 2017
a460015
DOC: ufunc-overrides: sync the discussion vs. current implementation
pv Apr 9, 2017
cd2e42c
DOC: ufunc-overrides: revise hierarchy discussion
pv Apr 9, 2017
ff628f1
BUG: Add back removed elision code.
charris Apr 9, 2017
1fc6e63
DOC,TST: clarify example of ndarray subclass using __array_ufunc__
mhvk Apr 10, 2017
a431743
BUG: Support nout == 0 and at method
eric-wieser Apr 12, 2017
1e460b7
DOC,MAINT: small corrections to NEP following Stephan's comments.
mhvk Apr 12, 2017
02600d3
ENH: Add NDArrayOperatorsMixin mixin class.
shoyer Apr 21, 2017
d3ff023
DOC: clarify recommendations for subclasses, deprecations.
mhvk Apr 21, 2017
b9359f1
MAINT: remove unnecessary checks, wrong code for 'outer', cleanup.
mhvk Apr 21, 2017
256a8ae
BUG: Fix ArrayLike(NDArrayOperatorsMixin) operations with object()
shoyer Apr 23, 2017
3272a86
ENH: Better error message for __array_ufunc__ not implemented
shoyer Apr 24, 2017
32221df
ENH: NDArrayOperatorsMixin calls ufuncs directly, like ndarray
shoyer Apr 27, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
DOC: Style and sphinx fixes for arrays.classes.rst.
  • Loading branch information
charris committed Apr 27, 2017
commit 79bb7331457bc8e3696935ba3fb29d53791330a6
30 changes: 15 additions & 15 deletions doc/source/reference/arrays.classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ Special attributes and methods

NumPy provides several hooks that classes can customize:

.. method:: class.__array_ufunc__(ufunc, method, *inputs, **kwargs)
.. py:method:: class.__array_ufunc__(ufunc, method, *inputs, **kwargs)

.. versionadded:: 1.13

Any class (ndarray subclass or not) can define this method (or set it to
:obj:`None`) to override behavior of NumPy's ufuncs. This works quite
similarly to Python's ``__mul__`` and other binary operation routines.
Any class, ndarray subclass or not, can define this method or set it to
:obj:`None` in order to override the behavior of NumPy's ufuncs. This works
quite similarly to Python's ``__mul__`` and other binary operation routines.

- *ufunc* is the ufunc object that was called.
- *method* is a string indicating which Ufunc method was called
Expand Down Expand Up @@ -94,17 +94,17 @@ NumPy provides several hooks that classes can customize:

.. note:: If you subclass :class:`ndarray`, we strongly recommend
that you avoid confusion by neither setting
:func:`__array_ufunc__` to :obj:`None` (which seems to make no
sense for an array subclass), nor defining it and also defining
reverse methods (which will be called by ``CPython`` in
preference over the :class:`ndarray` forward methods).
:func:`__array_ufunc__` to :obj:`None`, which makes no
sense for an array subclass, nor by defining it and also defining
reverse methods, which methods will be called by ``CPython`` in
preference over the :class:`ndarray` forward methods.

.. note:: If a class defines the :func:`__array_ufunc__` method,
this disables the :func:`__array_wrap__`,
:func:`__array_prepare__`, :data:`__array_priority__` mechanism
described below (which may eventually be deprecated).
Copy link
Member
@eric-wieser eric-wieser Mar 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unlikely to be true, given that a bunch of non-ufuncs invoke (parts of) this interface. Perhaps clarify to "this disables the ... interface for ufuncs"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in charris#5


.. method:: class.__array_finalize__(obj)
.. py:method:: class.__array_finalize__(obj)

This method is called whenever the system internally allocates a
new array from *obj*, where *obj* is a subclass (subtype) of the
Expand All @@ -113,7 +113,7 @@ NumPy provides several hooks that classes can customize:
to update meta-information from the "parent." Subclasses inherit
a default implementation of this method that does nothing.

.. method:: class.__array_prepare__(array, context=None)
.. py:method:: class.__array_prepare__(array, context=None)

At the beginning of every :ref:`ufunc <ufuncs.output-type>`, this
method is called on the input object with the highest array
Expand All @@ -126,9 +126,9 @@ NumPy provides several hooks that classes can customize:
ufunc for computation.

.. note:: It is hoped to eventually deprecate this method in favour of
:func:__array_ufunc__`.
:func:`__array_ufunc__`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this note apply to __array_wrap__ too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not until we have found a way to substitute the use of __array_wrap__ in a non-ufunc context; I'll clarify.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we use __array_prepare__ in non-ufunc contexts too (albeit a little less often)


.. method:: class.__array_wrap__(array, context=None)
.. py:method:: class.__array_wrap__(array, context=None)

At the end of every :ref:`ufunc <ufuncs.output-type>`, this method
is called on the input object with the highest array priority, or
Expand All @@ -140,17 +140,17 @@ NumPy provides several hooks that classes can customize:
into an instance of the subclass and update metadata before
returning the array to the user.

.. data:: class.__array_priority__
.. py:attribute:: class.__array_priority__

The value of this attribute is used to determine what type of
object to return in situations where there is more than one
possibility for the Python type of the returned object. Subclasses
inherit a default value of 0.0 for this attribute.

.. note:: It is hoped to eventually deprecate this attribute in favour
of :func:__array_ufunc__`.
of :func:`__array_ufunc__`.

.. method:: class.__array__([dtype])
.. py:method:: class.__array__([dtype])

If a class (ndarray subclass or not) having the :func:`__array__`
method is used as the output object of an :ref:`ufunc
Expand Down
0