Skip to content

Commit 4b4e6da

Browse files
authored
[3.11] Future-proof recipe by renaming dotproduct() to sumprod() (GH-100828)
1 parent a3e2407 commit 4b4e6da

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Doc/library/itertools.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ by combining :func:`map` and :func:`count` to form ``map(f, count())``.
3333
These tools and their built-in counterparts also work well with the high-speed
3434
functions in the :mod:`operator` module. For example, the multiplication
3535
operator can be mapped across two vectors to form an efficient dot-product:
36-
``sum(map(operator.mul, vector1, vector2))``.
36+
``sum(starmap(operator.mul, zip(vec1, vec2, strict=True)))``.
3737

3838

3939
**Infinite iterators:**
@@ -799,7 +799,7 @@ which incur interpreter overhead.
799799
"Returns the sequence elements n times"
800800
return chain.from_iterable(repeat(tuple(iterable), n))
801801

802-
def dotproduct(vec1, vec2):
802+
def sumprod(vec1, vec2):
803803
"Compute a sum of products."
804804
return sum(starmap(operator.mul, zip(vec1, vec2, strict=True)))
805805

@@ -813,7 +813,7 @@ which incur interpreter overhead.
813813
window = collections.deque([0], maxlen=n) * n
814814
for x in chain(signal, repeat(0, n-1)):
815815
window.append(x)
816-
yield dotproduct(kernel, window)
816+
yield sumprod(kernel, window)
817817

818818
def polynomial_from_roots(roots):
819819
"""Compute a polynomial's coefficients from its roots.
@@ -1181,7 +1181,7 @@ which incur interpreter overhead.
11811181
>>> list(ncycles('abc', 3))
11821182
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
11831183

1184-
>>> dotproduct([1,2,3], [4,5,6])
1184+
>>> sumprod([1,2,3], [4,5,6])
11851185
32
11861186

11871187
>>> data = [20, 40, 24, 32, 20, 28, 16]

0 commit comments

Comments
 (0)