Skip to content

Commit cc5b5af

Browse files
authored
Add complex number support to sqrt (#461)
* Add complex number support to `sqrt` * Fix missing parenthesis * Move definition of branch cut to definition list * Update description and reorder notes * Update intervals * Add warning concerning provisional status * Fix formatting * Create separate document describing branch cuts and specification conventions * Convert text to italtic * Fix markup * Fix nested directive * Update copy
1 parent 0184f1c commit cc5b5af

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

Diff for: spec/API_specification/array_api/elementwise_functions.py

+32-5
Original file line numberDiff line numberDiff line change
@@ -1482,28 +1482,55 @@ def square(x: array, /) -> array:
14821482
"""
14831483

14841484
def sqrt(x: array, /) -> array:
1485-
"""
1486-
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).
1485+
r"""
1486+
Calculates the principal square root for each element ``x_i`` of the input array ``x``.
1487+
1488+
.. note::
1489+
After rounding, each result must be indistinguishable from the infinitely precise result (as required by IEEE 754).
14871490
14881491
**Special cases**
14891492
1490-
For floating-point operands,
1493+
For real-valued floating-point operands,
14911494
14921495
- If ``x_i`` is ``NaN``, the result is ``NaN``.
14931496
- If ``x_i`` is less than ``0``, the result is ``NaN``.
14941497
- If ``x_i`` is ``+0``, the result is ``+0``.
14951498
- If ``x_i`` is ``-0``, the result is ``-0``.
14961499
- If ``x_i`` is ``+infinity``, the result is ``+infinity``.
14971500
1501+
For complex floating-point operands, let ``a = real(x_i)``, ``b = imag(x_i)``, and
1502+
1503+
- If ``a`` is either ``+0`` or ``-0`` and ``b`` is ``+0``, the result is ``+0 + 0j``.
1504+
- If ``a`` is any value (including ``NaN``) and ``b`` is ``+infinity``, the result is ``+infinity + infinity j``.
1505+
- If ``a`` is a finite number and ``b`` is ``NaN``, the result is ``NaN + NaN j``.
1506+
- If ``a`` ``-infinity`` and ``b`` is a positive (i.e., greater than ``0``) finite number, the result is ``NaN + NaN j``.
1507+
- If ``a`` is ``+infinity`` and ``b`` is a positive (i.e., greater than ``0``) finite number, the result is ``+0 + infinity j``.
1508+
- If ``a`` is ``-infinity`` and ``b`` is ``NaN``, the result is ``NaN + infinity j`` (sign of the imaginary component is unspecified).
1509+
- If ``a`` is ``+infinity`` and ``b`` is ``NaN``, the result is ``+infinity + NaN j``.
1510+
- If ``a`` is ``NaN`` and ``b`` is any value, the result is ``NaN + NaN j``.
1511+
- If ``a`` is ``NaN`` and ``b`` is ``NaN``, the result is ``NaN + NaN j``.
1512+
1513+
.. note::
1514+
For complex floating-point operands, ``sqrt(conj(x))`` must equal ``conj(sqrt(x))``.
1515+
1516+
.. note::
1517+
By convention, the branch cut of the square root is the negative real axis :math:`(-\infty, 0)`.
1518+
1519+
The square root is a continuous function from above the branch cut, taking into account the sign of the imaginary component.
1520+
1521+
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).
1522+
1523+
*Note: branch cuts have provisional status* (see :ref:`branch-cuts`).
1524+
14981525
Parameters
14991526
----------
15001527
x: array
1501-
input array. Should have a real-valued floating-point data type.
1528+
input array. Should have a floating-point data type.
15021529
15031530
Returns
15041531
-------
15051532
out: array
1506-
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`.
1533+
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`.
15071534
"""
15081535

15091536
def subtract(x1: array, x2: array, /) -> array:

Diff for: spec/design_topics/branch_cuts.rst

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.. _branch-cuts:
2+
3+
Branch Cuts
4+
===========
5+
6+
In the mathematical field of complex analysis, a **branch cut** is a curve in the complex plane across which an analytic multi-valued function is discontinuous. Branch cuts are often taken as lines or line segments, and the choice of any particular branch cut is a matter of convention.
7+
8+
For example, consider the function :math:`z^2` which maps a complex number :math:`z` to a well-defined number :math:`z^2`. The function's inverse function :math:`\sqrt{z}` does not, however, map to a single value. For example, for :math:`z = 1`, :math:`\sqrt{1} = \pm 1`. While one can choose a unique principal value for this and similar functions (e.g., in this case, the principal square root is :math:`+1`), choices cannot be made continuous over the whole complex plane, as lines of discontinuity must occur. To handle discontinuities, one commonly adopts branch cuts, which are not, in general, unique. Instead, one chooses a branch cut as a matter of convention in order to give simple analytic properties.
9+
10+
Branch cuts do not arise for single-valued trigonometric, hyperbolic, integer power, or exponential functions; however, branch cuts do arise for their multi-valued inverses.
11+
12+
In contrast to real-valued floating-point numbers which have well-defined behavior as specified in IEEE 754, complex-valued floating-point numbers have no equivalent specification. Accordingly, this specification chooses to follow C99 conventions for special cases and branch cuts for those functions supporting complex numbers. For those functions which do not have C99 equivalents (e.g., linear algebra APIs), the specification relies on dominant conventions among existing array libraries.
13+
14+
.. warning::
15+
All branch cuts documented in this specification are 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.
16+
17+
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.

Diff for: spec/design_topics/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ Design topics & constraints
1111
device_support
1212
static_typing
1313
accuracy
14+
branch_cuts
1415
C_API
1516
parallelism

Diff for: spec/purpose_and_scope.md

+3
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,9 @@ a (usually fixed-size) multidimensional container of items of the same type and
434434
**axis**:
435435
an array dimension.
436436

437+
**branch cut**:
438+
a curve in the complex plane across which a given complex function fails to be continuous.
439+
437440
**broadcast**:
438441
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.
439442

0 commit comments

Comments
 (0)