From 02600d38f3b2e70c3cd07770f93c3bac5255c8a6 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Fri, 21 Apr 2017 09:35:45 -0700 Subject: ENH: Add NDArrayOperatorsMixin mixin class. This mixin class provides an easy way to implement arithmetic operators that defer to __array_ufunc__ like numpy.ndarray in non-ndarray subclasses. --- numpy/lib/mixins.py | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 numpy/lib/mixins.py (limited to 'numpy/lib/mixins.py') diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py new file mode 100644 index 000000000..877a11039 --- /dev/null +++ b/numpy/lib/mixins.py @@ -0,0 +1,167 @@ +"""Mixin classes for custom array types that don't inherit from ndarray.""" +from __future__ import division, absolute_import, print_function + +import sys + +from numpy.core import umath as um + +# None of this module should be exposed in top-level NumPy module. +__all__ = [] + + +def _binary_method(ufunc): + def func(self, other): + try: + if other.__array_ufunc__ is None: + return NotImplemented + except AttributeError: + pass + return self.__array_ufunc__(ufunc, '__call__', self, other) + return func + + +def _reflected_binary_method(ufunc): + def func(self, other): + try: + if other.__array_ufunc__ is None: + return NotImplemented + except AttributeError: + pass + return self.__array_ufunc__(ufunc, '__call__', other, self) + return func + + +def _inplace_binary_method(ufunc): + def func(self, other): + result = self.__array_ufunc__( + ufunc, '__call__', self, other, out=(self,)) + if result is NotImplemented: + raise TypeError('unsupported operand types for in-place ' + 'arithmetic: %s and %s' + % (type(self).__name__, type(other).__name__)) + return result + return func + + +def _numeric_methods(ufunc): + return (_binary_method(ufunc), + _reflected_binary_method(ufunc), + _inplace_binary_method(ufunc)) + + +def _unary_method(ufunc): + def func(self): + return self.__array_ufunc__(ufunc, '__call__', self) + return func + + +class NDArrayOperatorsMixin(object): + """Mixin defining all operator special methods using __array_ufunc__. + + This class implements the special methods for almost all of Python's + builtin operators defined in the `operator` module, including comparisons + (``==``, ``>``, etc.) and arithmetic (``+``, ``*``, ``-``, etc.), by + deferring to the ``__array_ufunc__`` method, which subclasses must + implement. + + This class does not yet implement the special operators corresponding + to ``divmod``, unary ``+`` or ``matmul`` (``@``), because these operation + do not yet have corresponding NumPy ufuncs. + + It is useful for writing classes that do not inherit from `numpy.ndarray`, + but that should support arithmetic and numpy universal functions like + arrays as described in :ref:`A Mechanism for Overriding Ufuncs + `. + + As an trivial example, consider this implementation of an ``ArrayLike`` + class that simply wraps a NumPy array and ensures that the result of any + arithmetic operation is also an ``ArrayLike`` object:: + + class ArrayLike(np.lib.mixins.NDArrayOperatorsMixin): + def __init__(self, value): + self.value = np.asarray(value) + + # One might also consider adding the built-in list type to this + # list, to support operations like np.add(array_like, list) + _HANDLED_TYPES = (np.ndarray, numbers.Number) + + def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): + out = kwargs.get('out', ()) + for x in inputs + out: + # Only support operations with instances of _HANDLED_TYPES + # and superclass instances of this type + if not (isinstance(x, self._HANDLED_TYPES) or + isinstance(self, type(x))): + return NotImplemented + + # Defer to the implementation of the ufunc on unwrapped values + inputs = tuple(x.value if isinstance(self, type(x)) else x + for x in inputs) + if out: + kwargs['out'] = tuple( + x.value if isinstance(self, type(x)) else x + for x in out) + result = getattr(ufunc, method)(*inputs, **kwargs) + + if type(result) is tuple: + # multiple return values + return tuple(type(self)(x) for x in result) + elif method == 'at': + # no return value + return None + else: + # one return value + return type(self)(result) + + def __repr__(self): + return '%s(%r)' % (type(self).__name__, self.value) + + In interactions between ``ArrayLike`` objects and numbers or numpy arrays, + the result is always another ``ArrayLike``: + + >>> x = ArrayLike([1, 2, 3]) + >>> x - 1 + ArrayLike(array([0, 1, 2])) + >>> 1 - x + ArrayLike(array([ 0, -1, -2])) + >>> np.arange(3) - x + ArrayLike(array([-1, -1, -1])) + >>> x - np.arange(3) + ArrayLike(array([1, 1, 1])) + + Note that unlike ``numpy.ndarray``, ``ArrayLike`` does not allow operations + with arbitrary, unrecognized types. This ensures that interactions with + ArrayLike preserve a well-defined casting hierarchy. + """ + + # comparisons don't have reflected and in-place versions + __lt__ = _binary_method(um.less) + __le__ = _binary_method(um.less_equal) + __eq__ = _binary_method(um.equal) + __ne__ = _binary_method(um.not_equal) + __gt__ = _binary_method(um.greater) + __ge__ = _binary_method(um.greater_equal) + + # numeric methods + __add__, __radd__, __iadd__ = _numeric_methods(um.add) + __sub__, __rsub__, __isub__ = _numeric_methods(um.subtract) + __mul__, __rmul__, __imul__ = _numeric_methods(um.multiply) + if sys.version_info.major < 3: + # Python 3 uses only __truediv__ and __floordiv__ + __div__, __rdiv__, __idiv__ = _numeric_methods(um.divide) + __truediv__, __rtruediv__, __itruediv__ = _numeric_methods(um.true_divide) + __floordiv__, __rfloordiv__, __ifloordiv__ = _numeric_methods( + um.floor_divide) + __mod__, __rmod__, __imod__ = _numeric_methods(um.mod) + # TODO: handle the optional third argument for __pow__? + __pow__, __rpow__, __ipow__ = _numeric_methods(um.power) + __lshift__, __rlshift__, __ilshift__ = _numeric_methods(um.left_shift) + __rshift__, __rrshift__, __irshift__ = _numeric_methods(um.right_shift) + __and__, __rand__, __iand__ = _numeric_methods(um.bitwise_and) + __xor__, __rxor__, __ixor__ = _numeric_methods(um.bitwise_xor) + __or__, __ror__, __ior__ = _numeric_methods(um.bitwise_or) + + # unary methods + __neg__ = _unary_method(um.negative) + __abs__ = _unary_method(um.absolute) + __invert__ = _unary_method(um.invert) -- cgit v1.2.1 From 256a8ae75fc36f7d4531557f9572a046508afa07 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Sat, 22 Apr 2017 18:01:35 -0700 Subject: BUG: Fix ArrayLike(NDArrayOperatorsMixin) operations with object() --- numpy/lib/mixins.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'numpy/lib/mixins.py') diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py index 877a11039..2deb58827 100644 --- a/numpy/lib/mixins.py +++ b/numpy/lib/mixins.py @@ -88,18 +88,23 @@ class NDArrayOperatorsMixin(object): def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): out = kwargs.get('out', ()) for x in inputs + out: - # Only support operations with instances of _HANDLED_TYPES - # and superclass instances of this type + # Only support operations with instances of _HANDLED_TYPES, + # or instances of ArrayLike that are superclasses of this + # object's type. if not (isinstance(x, self._HANDLED_TYPES) or - isinstance(self, type(x))): + (isinstance(x, ArrayLike) and + isinstance(self, type(x)))): return NotImplemented - # Defer to the implementation of the ufunc on unwrapped values - inputs = tuple(x.value if isinstance(self, type(x)) else x + # Defer to the implementation of the ufunc on unwrapped values. + # Use ArrayLike instead of type(self) for isinstance to allow + # subclasses that don't override __array_ufunc__ to handle + # ArrayLike objects. + inputs = tuple(x.value if isinstance(x, ArrayLike) else x for x in inputs) if out: kwargs['out'] = tuple( - x.value if isinstance(self, type(x)) else x + x.value if isinstance(x, ArrayLike) else x for x in out) result = getattr(ufunc, method)(*inputs, **kwargs) -- cgit v1.2.1 From 32221dfb553980e34a398c71891c7dcdfaf2f477 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Thu, 27 Apr 2017 12:17:06 -0700 Subject: ENH: NDArrayOperatorsMixin calls ufuncs directly, like ndarray * ENH: NDArrayOperatorsMixin calls ufuncs directly, like ndarray Per our discussion in https://github.com/numpy/numpy/pull/8247#discussion_r112825050 * add back in accidentally dropped __repr__ --- numpy/lib/mixins.py | 59 ++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) (limited to 'numpy/lib/mixins.py') diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py index 2deb58827..21e4b346f 100644 --- a/numpy/lib/mixins.py +++ b/numpy/lib/mixins.py @@ -5,53 +5,54 @@ import sys from numpy.core import umath as um -# None of this module should be exposed in top-level NumPy module. +# Nothing should be exposed in the top-level NumPy module. __all__ = [] +def _disables_array_ufunc(obj): + """True when __array_ufunc__ is set to None.""" + try: + return obj.__array_ufunc__ is None + except AttributeError: + return False + + def _binary_method(ufunc): + """Implement a forward binary method with a ufunc, e.g., __add__.""" def func(self, other): - try: - if other.__array_ufunc__ is None: - return NotImplemented - except AttributeError: - pass - return self.__array_ufunc__(ufunc, '__call__', self, other) + if _disables_array_ufunc(other): + return NotImplemented + return ufunc(self, other) return func def _reflected_binary_method(ufunc): + """Implement a reflected binary method with a ufunc, e.g., __radd__.""" def func(self, other): - try: - if other.__array_ufunc__ is None: - return NotImplemented - except AttributeError: - pass - return self.__array_ufunc__(ufunc, '__call__', other, self) + if _disables_array_ufunc(other): + return NotImplemented + return ufunc(other, self) return func def _inplace_binary_method(ufunc): + """Implement an in-place binary method with a ufunc, e.g., __iadd__.""" def func(self, other): - result = self.__array_ufunc__( - ufunc, '__call__', self, other, out=(self,)) - if result is NotImplemented: - raise TypeError('unsupported operand types for in-place ' - 'arithmetic: %s and %s' - % (type(self).__name__, type(other).__name__)) - return result + return ufunc(self, other, out=(self,)) return func def _numeric_methods(ufunc): + """Implement forward, reflected and inplace binary methods with a ufunc.""" return (_binary_method(ufunc), _reflected_binary_method(ufunc), _inplace_binary_method(ufunc)) def _unary_method(ufunc): + """Implement a unary special method with a ufunc.""" def func(self): - return self.__array_ufunc__(ufunc, '__call__', self) + return ufunc(self) return func @@ -88,18 +89,14 @@ class NDArrayOperatorsMixin(object): def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): out = kwargs.get('out', ()) for x in inputs + out: - # Only support operations with instances of _HANDLED_TYPES, - # or instances of ArrayLike that are superclasses of this - # object's type. - if not (isinstance(x, self._HANDLED_TYPES) or - (isinstance(x, ArrayLike) and - isinstance(self, type(x)))): + # Only support operations with instances of _HANDLED_TYPES. + # Use ArrayLike instead of type(self) for isinstance to + # allow subclasses that don't override __array_ufunc__ to + # handle ArrayLike objects. + if not isinstance(x, self._HANDLED_TYPES + (ArrayLike,)): return NotImplemented # Defer to the implementation of the ufunc on unwrapped values. - # Use ArrayLike instead of type(self) for isinstance to allow - # subclasses that don't override __array_ufunc__ to handle - # ArrayLike objects. inputs = tuple(x.value if isinstance(x, ArrayLike) else x for x in inputs) if out: @@ -138,6 +135,8 @@ class NDArrayOperatorsMixin(object): with arbitrary, unrecognized types. This ensures that interactions with ArrayLike preserve a well-defined casting hierarchy. """ + # Like np.ndarray, this mixin class implements "Option 1" from the ufunc + # overrides NEP. # comparisons don't have reflected and in-place versions __lt__ = _binary_method(um.less) -- cgit v1.2.1 From e8c387cb8a105b9209474b6abf5c8feb0e92d830 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Thu, 27 Apr 2017 22:39:09 +0100 Subject: MAINT: Set the __name__ of generated methods Barely useful, but means that the function name is helpful if printed. --- numpy/lib/mixins.py | 67 +++++++++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 30 deletions(-) (limited to 'numpy/lib/mixins.py') diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py index 21e4b346f..b5231e372 100644 --- a/numpy/lib/mixins.py +++ b/numpy/lib/mixins.py @@ -17,42 +17,46 @@ def _disables_array_ufunc(obj): return False -def _binary_method(ufunc): +def _binary_method(ufunc, name): """Implement a forward binary method with a ufunc, e.g., __add__.""" def func(self, other): if _disables_array_ufunc(other): return NotImplemented return ufunc(self, other) + func.__name__ = '__{}__'.format(name) return func -def _reflected_binary_method(ufunc): +def _reflected_binary_method(ufunc, name): """Implement a reflected binary method with a ufunc, e.g., __radd__.""" def func(self, other): if _disables_array_ufunc(other): return NotImplemented return ufunc(other, self) + func.__name__ = '__r{}__'.format(name) return func -def _inplace_binary_method(ufunc): +def _inplace_binary_method(ufunc, name): """Implement an in-place binary method with a ufunc, e.g., __iadd__.""" def func(self, other): return ufunc(self, other, out=(self,)) + func.__name__ = '__i{}__'.format(name) return func -def _numeric_methods(ufunc): +def _numeric_methods(ufunc, name): """Implement forward, reflected and inplace binary methods with a ufunc.""" - return (_binary_method(ufunc), - _reflected_binary_method(ufunc), - _inplace_binary_method(ufunc)) + return (_binary_method(ufunc, name), + _reflected_binary_method(ufunc, name), + _inplace_binary_method(ufunc, name)) -def _unary_method(ufunc): +def _unary_method(ufunc, name): """Implement a unary special method with a ufunc.""" def func(self): return ufunc(self) + func.__name__ = '__{}__'.format(name) return func @@ -139,33 +143,36 @@ class NDArrayOperatorsMixin(object): # overrides NEP. # comparisons don't have reflected and in-place versions - __lt__ = _binary_method(um.less) - __le__ = _binary_method(um.less_equal) - __eq__ = _binary_method(um.equal) - __ne__ = _binary_method(um.not_equal) - __gt__ = _binary_method(um.greater) - __ge__ = _binary_method(um.greater_equal) + __lt__ = _binary_method(um.less, 'lt') + __le__ = _binary_method(um.less_equal, 'le') + __eq__ = _binary_method(um.equal, 'eq') + __ne__ = _binary_method(um.not_equal, 'ne') + __gt__ = _binary_method(um.greater, 'gt') + __ge__ = _binary_method(um.greater_equal, 'ge') # numeric methods - __add__, __radd__, __iadd__ = _numeric_methods(um.add) - __sub__, __rsub__, __isub__ = _numeric_methods(um.subtract) - __mul__, __rmul__, __imul__ = _numeric_methods(um.multiply) + __add__, __radd__, __iadd__ = _numeric_methods(um.add, 'add') + __sub__, __rsub__, __isub__ = _numeric_methods(um.subtract, 'sub') + __mul__, __rmul__, __imul__ = _numeric_methods(um.multiply, 'mul') if sys.version_info.major < 3: # Python 3 uses only __truediv__ and __floordiv__ - __div__, __rdiv__, __idiv__ = _numeric_methods(um.divide) - __truediv__, __rtruediv__, __itruediv__ = _numeric_methods(um.true_divide) + __div__, __rdiv__, __idiv__ = _numeric_methods(um.divide, 'div') + __truediv__, __rtruediv__, __itruediv__ = _numeric_methods( + um.true_divide, 'truediv') __floordiv__, __rfloordiv__, __ifloordiv__ = _numeric_methods( - um.floor_divide) - __mod__, __rmod__, __imod__ = _numeric_methods(um.mod) + um.floor_divide, 'floordiv') + __mod__, __rmod__, __imod__ = _numeric_methods(um.mod, 'mod') # TODO: handle the optional third argument for __pow__? - __pow__, __rpow__, __ipow__ = _numeric_methods(um.power) - __lshift__, __rlshift__, __ilshift__ = _numeric_methods(um.left_shift) - __rshift__, __rrshift__, __irshift__ = _numeric_methods(um.right_shift) - __and__, __rand__, __iand__ = _numeric_methods(um.bitwise_and) - __xor__, __rxor__, __ixor__ = _numeric_methods(um.bitwise_xor) - __or__, __ror__, __ior__ = _numeric_methods(um.bitwise_or) + __pow__, __rpow__, __ipow__ = _numeric_methods(um.power, 'pow') + __lshift__, __rlshift__, __ilshift__ = _numeric_methods( + um.left_shift, 'lshift') + __rshift__, __rrshift__, __irshift__ = _numeric_methods( + um.right_shift, 'rshift') + __and__, __rand__, __iand__ = _numeric_methods(um.bitwise_and, 'and') + __xor__, __rxor__, __ixor__ = _numeric_methods(um.bitwise_xor, 'xor') + __or__, __ror__, __ior__ = _numeric_methods(um.bitwise_or, 'or') # unary methods - __neg__ = _unary_method(um.negative) - __abs__ = _unary_method(um.absolute) - __invert__ = _unary_method(um.invert) + __neg__ = _unary_method(um.negative, 'neg') + __abs__ = _unary_method(um.absolute, 'abs') + __invert__ = _unary_method(um.invert, 'invert') -- cgit v1.2.1 From e799be5f6a1bb2e0a294a1b0f03a1dc5333f529b Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Sun, 30 Apr 2017 21:33:21 -0700 Subject: ENH: add __pos__ to NDArrayOperatorsMixin --- numpy/lib/mixins.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'numpy/lib/mixins.py') diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py index b5231e372..bbeed1437 100644 --- a/numpy/lib/mixins.py +++ b/numpy/lib/mixins.py @@ -70,8 +70,8 @@ class NDArrayOperatorsMixin(object): implement. This class does not yet implement the special operators corresponding - to ``divmod``, unary ``+`` or ``matmul`` (``@``), because these operation - do not yet have corresponding NumPy ufuncs. + to ``divmod`` or ``matmul`` (``@``), because these operation do not yet + have corresponding NumPy ufuncs. It is useful for writing classes that do not inherit from `numpy.ndarray`, but that should support arithmetic and numpy universal functions like @@ -174,5 +174,6 @@ class NDArrayOperatorsMixin(object): # unary methods __neg__ = _unary_method(um.negative, 'neg') + __pos__ = _unary_method(um.positive, 'pos') __abs__ = _unary_method(um.absolute, 'abs') __invert__ = _unary_method(um.invert, 'invert') -- cgit v1.2.1 From d51b538ba80d36841cc57911d77ea61cd1d3fb25 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Fri, 5 May 2017 21:50:18 -0700 Subject: ENH: add divmod support to NDArrayOperatorsMixin --- numpy/lib/mixins.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'numpy/lib/mixins.py') diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py index bbeed1437..fbdc2edfb 100644 --- a/numpy/lib/mixins.py +++ b/numpy/lib/mixins.py @@ -70,8 +70,7 @@ class NDArrayOperatorsMixin(object): implement. This class does not yet implement the special operators corresponding - to ``divmod`` or ``matmul`` (``@``), because these operation do not yet - have corresponding NumPy ufuncs. + to ``matmul`` (``@``), because ``np.matmul`` is not yet a NumPy ufunc. It is useful for writing classes that do not inherit from `numpy.ndarray`, but that should support arithmetic and numpy universal functions like @@ -161,7 +160,10 @@ class NDArrayOperatorsMixin(object): um.true_divide, 'truediv') __floordiv__, __rfloordiv__, __ifloordiv__ = _numeric_methods( um.floor_divide, 'floordiv') - __mod__, __rmod__, __imod__ = _numeric_methods(um.mod, 'mod') + __mod__, __rmod__, __imod__ = _numeric_methods(um.remainder, 'mod') + __divmod__ = _binary_method(um.divmod, 'divmod') + __rdivmod__ = _reflected_binary_method(um.divmod, 'divmod') + # __idivmod__ does not exist # TODO: handle the optional third argument for __pow__? __pow__, __rpow__, __ipow__ = _numeric_methods(um.power, 'pow') __lshift__, __rlshift__, __ilshift__ = _numeric_methods( -- cgit v1.2.1 From 59fa7d3521dc9f27e70df7966ff1c8bc1c5d34a7 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Sun, 4 Mar 2018 16:13:56 -0800 Subject: DOC: add versionadded for NDArrayOperatorsMixin. --- numpy/lib/mixins.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'numpy/lib/mixins.py') diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py index fbdc2edfb..3220f6534 100644 --- a/numpy/lib/mixins.py +++ b/numpy/lib/mixins.py @@ -137,6 +137,8 @@ class NDArrayOperatorsMixin(object): Note that unlike ``numpy.ndarray``, ``ArrayLike`` does not allow operations with arbitrary, unrecognized types. This ensures that interactions with ArrayLike preserve a well-defined casting hierarchy. + + .. versionadded:: 1.13 """ # Like np.ndarray, this mixin class implements "Option 1" from the ufunc # overrides NEP. -- cgit v1.2.1 From df8e83538461c29bc12c44198574bde8ffefcad7 Mon Sep 17 00:00:00 2001 From: mattip Date: Tue, 17 Apr 2018 13:46:36 +0300 Subject: DOC: clear up warnings, fix matplotlib plot --- numpy/lib/mixins.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'numpy/lib/mixins.py') diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py index 3220f6534..0379ecb1a 100644 --- a/numpy/lib/mixins.py +++ b/numpy/lib/mixins.py @@ -74,8 +74,8 @@ class NDArrayOperatorsMixin(object): It is useful for writing classes that do not inherit from `numpy.ndarray`, but that should support arithmetic and numpy universal functions like - arrays as described in :ref:`A Mechanism for Overriding Ufuncs - `. + arrays as described in `A Mechanism for Overriding Ufuncs + <../../neps/nep-0013-ufunc-overrides.html>`_. As an trivial example, consider this implementation of an ``ArrayLike`` class that simply wraps a NumPy array and ensures that the result of any -- cgit v1.2.1 From 8a115731966db1d723d03b4b599a5f3587edc94b Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Wed, 5 Dec 2018 10:01:47 -0800 Subject: ENH: implement matmul on NDArrayOperatorsMixin (#12488) * ENH: implement matmul on NDArrayOperatorsMixin * MAINT: remove unnecessary pytest import --- numpy/lib/mixins.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'numpy/lib/mixins.py') diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py index 0379ecb1a..52ad45b68 100644 --- a/numpy/lib/mixins.py +++ b/numpy/lib/mixins.py @@ -69,9 +69,6 @@ class NDArrayOperatorsMixin(object): deferring to the ``__array_ufunc__`` method, which subclasses must implement. - This class does not yet implement the special operators corresponding - to ``matmul`` (``@``), because ``np.matmul`` is not yet a NumPy ufunc. - It is useful for writing classes that do not inherit from `numpy.ndarray`, but that should support arithmetic and numpy universal functions like arrays as described in `A Mechanism for Overriding Ufuncs @@ -155,6 +152,8 @@ class NDArrayOperatorsMixin(object): __add__, __radd__, __iadd__ = _numeric_methods(um.add, 'add') __sub__, __rsub__, __isub__ = _numeric_methods(um.subtract, 'sub') __mul__, __rmul__, __imul__ = _numeric_methods(um.multiply, 'mul') + __matmul__, __rmatmul__, __imatmul__ = _numeric_methods( + um.matmul, 'matmul') if sys.version_info.major < 3: # Python 3 uses only __truediv__ and __floordiv__ __div__, __rdiv__, __idiv__ = _numeric_methods(um.divide, 'div') -- cgit v1.2.1