计算输入数组 x
中每个元素 x_i
的绝对值。
对于实值输入数组,逐元素结果的幅值与 x
中相应元素的幅值相同,但符号为正。
对于复浮点操作数,复数的绝对值被称为范数、模或幅值,对于复数 :math:z = a + bj
,其计算方式为:
\[ operatorname{abs}(z) = sqrt{a^2 + b^2} \]
参数
返回值
名称 | 类型 |
描述 |
out |
数组
|
一个包含 x 中每个元素绝对值的数组。如果 x 具有实值数据类型,则返回的数组与 x 具有相同的数据类型。如果 x 具有复浮点数据类型,则返回的数组具有一个实值浮点数据类型,其精度与 x 的精度匹配(例如,如果 x 是 complex128 ,则返回的数组必须具有 float64 数据类型)。
|
特殊情况
对于实值浮点操作数,
- 如果
x_i
是 NaN
,则结果是 NaN
。
- 如果
x_i
是 -0
,则结果是 +0
。
- 如果
x_i
是 -infinity
,则结果是 +infinity
。
对于复浮点操作数,令 a = real(x_i)
,b = imag(x_i)
,且
- 如果
a
是 +infinity
或 -infinity
且 b
是任意值(包括 NaN
),则结果是 +infinity
。
- 如果
a
是任意值(包括 NaN
)且 b
是 +infinity
或 -infinity
,则结果是 +infinity
。
- 如果
a
是 +0
或 -0
,则结果等于 abs(b)
。
- 如果
b
是 +0
或 -0
,则结果等于 abs(a)
。
- 如果
a
是 NaN
且 b
是一个有限数,则结果是 NaN
。
- 如果
a
是一个有限数且 b
是 NaN
,则结果是 NaN
。
- 如果
a
是 NaN
且 b
是 NaN
,则结果是 NaN
。
示例
>>> a = sparse.COO.from_numpy(np.array([[0, -1], [-2, 0]]))
>>> o = sparse.abs(a)
>>> o.todense()
array([[0, 1],
[2, 0]])
源代码位于 sparse/numba_backend/_common.py
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686 | def abs(x, /):
"""
Calculates the absolute value for each element ``x_i`` of the input array ``x``.
For real-valued input arrays, the element-wise result has the same magnitude as the respective
element in ``x`` but has positive sign.
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
$$
operatorname{abs}(z) = sqrt{a^2 + b^2}
$$
Parameters
----------
x: array
input array of a numeric data type.
Returns
-------
out: array
an array containing the absolute value of each element in ``x``.
If ``x`` has a real-valued data type, the returned array has the same data type as ``x``.
If ``x`` has a complex floating-point data type, the returned array has 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 has a ``float64`` data type).
Special Cases
-------------
For real-valued floating-point operands,
- If ``x_i`` is ``NaN``, the result is ``NaN``.
- If ``x_i`` is ``-0``, the result is ``+0``.
- If ``x_i`` is ``-infinity``, the result is ``+infinity``.
For complex floating-point operands, let ``a = real(x_i)``, ``b = imag(x_i)``, and
- If ``a`` is either ``+infinity`` or ``-infinity`` and ``b`` is any value (including ``NaN``),
the result is ``+infinity``.
- If ``a`` is any value (including ``NaN``) and ``b`` is either ``+infinity`` or ``-infinity``,
the result is ``+infinity``.
- If ``a`` is either ``+0`` or ``-0``, the result is equal to ``abs(b)``.
- If ``b`` is either ``+0`` or ``-0``, the result is equal to ``abs(a)``.
- If ``a`` is ``NaN`` and ``b`` is a finite number, the result is ``NaN``.
- If ``a`` is a finite number and ``b`` is ``NaN``, the result is ``NaN``.
- If ``a`` is ``NaN`` and ``b`` is ``NaN``, the result is ``NaN``.
Examples
--------
>>> a = sparse.COO.from_numpy(np.array([[0, -1], [-2, 0]]))
>>> o = sparse.abs(a)
>>> o.todense()
array([[0, 1],
[2, 0]])
"""
return x.__abs__()
|