简体   繁体   English

numpy.ufunc.identity的用法

[英]Usage of numpy.ufunc.identity

I wonder what line r = ufct.identity does inside function ufunc_reduce . 我想知道函数ufunc_reduce r = ufct.identity行是什么。 Does it just initialize the operation exactly like r = 0 ? 它是否只是像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)

Yes, for the ufunc np.add , ufct.identity is 0 . 是的,对于np.addufct.identity0 But for another function it may be something else. 但是对于其他功能,可能还有其他功能。 For instance, np.multiply.identity is 1 . 例如, np.multiply.identity1

The ufunc_reduce function you show doesn't know in advance what function it will be given, so it can't be hard-coded to use either 0 or 1 . 您显示的ufunc_reduce函数事先并不知道将提供什么功能,因此不能对其进行硬编码以使用01 It gets the proper value by checking the identity attribute of the ufunc instead. 它通过检查ufunc的identity属性来获取适当的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM