Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add "strict" to dotproduct(). Add docstring. Factor-out common code. (G…
…H-100480)

(cherry picked from commit f89de67)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
  • Loading branch information
rhettinger authored and miss-islington committed Dec 23, 2022
commit 141c47997b19bdbb5638ca04212db3d81a94bb81
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