Skip to content

Commit 4e82d64

Browse files
authored
Add complex number support for multiplication (#551)
1 parent 5283bee commit 4e82d64

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

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

+32-4
Original file line numberDiff line numberDiff line change
@@ -764,12 +764,14 @@ def __mod__(self: array, other: Union[int, float, array], /) -> array:
764764
"""
765765

766766
def __mul__(self: array, other: Union[int, float, array], /) -> array:
767-
"""
767+
r"""
768768
Calculates the product for each element of an array instance with the respective element of the array ``other``.
769769
770770
**Special cases**
771771
772-
For floating-point operands, let ``self`` equal ``x1`` and ``other`` equal ``x2``.
772+
Let ``self`` equal ``x1`` and ``other`` equal ``x2``.
773+
774+
For real-valued floating-point operands,
773775
774776
- If either ``x1_i`` or ``x2_i`` is ``NaN``, the result is ``NaN``.
775777
- If ``x1_i`` is either ``+infinity`` or ``-infinity`` and ``x2_i`` is either ``+0`` or ``-0``, the result is ``NaN``.
@@ -781,16 +783,42 @@ def __mul__(self: array, other: Union[int, float, array], /) -> array:
781783
- If ``x1_i`` is a nonzero finite number and ``x2_i`` is either ``+infinity`` or ``-infinity``, the result is a signed infinity with the mathematical sign determined by the rule already stated above.
782784
- In the remaining cases, where neither ``infinity`` nor `NaN` is involved, the product 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 result is an ``infinity`` of appropriate mathematical sign. If the magnitude is too small to represent, the result is a zero of appropriate mathematical sign.
783785
786+
For complex floating-point operands, multiplication is defined according to the following table. For real components ``a`` and ``c`` and imaginary components ``b`` and ``d``,
787+
788+
+------------+----------------+-----------------+--------------------------+
789+
| | c | dj | c + dj |
790+
+============+================+=================+==========================+
791+
| **a** | a * c | (a*d)j | (a*c) + (a*d)j |
792+
+------------+----------------+-----------------+--------------------------+
793+
| **bj** | (b*c)j | -(b*d) | -(b*d) + (b*c)j |
794+
+------------+----------------+-----------------+--------------------------+
795+
| **a + bj** | (a*c) + (b*c)j | -(b*d) + (a*d)j | special rules |
796+
+------------+----------------+-----------------+--------------------------+
797+
798+
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.
799+
800+
When ``a``, ``b``, ``c``, or ``d`` are all finite numbers (i.e., a value other than ``NaN``, ``+infinity``, or ``-infinity``), multiplication of complex floating-point operands should be computed as if calculated according to the textbook formula for complex number multiplication
801+
802+
.. math::
803+
(a + bj) \cdot (c + dj) = (ac - bd) + (bc + ad)j
804+
805+
When at least one of ``a``, ``b``, ``c``, or ``d`` is ``NaN``, ``+infinity``, or ``-infinity``,
806+
807+
- If ``a``, ``b``, ``c``, and ``d`` are all ``NaN``, the result is ``NaN + NaN j``.
808+
- In the remaining cases, the result is implementation dependent.
809+
810+
.. note::
811+
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 multiplication. 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 multiplication according to the textbook formula. Accordingly, special case behavior is unlikely to be consistent across implementations.
784812
785813
.. note::
786814
Floating-point multiplication is not always associative due to finite precision.
787815
788816
Parameters
789817
----------
790818
self: array
791-
array instance. Should have a real-valued data type.
819+
array instance. Should have a numeric data type.
792820
other: Union[int, float, array]
793-
other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a real-valued data type.
821+
other array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
794822
795823
Returns
796824
-------

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

+31-4
Original file line numberDiff line numberDiff line change
@@ -1496,12 +1496,12 @@ def logical_xor(x1: array, x2: array, /) -> array:
14961496
"""
14971497

14981498
def multiply(x1: array, x2: array, /) -> array:
1499-
"""
1499+
r"""
15001500
Calculates the product for each element ``x1_i`` of the input array ``x1`` with the respective element ``x2_i`` of the input array ``x2``.
15011501
15021502
**Special cases**
15031503
1504-
For floating-point operands,
1504+
For real-valued floating-point operands,
15051505
15061506
- If either ``x1_i`` or ``x2_i`` is ``NaN``, the result is ``NaN``.
15071507
- If ``x1_i`` is either ``+infinity`` or ``-infinity`` and ``x2_i`` is either ``+0`` or ``-0``, the result is ``NaN``.
@@ -1513,15 +1513,42 @@ def multiply(x1: array, x2: array, /) -> array:
15131513
- If ``x1_i`` is a nonzero finite number and ``x2_i`` is either ``+infinity`` or ``-infinity``, the result is a signed infinity with the mathematical sign determined by the rule already stated above.
15141514
- In the remaining cases, where neither ``infinity`` nor ``NaN`` is involved, the product 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 result is an `infinity` of appropriate mathematical sign. If the magnitude is too small to represent, the result is a zero of appropriate mathematical sign.
15151515
1516+
For complex floating-point operands, multiplication is defined according to the following table. For real components ``a`` and ``c`` and imaginary components ``b`` and ``d``,
1517+
1518+
+------------+----------------+-----------------+--------------------------+
1519+
| | c | dj | c + dj |
1520+
+============+================+=================+==========================+
1521+
| **a** | a * c | (a*d)j | (a*c) + (a*d)j |
1522+
+------------+----------------+-----------------+--------------------------+
1523+
| **bj** | (b*c)j | -(b*d) | -(b*d) + (b*c)j |
1524+
+------------+----------------+-----------------+--------------------------+
1525+
| **a + bj** | (a*c) + (b*c)j | -(b*d) + (a*d)j | special rules |
1526+
+------------+----------------+-----------------+--------------------------+
1527+
1528+
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.
1529+
1530+
When ``a``, ``b``, ``c``, or ``d`` are all finite numbers (i.e., a value other than ``NaN``, ``+infinity``, or ``-infinity``), multiplication of complex floating-point operands should be computed as if calculated according to the textbook formula for complex number multiplication
1531+
1532+
.. math::
1533+
(a + bj) \cdot (c + dj) = (ac - bd) + (bc + ad)j
1534+
1535+
When at least one of ``a``, ``b``, ``c``, or ``d`` is ``NaN``, ``+infinity``, or ``-infinity``,
1536+
1537+
- If ``a``, ``b``, ``c``, and ``d`` are all ``NaN``, the result is ``NaN + NaN j``.
1538+
- In the remaining cases, the result is implementation dependent.
1539+
1540+
.. note::
1541+
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 multiplication. 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 multiplication according to the textbook formula. Accordingly, special case behavior is unlikely to be consistent across implementations.
1542+
15161543
.. note::
15171544
Floating-point multiplication is not always associative due to finite precision.
15181545
15191546
Parameters
15201547
----------
15211548
x1: array
1522-
first input array. Should have a real-valued data type.
1549+
first input array. Should have a numeric data type.
15231550
x2: array
1524-
second input array. Must be compatible with ``x1`` (see :ref:`broadcasting`). Should have a real-valued data type.
1551+
second input array. Must be compatible with ``x1`` (see :ref:`broadcasting`). Should have a numeric data type.
15251552
15261553
Returns
15271554
-------

0 commit comments

Comments
 (0)