diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2023-01-07 18:46:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-07 18:46:35 (GMT) |
commit | 47b9f83a83db288c652e43567c7b0f74d87a29be (patch) | |
tree | cb4fde0440b01f79852ef45e1a2a2fe22ba6daca /Doc | |
parent | deaf090699a7312cccb0637409f44de3f382389b (diff) | |
download | cpython-47b9f83a83db288c652e43567c7b0f74d87a29be.zip cpython-47b9f83a83db288c652e43567c7b0f74d87a29be.tar.gz cpython-47b9f83a83db288c652e43567c7b0f74d87a29be.tar.bz2 |
GH-100485: Add math.sumprod() (GH-100677)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/itertools.rst | 11 | ||||
-rw-r--r-- | Doc/library/math.rst | 16 |
2 files changed, 18 insertions, 9 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 4ad679d..e7d2e13 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -33,7 +33,7 @@ by combining :func:`map` and :func:`count` to form ``map(f, count())``. These tools and their built-in counterparts also work well with the high-speed functions in the :mod:`operator` module. For example, the multiplication operator can be mapped across two vectors to form an efficient dot-product: -``sum(map(operator.mul, vector1, vector2))``. +``sum(starmap(operator.mul, zip(vec1, vec2, strict=True)))``. **Infinite iterators:** @@ -838,10 +838,6 @@ which incur interpreter overhead. "Returns the sequence elements n times" return chain.from_iterable(repeat(tuple(iterable), n)) - def dotproduct(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/ # convolve(data, [0.25, 0.25, 0.25, 0.25]) --> Moving average (blur) @@ -852,7 +848,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 dotproduct(kernel, window) + yield math.sumprod(kernel, window) def polynomial_from_roots(roots): """Compute a polynomial's coefficients from its roots. @@ -1211,9 +1207,6 @@ which incur interpreter overhead. >>> list(ncycles('abc', 3)) ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] - >>> dotproduct([1,2,3], [4,5,6]) - 32 - >>> data = [20, 40, 24, 32, 20, 28, 16] >>> list(convolve(data, [0.25, 0.25, 0.25, 0.25])) [5.0, 15.0, 21.0, 29.0, 29.0, 26.0, 24.0, 16.0, 11.0, 4.0] diff --git a/Doc/library/math.rst b/Doc/library/math.rst index aeebcaf..0e888c4 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -291,6 +291,22 @@ Number-theoretic and representation functions .. versionadded:: 3.7 +.. function:: sumprod(p, q) + + Return the sum of products of values from two iterables *p* and *q*. + + Raises :exc:`ValueError` if the inputs do not have the same length. + + Roughly equivalent to:: + + sum(itertools.starmap(operator.mul, zip(p, q, strict=true))) + + For float and mixed int/float inputs, the intermediate products + and sums are computed with extended precision. + + .. versionadded:: 3.12 + + .. function:: trunc(x) Return *x* with the fractional part |