Skip to content

abs

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:

Name Type Description Default
x

input array of a numeric data type.

required

Returns:

Name Type Description
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]])
Source code in 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__()