newbie
newbie

Reputation: 45

how to implement "cumdot" in python

Suppose I have two 1d arrays with same length(saying n),and now I want to implement a "cumdot" function,which output 1d array with length n and can be implemented in pure python code

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

How can I implement the "cumdot" function more efficiently?

Upvotes: 2

Views: 185

Answers (2)

Shahryar Saljoughi
Shahryar Saljoughi

Reputation: 3059

I think by pure python you mean: using core of python!

def comdot(a, b):
    result = [i[0]*i[1] for i in zip(a, b)]
    return result

Upvotes: -1

Mike Graham
Mike Graham

Reputation: 76753

def cumdot(a, b):
    return numpy.cumsum(a * b)

Upvotes: 4

Related Questions