|
1 | 1 | from ._types import array
|
2 | 2 |
|
3 | 3 | 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. |
6 | 8 |
|
7 | 9 | .. note::
|
8 | 10 | For signed integer data types, the absolute value of the minimum representable integer is implementation-dependent.
|
9 | 11 |
|
| 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 | +
|
10 | 18 | **Special Cases**
|
11 | 19 |
|
12 |
| - For floating-point operands, |
| 20 | + For real-valued floating-point operands, |
13 | 21 |
|
14 | 22 | - If ``x_i`` is ``NaN``, the result is ``NaN``.
|
15 | 23 | - If ``x_i`` is ``-0``, the result is ``+0``.
|
16 | 24 | - If ``x_i`` is ``-infinity``, the result is ``+infinity``.
|
17 | 25 |
|
| 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 | +
|
18 | 42 | Parameters
|
19 | 43 | ----------
|
20 | 44 | x: array
|
21 |
| - input array. Should have a real-valued data type. |
| 45 | + input array. Should have a numeric data type. |
22 | 46 |
|
23 | 47 | Returns
|
24 | 48 | -------
|
25 | 49 | 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). |
27 | 51 | """
|
28 | 52 |
|
29 | 53 | def acos(x: array, /) -> array:
|
|
0 commit comments