跳到内容

equal

计算输入数组 x1 的每个元素 x1_i 与输入数组 x2 的相应元素 x2_ix1_i == x2_i 的真值。

参数

名称 类型 描述 默认值
x1

第一个输入数组。可以具有任何数据类型。

必需
x2

第二个输入数组。必须与 x1 兼容。可以具有任何数据类型。

必需

返回值

名称 类型 描述
out 数组

一个包含按元素结果的数组。返回的数组的数据类型为 bool

特殊情况

对于实值浮点操作数,

  • 如果 x1_iNaNx2_iNaN,则结果为 False
  • 如果 x1_i+infinityx2_i+infinity,则结果为 True
  • 如果 x1_i-infinityx2_i-infinity,则结果为 True
  • 如果 x1_i-0x2_i+0-0,则结果为 True
  • 如果 x1_i+0x2_i+0-0,则结果为 True
  • 如果 x1_i 是有限数,x2_i 是有限数,且 x1_i 等于 x2_i,则结果为 True
  • 在其余情况下,结果为 False

对于复浮点操作数,设 a = real(x1_i)b = imag(x1_i)c = real(x2_i)d = imag(x2_i),且

  • 如果 abcdNaN,则结果为 False
  • 在其余情况下,结果是实值 ac(实部)之间以及实值 bd(虚部)之间的相等比较的逻辑与,如上文所述的实值浮点操作数(即 a == c AND b == d)。

示例

>>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
>>> b = sparse.COO.from_numpy(np.array([[0, 1], [1, 0]]))
>>> o = sparse.equal(a, b)
>>> o.todense()
array([[ True,  True],
       [ False,  True]])
源代码位于 sparse/numba_backend/_common.py
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
def equal(x1, x2, /):
    """
    Computes the truth value of ``x1_i == x2_i`` for each element ``x1_i`` of the input array ``x1``
    with the respective element ``x2_i`` of the input array ``x2``.

    Parameters
    ----------
    x1: array
        first input array. May have any data type.
    x2: array
        second input array. Must be compatible with ``x1``. May have any data type.

    Returns
    -------
    out: array
        an array containing the element-wise results. The returned array is of  data type of ``bool``.

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

    For real-valued floating-point operands,

    - If ``x1_i`` is ``NaN`` or ``x2_i`` is ``NaN``, the result is ``False``.
    - If ``x1_i`` is ``+infinity`` and ``x2_i`` is ``+infinity``, the result is ``True``.
    - If ``x1_i`` is ``-infinity`` and ``x2_i`` is ``-infinity``, the result is ``True``.
    - If ``x1_i`` is ``-0`` and ``x2_i`` is either ``+0`` or ``-0``, the result is ``True``.
    - If ``x1_i`` is ``+0`` and ``x2_i`` is either ``+0`` or ``-0``, the result is ``True``.
    - If ``x1_i`` is a finite number, ``x2_i`` is a finite number, and ``x1_i`` equals ``x2_i``, the result is ``True``.
    - In the remaining cases, the result is ``False``.

    For complex floating-point operands, let ``a = real(x1_i)``, ``b = imag(x1_i)``, ``c = real(x2_i)``,
    ``d = imag(x2_i)``, and

    - If ``a``, ``b``, ``c``, or ``d`` is ``NaN``, the result is ``False``.
    - In the remaining cases, the result is the logical AND of the equality comparison between the real values ``a``
        and ``c`` (real components) and between the real values ``b`` and ``d`` (imaginary components), as described
        above for real-valued  floating-point operands (i.e., ``a == c AND b == d``).

    Examples
    --------
    >>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
    >>> b = sparse.COO.from_numpy(np.array([[0, 1], [1, 0]]))
    >>> o = sparse.equal(a, b)  # doctest: +SKIP
    >>> o.todense()  # doctest: +SKIP
    array([[ True,  True],
           [ False,  True]])
    """
    return x1 == x2