跳到内容

求和

计算输入数组 x 的和。

参数

名称 类型 描述 默认值
x

数字数据类型的输入数组。

必需
axis

计算和的轴或多个轴。默认情况下,和在整个数组上计算。如果是一个整数元组,则和在多个轴上计算。默认值:None

None
dtype

返回数组的数据类型。如果为 None,则返回数组与 x 具有相同的数据类型,除非 x 的整数数据类型支持的值范围小于默认整数数据类型(例如,x 具有 int16uint32 数据类型,而默认整数数据类型为 int64)。在后一种情况下,

  • 如果 x 具有有符号整数数据类型(例如 int16),则返回数组具有默认整数数据类型。
  • 如果 x 具有无符号整数数据类型(例如 uint16),则返回数组具有与默认整数数据类型位数相同的无符号整数数据类型(例如,如果默认整数数据类型为 int32,则返回数组必须具有 uint32 数据类型)。

如果数据类型(无论是指定还是解析)与 x 的数据类型不同,则在计算和之前将输入数组转换为指定的数据类型。理由:dtype 关键字参数旨在帮助防止溢出。默认值:None

None
keepdims

如果为 True,则缩减的轴(维度)作为单例维度包含在结果中。因此,结果与输入数组兼容。否则,如果为 False,则缩减的轴(维度)不包含在结果中。默认值:False

False

返回值

名称 类型 描述
out 数组

如果和是在整个数组上计算的,则返回一个包含和的零维数组。否则,返回一个包含和的数组。返回数组的数据类型如上文 dtype 参数所述。

特殊情况

N 等于计算和的元素数量。

  • 如果 N0,则和为 0(即空和)。

示例

>>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
>>> o = sparse.sum(a, axis=1)
>>> o.todense()
array([1, 2])
源代码位于 sparse/numba_backend/_common.py
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
def sum(x, /, *, axis=None, dtype=None, keepdims=False):
    """
    Calculates the sum of the input array ``x``.

    Parameters
    ----------
    x: array
        input array of a numeric data type.
    axis: Optional[Union[int, Tuple[int, ...]]]
        axis or axes along which sums are computed.
        By default, the sum is computed over the entire array.
        If a tuple of integers, sums must are computed over multiple axes. Default: ``None``.

    dtype: Optional[dtype]
        data type of the returned array.
        If ``None``, the returned array has the same data type as ``x``, unless ``x`` has an integer data type
        supporting a smaller range of values than the default integer data type (e.g., ``x`` has an ``int16``
        or ``uint32`` data type and the default integer data type is ``int64``). In those latter cases:

        -   if ``x`` has a signed integer data type (e.g., ``int16``), the returned array has the default integer
            data type.
        -   if ``x`` has an unsigned integer data type (e.g., ``uint16``), the returned array has an unsigned integer
            data type having the same number of bits as the default integer data type (e.g., if the default integer
            data type is ``int32``, the returned array must have a ``uint32`` data type).

        If the data type (either specified or resolved) differs from the data type of ``x``, the input array is cast to
        the specified data type before computing the sum.
        Rationale: the ``dtype`` keyword argument is intended to help prevent overflows. Default: ``None``.
    keepdims: bool
        If ``True``, the reduced axes (dimensions) are included in the result as singleton dimensions.
        Accordingly, the result is compatible with the input array.
        Otherwise, if ``False``, the reduced axes (dimensions) are not included in the result. Default: ``False``.

    Returns
    -------
    out: array
        if the sum was computed over the entire array, a zero-dimensional array containing the sum.
        Otherwise, an array containing the sums.
        The returned array has the data type as described by the ``dtype`` parameter above.

    Special Cases
    -------------
    Let ``N`` equal the number of elements over which to compute the sum.

    -   If ``N`` is ``0``, the sum is ``0`` (i.e., the empty sum).

    Examples
    --------
    >>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
    >>> o = sparse.sum(a, axis=1)
    >>> o.todense()
    array([1, 2])
    """
    return x.sum(axis=axis, keepdims=keepdims, dtype=dtype)