Skip to content

sum

Calculates the sum of the input array x.

Parameters:

Name Type Description Default
x

input array of a numeric data type.

required
axis

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.

None
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.

None
keepdims

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.

False

Returns:

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