Python numpy.ufunc.identity的用法

Python numpy.ufunc.identity的用法,python,numpy,Python,Numpy,我想知道什么行r=ufct.identity内部函数ufunc\u减少了。它只是像r=0一样初始化操作吗 a = np.array([2,3,4,5]) b = np.array([8,5,4]) c = np.array([5,4,6,8,3]) def ufunc_reduce(ufct, *vectors): vs = np.ix_(*vectors) r = ufct.identity for v in vs: r = ufct(r,v)

我想知道什么行
r=ufct.identity
内部函数
ufunc\u减少了
。它只是像
r=0
一样初始化操作吗

a = np.array([2,3,4,5])
b = np.array([8,5,4])
c = np.array([5,4,6,8,3])

def ufunc_reduce(ufct, *vectors):
    vs = np.ix_(*vectors)
    r = ufct.identity
    for v in vs:
        r = ufct(r,v)
    return r

ufunc_reduce(np.add,a,b,c)

是的,对于ufunc
np.add
ufct.identity
0
。但对于另一个函数,它可能是另一个函数。例如,
np.multiply.identity
1


您显示的
ufunc\u reduce
函数事先不知道将提供什么函数,因此不能硬编码为使用
0
1
。它通过检查ufunc的
identity
属性来获得正确的值。

常规
ufunc中使用
identity
属性。reduce
作为wll,