跳到内容

所有

测试沿指定轴的所有输入数组元素是否评估为 True

参数

名称 类型 描述 默认值
x

输入数组。

必需

要执行逻辑 AND 归约的轴或多个轴。默认情况下,对整个数组执行逻辑 AND 归约。如果是一个整数元组,则在多个轴上执行逻辑 AND 归约。有效的 axis 是在 [-N, N) 区间上的整数,其中 Nx 的秩(维度数)。如果 axis 指定为负整数,函数通过从最后一个维度(其中 -1 指的是最后一个维度)向后计数来确定要执行归约的轴。如果提供了无效的 axis,函数将引发异常。默认值:None

None
keepdims

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

False

返回值

名称 类型 描述
out 数组

如果对整个数组执行了逻辑 AND 归约,则返回的数组是包含测试结果的零维数组;否则,返回的数组是包含测试结果的非零维数组。返回的数组数据类型为 bool

特殊情况
  • 正无穷、负无穷和 NaN 评估为 True

  • 如果 x 具有复浮点数据类型,则具有非零分量(实部或虚部)的元素评估为 True

  • 如果 x 是空数组,或者用于评估元素的轴(维度)大小为零,则测试结果为 True

示例

>>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
>>> o = sparse.all(a, axis=1)
>>> o.todense()
array([False, False])
源代码位于 sparse/numba_backend/_common.py
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
def all(x, /, *, axis=None, keepdims=False):
    """
    Tests whether all input array elements evaluate to ``True`` along a specified axis.

    Parameters
    ----------
    x: array
        input array.
    axis: Optional[Union[int, Tuple[int, ...]]]
        axis or axes along which to perform a logical AND reduction. By default, a logical AND
        reduction is performed over the entire array.
        If a tuple of integers, logical AND reductions are performed over multiple axes.
        A valid ``axis`` is an integer on the interval ``[-N, N)``, where ``N`` is the rank
        (number of dimensions) of ``x``. If an ``axis`` is specified as a negative integer,
        the function determines the axis along which to perform a reduction by counting backward
        from the last dimension (where ``-1`` refers to the last dimension). If provided an invalid
        ``axis``, the function raiseS an exception. Default: ``None``.
    keepdims: bool
        If ``True``, the reduced axes (dimensions) are included in the result as singleton dimensions,
        and, 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 a logical AND reduction was performed over the entire array, the returned array is a
        zero-dimensional array containing the test result; otherwise, the returned array is a
        non-zero-dimensional array containing the test results.
        The returned array has a data type of ``bool``.

    Special Cases
    -------------

       - Positive infinity, negative infinity, and NaN  evaluate to ``True``.

       - If ``x`` has a complex floating-point data type, elements having a non-zero component
        (real or imaginary) evaluate to ``True``.

       - If ``x`` is an empty array or the size of the axis (dimension) along which to evaluate elements
         is zero, the test result is ``True``.

    Examples
    --------
    >>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
    >>> o = sparse.all(a, axis=1)
    >>> o.todense()  # doctest: +NORMALIZE_WHITESPACE
    array([False, False])
    """
    return x.all(axis=axis, keepdims=keepdims)