[Python-checkins] Add "strict" to dotproduct(). Add docstring. Factor-out common code. (GH-100480)
miss-islington
webhook-mailer at python.org
Fri Dec 23 19:00:27 EST 2022
https://github.com/python/cpython/commit/9477594374ba58bc00a95d99d0e2e22cccd02f5e
commit: 9477594374ba58bc00a95d99d0e2e22cccd02f5e
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-12-23T16:00:21-08:00
summary:
Add "strict" to dotproduct(). Add docstring. Factor-out common code. (GH-100480)
(cherry picked from commit f89de679ffec35e82548341cb23e675546602288)
Co-authored-by: Raymond Hettinger <rhettinger at users.noreply.github.com>
files:
M Doc/library/itertools.rst
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 8eb843ab0a67..371f7c1fdcd4 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -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/
@@ -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.
More information about the Python-checkins
mailing list