Skip to content

Commit 6e320d0

Browse files
authored
Add clip to the specification
PR-URL: #715
1 parent 7a56675 commit 6e320d0

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

spec/draft/API_specification/elementwise_functions.rst

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Objects in API
3333
bitwise_right_shift
3434
bitwise_xor
3535
ceil
36+
clip
3637
conj
3738
copysign
3839
cos

src/array_api_stubs/_draft/elementwise_functions.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"bitwise_right_shift",
1616
"bitwise_xor",
1717
"ceil",
18+
"clip",
1819
"conj",
1920
"copysign",
2021
"cos",
@@ -65,7 +66,7 @@
6566
]
6667

6768

68-
from ._types import array
69+
from ._types import Optional, Union, array
6970

7071

7172
def abs(x: array, /) -> array:
@@ -775,6 +776,38 @@ def ceil(x: array, /) -> array:
775776
"""
776777

777778

779+
def clip(
780+
x: array,
781+
/,
782+
min: Optional[Union[int, float, array]] = None,
783+
max: Optional[Union[int, float, array]] = None,
784+
) -> array:
785+
r"""
786+
Clamps each element ``x_i`` of the input array ``x`` to the range ``[min, max]``.
787+
788+
Parameters
789+
----------
790+
x: array
791+
input array. Should have a real-valued data type.
792+
min: Optional[Union[int, float, array]]
793+
lower-bound of the range to which to clamp. If ``None``, no lower bound must be applied. Must be compatible with ``x1`` (see :ref:`broadcasting`). Should have a real-valued data type. Default: ``None``.
794+
max: Optional[Union[int, float, array]]
795+
upper-bound of the range to which to clamp. If ``None``, no upper bound must be applied. Must be compatible with ``x1`` (see :ref:`broadcasting`). Should have a real-valued data type. Default: ``None``.
796+
797+
Returns
798+
-------
799+
out: array
800+
an array containing element-wise results. The returned array must have the same data type as ``x``.
801+
802+
Notes
803+
-----
804+
805+
- If both ``min`` and ``max`` are ``None``, the elements of the returned array must equal the respective elements in ``x``.
806+
- If a broadcasted element in ``min`` is greater than a corresponding broadcasted element in ``max``, behavior is unspecified and thus implementation-dependent.
807+
- If ``x`` and either ``min`` or ``max`` have different data type kinds (e.g., integer versus floating-point), behavior is unspecified and thus implementation-dependent.
808+
"""
809+
810+
778811
def conj(x: array, /) -> array:
779812
"""
780813
Returns the complex conjugate for each element ``x_i`` of the input array ``x``.

0 commit comments

Comments
 (0)