假设我有两个长度相同的1d数组(说n),现在我想实现一个“ cumdot”函数,该函数输出长度为n的1d数组,可以用纯python代码实现
def cumdot(a,b):#a,b are two 1d arrays with same length
n = len(a)
output = np.empty(n)
for i in range(n):
output[i] = np.dot(a[:i+1],b[:i+1])
return output
如何更有效地实现“ cumdot”功能?
最佳答案
def cumdot(a, b):
return numpy.cumsum(a * b)