Skip to content

Commit 03886d6

Browse files
authored
Add support for copysign to the specification
PR-URL: #693
1 parent 8c42f02 commit 03886d6

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Diff for: spec/draft/API_specification/elementwise_functions.rst

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Objects in API
3434
bitwise_xor
3535
ceil
3636
conj
37+
copysign
3738
cos
3839
cosh
3940
divide

Diff for: src/array_api_stubs/_draft/elementwise_functions.py

+42
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"bitwise_xor",
1717
"ceil",
1818
"conj",
19+
"copysign",
1920
"cos",
2021
"cosh",
2122
"divide",
@@ -804,6 +805,47 @@ def conj(x: array, /) -> array:
804805
"""
805806

806807

808+
def copysign(x1: array, x2: array, /) -> array:
809+
r"""
810+
Composes a floating-point value with the magnitude of ``x1_i`` and the sign of ``x2_i`` for each element of the input array ``x1``.
811+
812+
Parameters
813+
----------
814+
x1: array
815+
input array containing magnitudes. Should have a real-valued floating-point data type.
816+
x2: array
817+
input array whose sign bits are applied to the magnitudes of ``x1``. Must be compatible with ``x1`` (see :ref:`broadcasting`). Should have a real-valued floating-point data type.
818+
819+
Returns
820+
-------
821+
out: array
822+
an array containing the element-wise results. The returned array must have a floating-point data type determined by :ref:`type-promotion`.
823+
824+
Notes
825+
-----
826+
827+
**Special cases**
828+
829+
For real-valued floating-point operands, let ``|x|`` be the absolute value, and if ``x1_i`` is not ``NaN``,
830+
831+
- If ``x2_i`` is less than ``0``, the result is ``-|x1_i|``.
832+
- If ``x2_i`` is ``-0``, the result is ``-|x1_i|``.
833+
- If ``x2_i`` is ``+0``, the result is ``|x1_i|``.
834+
- If ``x2_i`` is greater than ``0``, the result is ``|x1_i|``.
835+
- If ``x2_i`` is ``NaN`` and the sign bit of ``x2_i`` is ``1``, the result is ``-|x1_i|``.
836+
- If ``x2_i`` is ``NaN`` and the sign bit of ``x2_i`` is ``0``, the result is ``|x1_i|``.
837+
838+
If ``x1_i`` is ``NaN``,
839+
840+
- If ``x2_i`` is less than ``0``, the result is ``NaN`` with a sign bit of ``1``.
841+
- If ``x2_i`` is ``-0``, the result is ``NaN`` with a sign bit of ``1``.
842+
- If ``x2_i`` is ``+0``, the result is ``NaN`` with a sign bit of ``0``.
843+
- If ``x2_i`` is greater than ``0``, the result is ``NaN`` with a sign bit of ``0``.
844+
- If ``x2_i`` is ``NaN`` and the sign bit of ``x2_i`` is ``1``, the result is ``NaN`` with a sign bit of ``1``.
845+
- If ``x2_i`` is ``NaN`` and the sign bit of ``x2_i`` is ``0``, the result is ``NaN`` with a sign bit of ``0``.
846+
"""
847+
848+
807849
def cos(x: array, /) -> array:
808850
r"""
809851
Calculates an implementation-dependent approximation to the cosine for each element ``x_i`` of the input array ``x``.

0 commit comments

Comments
 (0)