summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/lib/mixins.py101
1 files changed, 52 insertions, 49 deletions
diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py
index 08306cb02..52ad45b68 100644
--- a/numpy/lib/mixins.py
+++ b/numpy/lib/mixins.py
@@ -78,55 +78,58 @@ class NDArrayOperatorsMixin(object):
class that simply wraps a NumPy array and ensures that the result of any
arithmetic operation is also an ``ArrayLike`` object::
- >>> import numpy.lib
- >>> import numbers
- >>> 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.
- ... # 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.
- ... inputs = tuple(x.value if isinstance(x, ArrayLike) else x
- ... for x in inputs)
- ... if out:
- ... kwargs['out'] = tuple(
- ... x.value if isinstance(x, ArrayLike) 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]))
+ 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.
+ # 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.
+ inputs = tuple(x.value if isinstance(x, ArrayLike) else x
+ for x in inputs)
+ if out:
+ kwargs['out'] = tuple(
+ x.value if isinstance(x, ArrayLike) 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