diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2021-08-23 21:32:20 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-23 21:32:20 -0600 |
commit | 098f874144161b6a49efa5108846a408ca8f39b8 (patch) | |
tree | d618c32a54705d38e5458669774c88d9f6225212 /numpy/array_api/_searching_functions.py | |
parent | a3ac75c6f92ed158777492f343dc59adeacb745c (diff) | |
parent | 7091e4c48ce7af8a5263b6808a6d7976d4af4c6f (diff) | |
download | numpy-098f874144161b6a49efa5108846a408ca8f39b8.tar.gz |
Merge pull request #18585 from data-apis/array-api
ENH: Implementation of the NEP 47 (adopting the array API standard)
Diffstat (limited to 'numpy/array_api/_searching_functions.py')
-rw-r--r-- | numpy/array_api/_searching_functions.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/numpy/array_api/_searching_functions.py b/numpy/array_api/_searching_functions.py new file mode 100644 index 000000000..3dcef61c3 --- /dev/null +++ b/numpy/array_api/_searching_functions.py @@ -0,0 +1,46 @@ +from __future__ import annotations + +from ._array_object import Array +from ._dtypes import _result_type + +from typing import Optional, Tuple + +import numpy as np + + +def argmax(x: Array, /, *, axis: Optional[int] = None, keepdims: bool = False) -> Array: + """ + Array API compatible wrapper for :py:func:`np.argmax <numpy.argmax>`. + + See its docstring for more information. + """ + return Array._new(np.asarray(np.argmax(x._array, axis=axis, keepdims=keepdims))) + + +def argmin(x: Array, /, *, axis: Optional[int] = None, keepdims: bool = False) -> Array: + """ + Array API compatible wrapper for :py:func:`np.argmin <numpy.argmin>`. + + See its docstring for more information. + """ + return Array._new(np.asarray(np.argmin(x._array, axis=axis, keepdims=keepdims))) + + +def nonzero(x: Array, /) -> Tuple[Array, ...]: + """ + Array API compatible wrapper for :py:func:`np.nonzero <numpy.nonzero>`. + + See its docstring for more information. + """ + return tuple(Array._new(i) for i in np.nonzero(x._array)) + + +def where(condition: Array, x1: Array, x2: Array, /) -> Array: + """ + Array API compatible wrapper for :py:func:`np.where <numpy.where>`. + + See its docstring for more information. + """ + # Call result type here just to raise on disallowed type combinations + _result_type(x1.dtype, x2.dtype) + return Array._new(np.where(condition._array, x1._array, x2._array)) |