]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add "strict" to dotproduct(). Add docstring. Factor-out common code. (GH-100480)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Fri, 23 Dec 2022 23:52:16 +0000 (15:52 -0800)
committerGitHub <noreply@github.com>
Fri, 23 Dec 2022 23:52:16 +0000 (15:52 -0800)
Doc/library/itertools.rst

index 9146ed1bfb6226a0472fdca8140d15d60f8fdb86..9688aae68e6cec2e9fa3846da741ecea28d21c6e 100644 (file)
@@ -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.