Skip to content

Commit bcd2e31

Browse files
committed
Add complex number support for pow
1 parent dc98c25 commit bcd2e31

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

spec/API_specification/array_api/array_object.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -814,17 +814,19 @@ def __pos__(self: array, /) -> array:
814814
"""
815815

816816
def __pow__(self: array, other: Union[int, float, array], /) -> array:
817-
"""
817+
r"""
818818
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``.
819819
820820
.. note::
821821
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.
822822
823-
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.
823+
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.
824824
825825
**Special cases**
826826
827-
For floating-point operands, let ``self`` equal ``x1`` and ``other`` equal ``x2``.
827+
Let ``self`` equal ``x1`` and ``other`` equal ``x2``.
828+
829+
For real-valued floating-point operands,
828830
829831
- If ``x1_i`` is not equal to ``1`` and ``x2_i`` is ``NaN``, the result is ``NaN``.
830832
- If ``x2_i`` is ``+0``, the result is ``1``, even if ``x1_i`` is ``NaN``.
@@ -851,12 +853,24 @@ def __pow__(self: array, other: Union[int, float, array], /) -> array:
851853
- If ``x1_i`` is ``-0``, ``x2_i`` is less than ``0``, and ``x2_i`` is not an odd integer value, the result is ``+infinity``.
852854
- 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``.
853855
856+
For complex floating-point operands, special cases should be handled as if the operation is implemented as ``exp(x2*log(x1))``.
857+
858+
.. note::
859+
Conforming implementations are allowed to treat special cases involving complex floating-point operands more carefully than as described in this specification.
860+
861+
.. note::
862+
By convention, the branch cut of the natural logarithm is the negative real axis :math:`(-\infty, 0)`.
863+
864+
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`).
865+
866+
*Note: branch cuts have provisional status* (see :ref:`branch-cuts`).
867+
854868
Parameters
855869
----------
856870
self: array
857-
array instance whose elements correspond to the exponentiation base. Should have a real-valued data type.
871+
array instance whose elements correspond to the exponentiation base. Should have a numeric data type.
858872
other: Union[int, float, array]
859-
other array whose elements correspond to the exponentiation exponent. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a real-valued data type.
873+
other array whose elements correspond to the exponentiation exponent. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
860874
861875
Returns
862876
-------

spec/API_specification/array_api/elementwise_functions.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -1198,17 +1198,17 @@ def positive(x: array, /) -> array:
11981198
"""
11991199

12001200
def pow(x1: array, x2: array, /) -> array:
1201-
"""
1201+
r"""
12021202
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``.
12031203
12041204
.. note::
12051205
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.
12061206
1207-
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).
1207+
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).
12081208
12091209
**Special cases**
12101210
1211-
For floating-point operands,
1211+
For real-valued floating-point operands,
12121212
12131213
- If ``x1_i`` is not equal to ``1`` and ``x2_i`` is ``NaN``, the result is ``NaN``.
12141214
- If ``x2_i`` is ``+0``, the result is ``1``, even if ``x1_i`` is ``NaN``.
@@ -1235,12 +1235,24 @@ def pow(x1: array, x2: array, /) -> array:
12351235
- If ``x1_i`` is ``-0``, ``x2_i`` is less than ``0``, and ``x2_i`` is not an odd integer value, the result is ``+infinity``.
12361236
- 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``.
12371237
1238+
For complex floating-point operands, special cases should be handled as if the operation is implemented as ``exp(x2*log(x1))``.
1239+
1240+
.. note::
1241+
Conforming implementations are allowed to treat special cases involving complex floating-point operands more carefully than as described in this specification.
1242+
1243+
.. note::
1244+
By convention, the branch cut of the natural logarithm is the negative real axis :math:`(-\infty, 0)`.
1245+
1246+
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`).
1247+
1248+
*Note: branch cuts have provisional status* (see :ref:`branch-cuts`).
1249+
12381250
Parameters
12391251
----------
12401252
x1: array
1241-
first input array whose elements correspond to the exponentiation base. Should have a real-valued data type.
1253+
first input array whose elements correspond to the exponentiation base. Should have a numeric data type.
12421254
x2: array
1243-
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.
1255+
second input array whose elements correspond to the exponentiation exponent. Must be compatible with ``x1`` (see :ref:`broadcasting`). Should have a numeric data type.
12441256
12451257
Returns
12461258
-------

0 commit comments

Comments
 (0)