Skip to content

Commit 8062655

Browse files
authored
Add support for complex number addition (#525)
1 parent 31142bb commit 8062655

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

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

+24-3
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ def __add__(self: array, other: Union[int, float, array], /) -> array:
144144
145145
**Special cases**
146146
147-
For floating-point operands, let ``self`` equal ``x1`` and ``other`` equal ``x2``.
147+
Let ``self`` equal ``x1`` and ``other`` equal ``x2``.
148+
149+
For real-valued floating-point operands,
148150
149151
- If either ``x1_i`` or ``x2_i`` is ``NaN``, the result is ``NaN``.
150152
- If ``x1_i`` is ``+infinity`` and ``x2_i`` is ``-infinity``, the result is ``NaN``.
@@ -167,12 +169,31 @@ def __add__(self: array, other: Union[int, float, array], /) -> array:
167169
.. note::
168170
Floating-point addition is a commutative operation, but not always associative.
169171
172+
For complex floating-point operands, addition is defined according to the following table. For real components ``a`` and ``c`` and imaginary components ``b`` and ``d``,
173+
174+
+------------+------------+------------+----------------+
175+
| | c | dj | c + dj |
176+
+============+============+============+================+
177+
| **a** | a + c | a + dj | (a+c) + dj |
178+
+------------+------------+------------+----------------+
179+
| **bj** | c + bj | (b+d)j | c + (b+d)j |
180+
+------------+------------+------------+----------------+
181+
| **a + bj** | (a+c) + bj | a + (b+d)j | (a+c) + (b+d)j |
182+
+------------+------------+------------+----------------+
183+
184+
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. For example, let ``a = real(x1_i)``, ``b = imag(x1_i)``, ``c = real(x2_i)``, ``d = imag(x2_i)``, and
185+
186+
- If ``a`` is ``-0`` and ``c`` is ``-0``, the real component of the result is ``-0``.
187+
- Similarly, if ``b`` is ``+0`` and ``d`` is ``-0``, the imaginary component of the result is ``+0``.
188+
189+
Hence, if ``z1 = a + bj = -0 + 0j`` and ``z2 = c + dj = -0 - 0j``, the result of ``z1 + z2`` is ``-0 + 0j``.
190+
170191
Parameters
171192
----------
172193
self: array
173-
array instance (augend array). Should have a real-valued data type.
194+
array instance (augend array). Should have a numeric data type.
174195
other: Union[int, float, array]
175-
addend array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a real-valued data type.
196+
addend array. Must be compatible with ``self`` (see :ref:`broadcasting`). Should have a numeric data type.
176197
177198
Returns
178199
-------

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

+22-3
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def add(x1: array, x2: array, /) -> array:
161161
162162
**Special cases**
163163
164-
For floating-point operands,
164+
For real-valued floating-point operands,
165165
166166
- If either ``x1_i`` or ``x2_i`` is ``NaN``, the result is ``NaN``.
167167
- If ``x1_i`` is ``+infinity`` and ``x2_i`` is ``-infinity``, the result is ``NaN``.
@@ -184,12 +184,31 @@ def add(x1: array, x2: array, /) -> array:
184184
.. note::
185185
Floating-point addition is a commutative operation, but not always associative.
186186
187+
For complex floating-point operands, addition is defined according to the following table. For real components ``a`` and ``c`` and imaginary components ``b`` and ``d``,
188+
189+
+------------+------------+------------+----------------+
190+
| | c | dj | c + dj |
191+
+============+============+============+================+
192+
| **a** | a + c | a + dj | (a+c) + dj |
193+
+------------+------------+------------+----------------+
194+
| **bj** | c + bj | (b+d)j | c + (b+d)j |
195+
+------------+------------+------------+----------------+
196+
| **a + bj** | (a+c) + bj | a + (b+d)j | (a+c) + (b+d)j |
197+
+------------+------------+------------+----------------+
198+
199+
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. For example, let ``a = real(x1_i)``, ``b = imag(x1_i)``, ``c = real(x2_i)``, ``d = imag(x2_i)``, and
200+
201+
- If ``a`` is ``-0`` and ``c`` is ``-0``, the real component of the result is ``-0``.
202+
- Similarly, if ``b`` is ``+0`` and ``d`` is ``-0``, the imaginary component of the result is ``+0``.
203+
204+
Hence, if ``z1 = a + bj = -0 + 0j`` and ``z2 = c + dj = -0 - 0j``, the result of ``z1 + z2`` is ``-0 + 0j``.
205+
187206
Parameters
188207
----------
189208
x1: array
190-
first input array. Should have a real-valued data type.
209+
first input array. Should have a numeric data type.
191210
x2: array
192-
second input array. Must be compatible with ``x1`` (see :ref:`broadcasting`). Should have a real-valued data type.
211+
second input array. Must be compatible with ``x1`` (see :ref:`broadcasting`). Should have a numeric data type.
193212
194213
Returns
195214
-------

0 commit comments

Comments
 (0)