Skip to content

Commit

Permalink
Add "strict" to dotproduct(). Add docstring. Factor-out common code. (G…
Browse files Browse the repository at this point in the history
…H-100480)

(cherry picked from commit f89de67)

Co-authored-by: Raymond Hettinger <[email protected]>
  • Loading branch information
miss-islington and rhettinger authored Dec 24, 2022
1 parent d5eb2f4 commit 9477594
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,8 @@ which incur interpreter overhead.
return chain.from_iterable(repeat(tuple(iterable), n))

def dotproduct(vec1, vec2):
return sum(map(operator.mul, vec1, vec2))
"Compute a sum of products."
return sum(starmap(operator.mul, zip(vec1, vec2, strict=True)))

def convolve(signal, kernel):
# See: https://betterexplained.com/articles/intuitive-convolution/
Expand All @@ -807,7 +808,7 @@ which incur interpreter overhead.
window = collections.deque([0], maxlen=n) * n
for x in chain(signal, repeat(0, n-1)):
window.append(x)
yield sum(map(operator.mul, kernel, window))
yield dotproduct(kernel, window)

def polynomial_from_roots(roots):
"""Compute a polynomial's coefficients from its roots.
Expand Down

0 comments on commit 9477594

Please sign in to comment.