Skip to content

Commit

Permalink
Update convolve recipe with note about implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bbayles committed Jul 21, 2023
1 parent 45b1080 commit 9514d07
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions more_itertools/recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,11 +722,14 @@ def convolve(signal, kernel):
is immediately consumed and stored.
"""
# This implementation intentionally doesn't match the one in the itertools
# documentation.
kernel = tuple(kernel)[::-1]
n = len(kernel)
padded_signal = chain(repeat(0, n - 1), signal, repeat(0, n - 1))
windowed_signal = sliding_window(padded_signal, n)
return map(_sumprod, repeat(kernel), windowed_signal)
window = deque([0], maxlen=n) * n
for x in chain(signal, repeat(0, n - 1)):
window.append(x)
yield _sumprod(kernel, window)


def before_and_after(predicate, it):
Expand Down

0 comments on commit 9514d07

Please sign in to comment.