Skip to content

Add complex number support to sign #556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions spec/API_specification/array_api/elementwise_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1351,19 +1351,38 @@ def round(x: array, /) -> array:
"""

def sign(x: array, /) -> array:
"""
r"""
Returns an indication of the sign of a number for each element ``x_i`` of the input array ``x``.

The sign function (also known as the **signum function**) of a number :math:`x_i` is defined as

.. math::
\operatorname{sign}(x_i) = \begin{cases}
0 & \textrm{if } x_i = 0 \\
\frac{x}{|x|} & \textrm{otherwise}
\end{cases}

where :math:`|x_i|` is the absolute value of :math:`x_i`.

**Special cases**

For real-valued operands,

- If ``x_i`` is less than ``0``, the result is ``-1``.
- If ``x_i`` is either ``-0`` or ``+0``, the result is ``0``.
- If ``x_i`` is greater than ``0``, the result is ``+1``.
- If ``x_i`` is ``NaN``, the result is ``NaN``.

For complex floating-point operands, let ``a = real(x_i)``, ``b = imag(x_i)``, and

- If ``a`` is either ``-0`` or ``+0`` and ``b`` is either ``-0`` or ``+0``, the result is ``0 + 0j``.
- If ``a`` is ``NaN`` or ``b`` is ``NaN``, the result is ``NaN + NaN j``.
- In the remaining cases, special cases must be handled according to the rules of complex number division (see :func:`~array_api.divide`).

Parameters
----------
x: array
input array. Should have a real-valued data type.
input array. Should have a numeric data type.

Returns
-------
Expand Down