跳到内容

imag

返回输入数组 x 的每个元素 x_i 的复数的虚部。

参数

名称 类型 描述 默认值
x

一个复浮点数据类型的输入数组。

必需

返回值

名称 类型 描述
out 数组

一个包含按元素计算结果的数组。返回的数组具有与 x 相同的浮点精度浮点数据类型(例如,如果 xcomplex64,则返回的数组具有 float32 浮点数据类型)。

示例

>>> a = sparse.COO.from_numpy(np.array([[0 + 1j, 2 + 0j], [0 + 0j, 3 + 1j]]))
>>> o = sparse.imag(a)
>>> o.todense()
array([[1., 0.],
       [0., 1.]])
源代码位于 sparse/numba_backend/_common.py
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
def imag(x, /):
    """
    Returns the imaginary component of a complex number for each element ``x_i`` of the input array ``x``.

    Parameters
    ----------
    x: array
        input array of a complex floating-point data type.

    Returns
    -------
    out: array
        an array containing the element-wise results.
        The returned array has a floating-point data type with the same floating-point precision as ``x``
        (e.g., if ``x`` is ``complex64``, the returned array has the floating-point data type ``float32``).

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