跳到内容

计算输入数组 x 中元素的乘积。

参数

名称 类型 描述 默认值
x

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

必需

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

None
数据类型

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

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

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

None
保持维度

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

False

返回值

名称 类型 描述
输出 数组

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

备注
特殊情况

N 等于要计算乘积的元素数量。

  • 如果 N0,乘积为 1(即空乘积)。

示例

>>> a = sparse.COO.from_numpy(np.array([[0, 2], [-1, 1]]))
>>> o = sparse.prod(a, axis=1)
>>> o.todense()
array([ 0, -1])
源代码位于 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)