@@ -789,6 +789,7 @@ which incur interpreter overhead.
789
789
.. testcode ::
790
790
791
791
import collections
792
+ import functools
792
793
import math
793
794
import operator
794
795
import random
@@ -1082,7 +1083,7 @@ The following recipes have a more mathematical flavor:
1082
1083
# convolve(data, [1, -2, 1]) --> 2nd finite difference (2nd derivative)
1083
1084
kernel = tuple(kernel)[::-1]
1084
1085
n = len(kernel)
1085
- padded_signal = chain(repeat(0, n-1), signal, [0] * ( n-1))
1086
+ padded_signal = chain(repeat(0, n-1), signal, repeat(0, n-1))
1086
1087
for window in sliding_window(padded_signal, n):
1087
1088
yield math.sumprod(kernel, window)
1088
1089
@@ -1092,10 +1093,8 @@ The following recipes have a more mathematical flavor:
1092
1093
(x - 5) (x + 4) (x - 3) expands to: x³ -4x² -17x + 60
1093
1094
"""
1094
1095
# polynomial_from_roots([5, -4, 3]) --> [1, -4, -17, 60]
1095
- expansion = [1]
1096
- for r in roots:
1097
- expansion = convolve(expansion, (1, -r))
1098
- return list(expansion)
1096
+ factors = zip(repeat(1), map(operator.neg, roots))
1097
+ return list(functools.reduce(convolve, factors, [1]))
1099
1098
1100
1099
def polynomial_eval(coefficients, x):
1101
1100
"""Evaluate a polynomial at a specific value.
0 commit comments