Skip to content

Commit 9477594

Browse files
Add "strict" to dotproduct(). Add docstring. Factor-out common code. (GH-100480)
(cherry picked from commit f89de67) Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
1 parent d5eb2f4 commit 9477594

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

Doc/library/itertools.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,8 @@ which incur interpreter overhead.
795795
return chain.from_iterable(repeat(tuple(iterable), n))
796796

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

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

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

0 commit comments

Comments
 (0)