Skip to content

prod

Calculates the product of input array x elements.

Parameters:

Name Type Description Default
x

input array of a numeric data type.

required
axis

axis or axes along which products is computed. By default, the product are computed over the entire array. If a tuple of integers, products 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 are 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 product was computed over the entire array, a zero-dimensional array containing the product. Otherwise, a non-zero-dimensional array containing the products. The returned array has a data type as described by the dtype parameter above.

Notes
Special Cases

Let N equal the number of elements over which to compute the product.

  • If N is 0, the product is 1 (i.e., the empty product).

Examples:

>>> a = sparse.COO.from_numpy(np.array([[0, 2], [-1, 1]]))
>>> o = sparse.prod(a, axis=1)
>>> o.todense()
array([ 0, -1])
Source code in sparse/numba_backend/_common.py
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
def prod(x, /, *, axis=None, dtype=None, keepdims=False):
    """
    Calculates the product of input array ``x`` elements.

    Parameters
    ----------
    x: array
        input array of a numeric data type.
    axis: Optional[Union[int, Tuple[int, ...]]]
        axis or axes along which products is computed.
        By default, the product are computed over the entire array.
        If a tuple of integers, products 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 are 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 product was computed over the entire array, a zero-dimensional array containing the product.
        Otherwise, a non-zero-dimensional array containing the products.
        The returned array has a data type as described by the ``dtype`` parameter above.

    Notes
    -----

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

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

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