Skip to content

Commit 9eca992

Browse files
authored
Add complex number support to abs (#546)
1 parent f96cd18 commit 9eca992

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

spec/API_specification/array_api/array_object.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -110,28 +110,48 @@ def T(self: array) -> array:
110110

111111
def __abs__(self: array, /) -> array:
112112
"""
113-
Calculates the absolute value for each element of an array instance (i.e., the element-wise result has the same magnitude as the respective element but has positive sign).
113+
Calculates the absolute value for each element of an array instance.
114+
115+
For real-valued input arrays, the element-wise result has the same magnitude as the respective element in ``x`` but has positive sign.
114116
115117
.. note::
116118
For signed integer data types, the absolute value of the minimum representable integer is implementation-dependent.
117119
118120
**Special cases**
119121
120-
For floating-point operands, let ``self`` equal ``x``.
122+
Let ``self`` equal ``x``.
123+
124+
For real-valued floating-point operands,
121125
122126
- If ``x_i`` is ``NaN``, the result is ``NaN``.
123127
- If ``x_i`` is ``-0``, the result is ``+0``.
124128
- If ``x_i`` is ``-infinity``, the result is ``+infinity``.
125129
130+
For complex floating-point operands, let ``a = real(x_i)``, ``b = imag(x_i)``, and
131+
132+
- If ``a`` is either ``+infinity`` or ``-infinity`` and ``b`` is any value (including ``NaN``), the result is ``+infinity``.
133+
- If ``a`` is any value (including ``NaN``) and ``b`` is either ``+infinity`` or ``-infinity``, the result is ``+infinity``.
134+
- If ``a`` is either ``+0`` or ``-0``, the result is equal to ``abs(b)``.
135+
- If ``b`` is either ``+0`` or ``-0``, the result is equal to ``abs(a)``.
136+
- If ``a`` is ``NaN`` and ``b`` is a finite number, the result is ``NaN``.
137+
- If ``a`` is a finite number and ``b`` is ``NaN``, the result is ``NaN``.
138+
- If ``a`` is ``NaN`` and ``b`` is ``NaN``, the result is ``NaN``.
139+
140+
.. note::
141+
For complex floating-point operands, conforming implementations should take care to avoid undue overflow or underflow during intermediate stages of computation.
142+
143+
..
144+
TODO: once ``hypot`` is added to the specification, remove the special cases for complex floating-point operands and the note concerning guarding against undue overflow/underflow, and state that special cases must be handled as if implemented as ``hypot(real(x), imag(x))``.
145+
126146
Parameters
127147
----------
128148
self: array
129-
array instance. Should have a real-valued data type.
149+
array instance. Should have a numeric data type.
130150
131151
Returns
132152
-------
133153
out: array
134-
an array containing the element-wise absolute value. The returned array must have the same data type as ``self``.
154+
an array containing the element-wise absolute value. If ``self`` has a real-valued data type, the returned array must have the same data type as ``self``. If ``self`` has a complex floating-point data type, the returned arrayed must have a real-valued floating-point data type whose precision matches the precision of ``self`` (e.g., if ``self`` is ``complex128``, then the returned array must have a ``float64`` data type).
135155
136156
137157
.. note::

spec/API_specification/array_api/elementwise_functions.py

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
11
from ._types import array
22

33
def abs(x: array, /) -> array:
4-
"""
5-
Calculates the absolute value for each element ``x_i`` of the input array ``x`` (i.e., the element-wise result has the same magnitude as the respective element in ``x`` but has positive sign).
4+
r"""
5+
Calculates the absolute value for each element ``x_i`` of the input array ``x``.
6+
7+
For real-valued input arrays, the element-wise result has the same magnitude as the respective element in ``x`` but has positive sign.
68
79
.. note::
810
For signed integer data types, the absolute value of the minimum representable integer is implementation-dependent.
911
12+
.. note::
13+
For complex floating-point operands, the complex absolute value is known as the norm, modulus, or magnitude and, for a complex number :math:`z = a + bj` is computed as
14+
15+
.. math::
16+
\operatorname{abs}(z) = \sqrt{a^2 + b^2}
17+
1018
**Special Cases**
1119
12-
For floating-point operands,
20+
For real-valued floating-point operands,
1321
1422
- If ``x_i`` is ``NaN``, the result is ``NaN``.
1523
- If ``x_i`` is ``-0``, the result is ``+0``.
1624
- If ``x_i`` is ``-infinity``, the result is ``+infinity``.
1725
26+
For complex floating-point operands, let ``a = real(x_i)``, ``b = imag(x_i)``, and
27+
28+
- If ``a`` is either ``+infinity`` or ``-infinity`` and ``b`` is any value (including ``NaN``), the result is ``+infinity``.
29+
- If ``a`` is any value (including ``NaN``) and ``b`` is either ``+infinity`` or ``-infinity``, the result is ``+infinity``.
30+
- If ``a`` is either ``+0`` or ``-0``, the result is equal to ``abs(b)``.
31+
- If ``b`` is either ``+0`` or ``-0``, the result is equal to ``abs(a)``.
32+
- If ``a`` is ``NaN`` and ``b`` is a finite number, the result is ``NaN``.
33+
- If ``a`` is a finite number and ``b`` is ``NaN``, the result is ``NaN``.
34+
- If ``a`` is ``NaN`` and ``b`` is ``NaN``, the result is ``NaN``.
35+
36+
.. note::
37+
For complex floating-point operands, conforming implementations should take care to avoid undue overflow or underflow during intermediate stages of computation.
38+
39+
..
40+
TODO: once ``hypot`` is added to the specification, remove the special cases for complex floating-point operands and the note concerning guarding against undue overflow/underflow, and state that special cases must be handled as if implemented as ``hypot(real(x), imag(x))``.
41+
1842
Parameters
1943
----------
2044
x: array
21-
input array. Should have a real-valued data type.
45+
input array. Should have a numeric data type.
2246
2347
Returns
2448
-------
2549
out: array
26-
an array containing the absolute value of each element in ``x``. The returned array must have the same data type as ``x``.
50+
an array containing the absolute value of each element in ``x``. If ``x`` has a real-valued data type, the returned array must have the same data type as ``x``. If ``x`` has a complex floating-point data type, the returned arrayed must have a real-valued floating-point data type whose precision matches the precision of ``x`` (e.g., if ``x`` is ``complex128``, then the returned array must have a ``float64`` data type).
2751
"""
2852

2953
def acos(x: array, /) -> array:

0 commit comments

Comments
 (0)