跳到内容

元素级操作

将一个函数应用于任意数量的参数。

参数

名称 类型 描述 默认值
func 可调用对象

要应用的函数。必须支持广播。

必需
*args 元组

函数的参数。可以是 sparse.SparseArray 对象或 scipy.sparse.spmatrix 对象。

()
**kwargs 字典

要传递给函数的任何额外参数。

{}

返回值

类型 描述
SparseArray

应用函数的结果。

引发

类型 描述
ValueError

如果操作会导致稠密矩阵,或者操作数没有可广播的形状。

另请参阅

numpy.ufunc : 一个类似的Numpy构造。请注意,任何 ufunc 都可以用作此函数的 func 输入。

备注

以前,有时支持使用Numpy数组进行操作。现在,需要将Numpy数组转换为 sparse.COO 对象。

源代码位于 sparse/numba_backend/_umath.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def elemwise(func, *args, **kwargs):
    """
    Apply a function to any number of arguments.

    Parameters
    ----------
    func : Callable
        The function to apply. Must support broadcasting.
    *args : tuple, optional
        The arguments to the function. Can be [`sparse.SparseArray`][] objects
        or [`scipy.sparse.spmatrix`][] objects.
    **kwargs : dict, optional
        Any additional arguments to pass to the function.

    Returns
    -------
    SparseArray
        The result of applying the function.

    Raises
    ------
    ValueError
        If the operation would result in a dense matrix, or if the operands
        don't have broadcastable shapes.

    See Also
    --------
    [`numpy.ufunc`][] :
        A similar Numpy construct. Note that any `ufunc` can be used
        as the `func` input to this function.

    Notes
    -----
    Previously, operations with Numpy arrays were sometimes supported. Now,
    it is necessary to convert Numpy arrays to [`sparse.COO`][] objects.
    """

    return _Elemwise(func, *args, **kwargs).get_result()