mirror of
https://github.com/python/cpython.git
synced 2025-10-03 13:45:29 +00:00
Add "strict" to dotproduct(). Add docstring. Factor-out common code. (GH-100480)
(cherry picked from commit f89de679ff
)
Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
This commit is contained in:
parent
d5eb2f4747
commit
9477594374
1 changed files with 3 additions and 2 deletions
|
@ -795,7 +795,8 @@ which incur interpreter overhead.
|
||||||
return chain.from_iterable(repeat(tuple(iterable), n))
|
return chain.from_iterable(repeat(tuple(iterable), n))
|
||||||
|
|
||||||
def dotproduct(vec1, vec2):
|
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):
|
def convolve(signal, kernel):
|
||||||
# See: https://betterexplained.com/articles/intuitive-convolution/
|
# See: https://betterexplained.com/articles/intuitive-convolution/
|
||||||
|
@ -807,7 +808,7 @@ which incur interpreter overhead.
|
||||||
window = collections.deque([0], maxlen=n) * n
|
window = collections.deque([0], maxlen=n) * n
|
||||||
for x in chain(signal, repeat(0, n-1)):
|
for x in chain(signal, repeat(0, n-1)):
|
||||||
window.append(x)
|
window.append(x)
|
||||||
yield sum(map(operator.mul, kernel, window))
|
yield dotproduct(kernel, window)
|
||||||
|
|
||||||
def polynomial_from_roots(roots):
|
def polynomial_from_roots(roots):
|
||||||
"""Compute a polynomial's coefficients from its roots.
|
"""Compute a polynomial's coefficients from its roots.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue