Skip to content

Commit 31b3df8

Browse files
authored
Add support for complex number division (#554)
1 parent 4e82d64 commit 31b3df8

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

spec/API_specification/array_api/array_object.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ def __sub__(self: array, other: Union[int, float, array], /) -> array:
10731073
"""
10741074

10751075
def __truediv__(self: array, other: Union[int, float, array], /) -> array:
1076-
"""
1076+
r"""
10771077
Evaluates ``self_i / other_i`` for each element of an array instance with the respective element of the array ``other``.
10781078
10791079
.. note::
@@ -1083,7 +1083,9 @@ def __truediv__(self: array, other: Union[int, float, array], /) -> array:
10831083
10841084
**Special cases**
10851085
1086-
For floating-point operands, let ``self`` equal ``x1`` and ``other`` equal ``x2``.
1086+
Let ``self`` equal ``x1`` and ``other`` equal ``x2``.
1087+
1088+
For floating-point operands,
10871089
10881090
- If either ``x1_i`` or ``x2_i`` is ``NaN``, the result is ``NaN``.
10891091
- If ``x1_i`` is either ``+infinity`` or ``-infinity`` and ``x2_i`` is either ``+infinity`` or ``-infinity``, the result is `NaN`.
@@ -1108,17 +1110,44 @@ def __truediv__(self: array, other: Union[int, float, array], /) -> array:
11081110
- If ``x1_i`` and ``x2_i`` have different mathematical signs and are both nonzero finite numbers, the result has a negative mathematical sign.
11091111
- In the remaining cases, where neither ``-infinity``, ``+0``, ``-0``, nor ``NaN`` is involved, the quotient must be computed and rounded to the nearest representable value according to IEEE 754-2019 and a supported rounding mode. If the magnitude is too large to represent, the operation overflows and the result is an ``infinity`` of appropriate mathematical sign. If the magnitude is too small to represent, the operation underflows and the result is a zero of appropriate mathematical sign.
11101112
1113+
For complex floating-point operands, division is defined according to the following table. For real components ``a`` and ``c`` and imaginary components ``b`` and ``d``,
1114+
1115+
+------------+----------------+-----------------+--------------------------+
1116+
| | c | dj | c + dj |
1117+
+============+================+=================+==========================+
1118+
| **a** | a / c | -(a/d)j | special rules |
1119+
+------------+----------------+-----------------+--------------------------+
1120+
| **bj** | (b/c)j | b/d | special rules |
1121+
+------------+----------------+-----------------+--------------------------+
1122+
| **a + bj** | (a/c) + (b/c)j | b/d - (a/d)j | special rules |
1123+
+------------+----------------+-----------------+--------------------------+
1124+
1125+
In general, for complex floating-point operands, real-valued floating-point special cases must independently apply to the real and imaginary component operations involving real numbers as described in the above table.
1126+
1127+
When ``a``, ``b``, ``c``, or ``d`` are all finite numbers (i.e., a value other than ``NaN``, ``+infinity``, or ``-infinity``), division of complex floating-point operands should be computed as if calculated according to the textbook formula for complex number division
1128+
1129+
.. math::
1130+
\frac{a + bj}{c + dj} = \frac{(ac + bd) + (bc - ad)j}{c^2 + d^2}
1131+
1132+
When at least one of ``a``, ``b``, ``c``, or ``d`` is ``NaN``, ``+infinity``, or ``-infinity``,
1133+
1134+
- If ``a``, ``b``, ``c``, and ``d`` are all ``NaN``, the result is ``NaN + NaN j``.
1135+
- In the remaining cases, the result is implementation dependent.
1136+
1137+
.. note::
1138+
For complex floating-point operands, the results of special cases may be implementation dependent depending on how an implementation chooses to model complex numbers and complex infinity (e.g., complex plane versus Riemann sphere). For those implementations following C99 and its one-infinity model, when at least one component is infinite, even if the other component is ``NaN``, the complex value is infinite, and the usual arithmetic rules do not apply to complex-complex division. In the interest of performance, other implementations may want to avoid the complex branching logic necessary to implement the one-infinity model and choose to implement all complex-complex division according to the textbook formula. Accordingly, special case behavior is unlikely to be consistent across implementations.
1139+
11111140
Parameters
11121141
----------
11131142
self: array
1114-
array instance. Should have a real-valued data type.
1143+
array instance. Should have a numeric data type.
11151144
other: Union[int, float, array]
1116-
other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a real-valued data type.
1145+
other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
11171146
11181147
Returns
11191148
-------
11201149
out: array
1121-
an array containing the element-wise results. The returned array should have a real-valued floating-point data type determined by :ref:`type-promotion`.
1150+
an array containing the element-wise results. The returned array should have a floating-point data type determined by :ref:`type-promotion`.
11221151
11231152
11241153
.. note::

spec/API_specification/array_api/elementwise_functions.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -763,8 +763,8 @@ def cosh(x: array, /) -> array:
763763
"""
764764

765765
def divide(x1: array, x2: array, /) -> array:
766-
"""
767-
Calculates the division for each element ``x1_i`` of the input array ``x1`` with the respective element ``x2_i`` of the input array ``x2``.
766+
r"""
767+
Calculates the division of each element ``x1_i`` of the input array ``x1`` with the respective element ``x2_i`` of the input array ``x2``.
768768
769769
.. note::
770770
If one or both of the input arrays have integer data types, the result is implementation-dependent, as type promotion between data type "kinds" (e.g., integer versus floating-point) is unspecified.
@@ -773,7 +773,7 @@ def divide(x1: array, x2: array, /) -> array:
773773
774774
**Special cases**
775775
776-
For floating-point operands,
776+
For real-valued floating-point operands,
777777
778778
- If either ``x1_i`` or ``x2_i`` is ``NaN``, the result is ``NaN``.
779779
- If ``x1_i`` is either ``+infinity`` or ``-infinity`` and ``x2_i`` is either ``+infinity`` or ``-infinity``, the result is ``NaN``.
@@ -798,17 +798,44 @@ def divide(x1: array, x2: array, /) -> array:
798798
- If ``x1_i`` and ``x2_i`` have different mathematical signs and are both nonzero finite numbers, the result has a negative mathematical sign.
799799
- In the remaining cases, where neither ``-infinity``, ``+0``, ``-0``, nor ``NaN`` is involved, the quotient must be computed and rounded to the nearest representable value according to IEEE 754-2019 and a supported rounding mode. If the magnitude is too large to represent, the operation overflows and the result is an ``infinity`` of appropriate mathematical sign. If the magnitude is too small to represent, the operation underflows and the result is a zero of appropriate mathematical sign.
800800
801+
For complex floating-point operands, division is defined according to the following table. For real components ``a`` and ``c`` and imaginary components ``b`` and ``d``,
802+
803+
+------------+----------------+-----------------+--------------------------+
804+
| | c | dj | c + dj |
805+
+============+================+=================+==========================+
806+
| **a** | a / c | -(a/d)j | special rules |
807+
+------------+----------------+-----------------+--------------------------+
808+
| **bj** | (b/c)j | b/d | special rules |
809+
+------------+----------------+-----------------+--------------------------+
810+
| **a + bj** | (a/c) + (b/c)j | b/d - (a/d)j | special rules |
811+
+------------+----------------+-----------------+--------------------------+
812+
813+
In general, for complex floating-point operands, real-valued floating-point special cases must independently apply to the real and imaginary component operations involving real numbers as described in the above table.
814+
815+
When ``a``, ``b``, ``c``, or ``d`` are all finite numbers (i.e., a value other than ``NaN``, ``+infinity``, or ``-infinity``), division of complex floating-point operands should be computed as if calculated according to the textbook formula for complex number division
816+
817+
.. math::
818+
\frac{a + bj}{c + dj} = \frac{(ac + bd) + (bc - ad)j}{c^2 + d^2}
819+
820+
When at least one of ``a``, ``b``, ``c``, or ``d`` is ``NaN``, ``+infinity``, or ``-infinity``,
821+
822+
- If ``a``, ``b``, ``c``, and ``d`` are all ``NaN``, the result is ``NaN + NaN j``.
823+
- In the remaining cases, the result is implementation dependent.
824+
825+
.. note::
826+
For complex floating-point operands, the results of special cases may be implementation dependent depending on how an implementation chooses to model complex numbers and complex infinity (e.g., complex plane versus Riemann sphere). For those implementations following C99 and its one-infinity model, when at least one component is infinite, even if the other component is ``NaN``, the complex value is infinite, and the usual arithmetic rules do not apply to complex-complex division. In the interest of performance, other implementations may want to avoid the complex branching logic necessary to implement the one-infinity model and choose to implement all complex-complex division according to the textbook formula. Accordingly, special case behavior is unlikely to be consistent across implementations.
827+
801828
Parameters
802829
----------
803830
x1: array
804-
dividend input array. Should have a real-valued data type.
831+
dividend input array. Should have a numeric data type.
805832
x2: array
806-
divisor input array. Must be compatible with ``x1`` (see :ref:`broadcasting`). Should have a real-valued data type.
833+
divisor input array. Must be compatible with ``x1`` (see :ref:`broadcasting`). Should have a numeric data type.
807834
808835
Returns
809836
-------
810837
out: array
811-
an array containing the element-wise results. The returned array must have a real-valued floating-point data type determined by :ref:`type-promotion`.
838+
an array containing the element-wise results. The returned array must have a floating-point data type determined by :ref:`type-promotion`.
812839
"""
813840

814841
def equal(x1: array, x2: array, /) -> array:

0 commit comments

Comments
 (0)