MCPcopy
hub / github.com/numpy/numpy / __array_ufunc__

Method __array_ufunc__

numpy/lib/tests/test_mixins.py:18–45  ·  view source on GitHub ↗
(self, ufunc, method, *inputs, **kwargs)

Source from the content-addressed store, hash-verified

16 _HANDLED_TYPES = (np.ndarray, numbers.Number)
17
18 def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
19 out = kwargs.get('out', ())
20 for x in inputs + out:
21 # Only support operations with instances of _HANDLED_TYPES.
22 # Use ArrayLike instead of type(self) for isinstance to
23 # allow subclasses that don't override __array_ufunc__ to
24 # handle ArrayLike objects.
25 if not isinstance(x, self._HANDLED_TYPES + (ArrayLike,)):
26 return NotImplemented
27
28 # Defer to the implementation of the ufunc on unwrapped values.
29 inputs = tuple(x.value if isinstance(x, ArrayLike) else x
30 for x in inputs)
31 if out:
32 kwargs['out'] = tuple(
33 x.value if isinstance(x, ArrayLike) else x
34 for x in out)
35 result = getattr(ufunc, method)(*inputs, **kwargs)
36
37 if type(result) is tuple:
38 # multiple return values
39 return tuple(type(self)(x) for x in result)
40 elif method == 'at':
41 # no return value
42 return None
43 else:
44 # one return value
45 return type(self)(result)
46
47 def __repr__(self):
48 return f'{type(self).__name__}({self.value!r})'

Callers

nothing calls this directly

Calls 1

getMethod · 0.45

Tested by

no test coverage detected