跳到内容

实数

返回输入数组 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.real(a)
>>> o.todense()
array([[0., 2.],
       [0., 3.]])
源代码位于 sparse/numba_backend/_common.py
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
def real(x, /):
    """
    Returns the real 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.real(a)
    >>> o.todense()  # doctest: +NORMALIZE_WHITESPACE
    array([[0., 2.],
           [0., 3.]])
    """
    return x.real