Skip to content

Commit f6c18b5

Browse files
authored
Add complex number support for pow (#537)
1 parent 770b53b commit f6c18b5

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

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

+19-5
Original file line numberDiff line numberDiff line change
@@ -909,17 +909,19 @@ def __pos__(self: array, /) -> array:
909909
"""
910910

911911
def __pow__(self: array, other: Union[int, float, array], /) -> array:
912-
"""
912+
r"""
913913
Calculates an implementation-dependent approximation of exponentiation by raising each element (the base) of an array instance to the power of ``other_i`` (the exponent), where ``other_i`` is the corresponding element of the array ``other``.
914914
915915
.. note::
916916
If both ``self`` and ``other`` have integer data types, the result of ``__pow__`` when `other_i` is negative (i.e., less than zero) is unspecified and thus implementation-dependent.
917917
918-
If ``self`` has an integer data type and ``other`` has a real-valued floating-point data type, behavior is implementation-dependent, as type promotion between data type "kinds" (e.g., integer versus floating-point) is unspecified.
918+
If ``self`` has an integer data type and ``other`` has a floating-point data type, behavior is implementation-dependent, as type promotion between data type "kinds" (e.g., integer versus floating-point) is unspecified.
919919
920920
**Special cases**
921921
922-
For floating-point operands, let ``self`` equal ``x1`` and ``other`` equal ``x2``.
922+
Let ``self`` equal ``x1`` and ``other`` equal ``x2``.
923+
924+
For real-valued floating-point operands,
923925
924926
- If ``x1_i`` is not equal to ``1`` and ``x2_i`` is ``NaN``, the result is ``NaN``.
925927
- If ``x2_i`` is ``+0``, the result is ``1``, even if ``x1_i`` is ``NaN``.
@@ -946,12 +948,24 @@ def __pow__(self: array, other: Union[int, float, array], /) -> array:
946948
- If ``x1_i`` is ``-0``, ``x2_i`` is less than ``0``, and ``x2_i`` is not an odd integer value, the result is ``+infinity``.
947949
- If ``x1_i`` is less than ``0``, ``x1_i`` is a finite number, ``x2_i`` is a finite number, and ``x2_i`` is not an integer value, the result is ``NaN``.
948950
951+
For complex floating-point operands, special cases should be handled as if the operation is implemented as ``exp(x2*log(x1))``.
952+
953+
.. note::
954+
Conforming implementations are allowed to treat special cases involving complex floating-point operands more carefully than as described in this specification.
955+
956+
.. note::
957+
By convention, the branch cut of the natural logarithm is the negative real axis :math:`(-\infty, 0)`.
958+
959+
The natural logarithm is a continuous function from above the branch cut, taking into account the sign of the imaginary component. As special cases involving complex floating-point operands should be handled according to ``exp(other*log(self))``, exponentiation has the same branch cut for ``self`` as the natural logarithm (see :func:`~array_api.log`).
960+
961+
*Note: branch cuts have provisional status* (see :ref:`branch-cuts`).
962+
949963
Parameters
950964
----------
951965
self: array
952-
array instance whose elements correspond to the exponentiation base. Should have a real-valued data type.
966+
array instance whose elements correspond to the exponentiation base. Should have a numeric data type.
953967
other: Union[int, float, array]
954-
other array whose elements correspond to the exponentiation exponent. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a real-valued data type.
968+
other array whose elements correspond to the exponentiation exponent. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
955969
956970
Returns
957971
-------

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

+17-5
Original file line numberDiff line numberDiff line change
@@ -1551,17 +1551,17 @@ def positive(x: array, /) -> array:
15511551
"""
15521552

15531553
def pow(x1: array, x2: array, /) -> array:
1554-
"""
1554+
r"""
15551555
Calculates an implementation-dependent approximation of exponentiation by raising each element ``x1_i`` (the base) of the input array ``x1`` to the power of ``x2_i`` (the exponent), where ``x2_i`` is the corresponding element of the input array ``x2``.
15561556
15571557
.. note::
15581558
If both ``x1`` and ``x2`` have integer data types, the result of ``pow`` when ``x2_i`` is negative (i.e., less than zero) is unspecified and thus implementation-dependent.
15591559
1560-
If ``x1`` has an integer data type and ``x2`` has a real-valued floating-point data type, behavior is implementation-dependent (type promotion between data type "kinds" (integer versus floating-point) is unspecified).
1560+
If ``x1`` has an integer data type and ``x2`` has a floating-point data type, behavior is implementation-dependent (type promotion between data type "kinds" (integer versus floating-point) is unspecified).
15611561
15621562
**Special cases**
15631563
1564-
For floating-point operands,
1564+
For real-valued floating-point operands,
15651565
15661566
- If ``x1_i`` is not equal to ``1`` and ``x2_i`` is ``NaN``, the result is ``NaN``.
15671567
- If ``x2_i`` is ``+0``, the result is ``1``, even if ``x1_i`` is ``NaN``.
@@ -1588,12 +1588,24 @@ def pow(x1: array, x2: array, /) -> array:
15881588
- If ``x1_i`` is ``-0``, ``x2_i`` is less than ``0``, and ``x2_i`` is not an odd integer value, the result is ``+infinity``.
15891589
- If ``x1_i`` is less than ``0``, ``x1_i`` is a finite number, ``x2_i`` is a finite number, and ``x2_i`` is not an integer value, the result is ``NaN``.
15901590
1591+
For complex floating-point operands, special cases should be handled as if the operation is implemented as ``exp(x2*log(x1))``.
1592+
1593+
.. note::
1594+
Conforming implementations are allowed to treat special cases involving complex floating-point operands more carefully than as described in this specification.
1595+
1596+
.. note::
1597+
By convention, the branch cut of the natural logarithm is the negative real axis :math:`(-\infty, 0)`.
1598+
1599+
The natural logarithm is a continuous function from above the branch cut, taking into account the sign of the imaginary component. As special cases involving complex floating-point operands should be handled according to ``exp(x2*log(x1))``, exponentiation has the same branch cut for ``x1`` as the natural logarithm (see :func:`~array_api.log`).
1600+
1601+
*Note: branch cuts have provisional status* (see :ref:`branch-cuts`).
1602+
15911603
Parameters
15921604
----------
15931605
x1: array
1594-
first input array whose elements correspond to the exponentiation base. Should have a real-valued data type.
1606+
first input array whose elements correspond to the exponentiation base. Should have a numeric data type.
15951607
x2: array
1596-
second input array whose elements correspond to the exponentiation exponent. Must be compatible with ``x1`` (see :ref:`broadcasting`). Should have a real-valued data type.
1608+
second input array whose elements correspond to the exponentiation exponent. Must be compatible with ``x1`` (see :ref:`broadcasting`). Should have a numeric data type.
15971609
15981610
Returns
15991611
-------

0 commit comments

Comments
 (0)