diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2022-12-23 23:52:16 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-23 23:52:16 (GMT) |
commit | f89de679ffec35e82548341cb23e675546602288 (patch) | |
tree | ab5323aa42d2e69654b04e97cf901b6bd109e4e5 | |
parent | 474220e3a58d739acc5154eb3e000461d2222d62 (diff) | |
download | cpython-f89de679ffec35e82548341cb23e675546602288.zip cpython-f89de679ffec35e82548341cb23e675546602288.tar.gz cpython-f89de679ffec35e82548341cb23e675546602288.tar.bz2 |
Add "strict" to dotproduct(). Add docstring. Factor-out common code. (GH-100480)
-rw-r--r-- | Doc/library/itertools.rst | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 9146ed1..9688aae 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -834,7 +834,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/ @@ -846,7 +847,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. |