Skip to content

Add complex number support to sqrt #461

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 14 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
40 changes: 35 additions & 5 deletions spec/API_specification/array_api/elementwise_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1458,28 +1458,58 @@ def square(x: array, /) -> array:
"""

def sqrt(x: array, /) -> array:
"""
Calculates the square root, having domain ``[0, +infinity]`` and codomain ``[0, +infinity]``, for each element ``x_i`` of the input array ``x``. After rounding, each result must be indistinguishable from the infinitely precise result (as required by IEEE 754).
r"""
Calculates the principal square root for each element ``x_i`` of the input array ``x``.

.. note::
After rounding, each result must be indistinguishable from the infinitely precise result (as required by IEEE 754).

**Special cases**

For floating-point operands,
For real-valued floating-point operands,

- If ``x_i`` is ``NaN``, the result is ``NaN``.
- If ``x_i`` is less than ``0``, the result is ``NaN``.
- If ``x_i`` is ``+0``, the result is ``+0``.
- If ``x_i`` is ``-0``, the result is ``-0``.
- If ``x_i`` is ``+infinity``, the result is ``+infinity``.

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 ``+0``, the result is ``+0 + 0j``.
- If ``a`` is any value (including ``NaN``) and ``b`` is ``+infinity``, the result is ``+infinity + infinity j``.
- If ``a`` is a finite number and ``b`` is ``NaN``, the result is ``NaN + NaN j``.
- If ``a`` ``-infinity`` and ``b`` is a positive (i.e., greater than ``0``) finite number, the result is ``NaN + NaN j``.
- If ``a`` is ``+infinity`` and ``b`` is a positive (i.e., greater than ``0``) finite number, the result is ``+0 + infinity j``.
- If ``a`` is ``-infinity`` and ``b`` is ``NaN``, the result is ``NaN + infinity j`` (sign of the imaginary component is unspecified).
- If ``a`` is ``+infinity`` and ``b`` is ``NaN``, the result is ``+infinity + NaN j``.
- If ``a`` is ``NaN`` and ``b`` is any value, the result is ``NaN + NaN j``.
- If ``a`` is ``NaN`` and ``b`` is ``NaN``, the result is ``NaN + NaN j``.

.. note::
For complex floating-point operands, ``sqrt(conj(x))`` must equal ``conj(sqrt(x))``.

.. note::
By convention, the branch cut of square root is the negative real axis :math:`(-\infty, 0)`.

The square root is a continuous function from above the branch cut, taking into account the sign of the imaginary component.

Accordingly, for complex arguments, the function returns the square root in the range of the right half-plane, including the imaginary axis (i.e., the plane defined by :math:`[0, +\infty)` along the real axis and :math:`(-\infty, +\infty)` along the imaginary axis).

.. warning::
The choice of the branch cut is considered **provisional**. While conforming implementations of the array API standard should adopt the branch cuts described in this standard, consumers of array API standard implementations should **not** assume that branch cuts are consistent between implementations.

Provided no issues arise due to the choice of branch cut, the provisional status is likely to be removed in a future revision of this standard.

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

Returns
-------
out: array
an array containing the square root of each element in ``x``. The returned array must have a real-valued floating-point data type determined by :ref:`type-promotion`.
an array containing the square root of each element in ``x``. The returned array must have a floating-point data type determined by :ref:`type-promotion`.
"""

def subtract(x1: array, x2: array, /) -> array:
Expand Down
3 changes: 3 additions & 0 deletions spec/purpose_and_scope.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,9 @@ a (usually fixed-size) multidimensional container of items of the same type and
**axis**:
an array dimension.

**branch cut**:
a curve in the complex plane across which a given complex function fails to be continuous.

**broadcast**:
automatic (implicit) expansion of array dimensions to be of equal sizes without copying array data for the purpose of making arrays with different shapes have compatible shapes for element-wise operations.

Expand Down