Skip to content

Commit 4075fe1

Browse files
authored
Remove itermediate list. Expand docstring. (GH-102862)
1 parent ef000eb commit 4075fe1

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Doc/library/itertools.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -867,13 +867,17 @@ which incur interpreter overhead.
867867
yield math.sumprod(kernel, window)
868868

869869
def polynomial_eval(coefficients, x):
870-
"Evaluate a polynomial at a specific value."
871-
# polynomial_eval([1, -4, -17, 60], x=2.5) --> 8.125 x³ -4x² -17x + 60
870+
"""Evaluate a polynomial at a specific value.
871+
872+
Computes with better numeric stability than Horner's method.
873+
"""
874+
# Evaluate x³ -4x² -17x + 60 at x = 2.5
875+
# polynomial_eval([1, -4, -17, 60], x=2.5) --> 8.125
872876
n = len(coefficients)
873877
if n == 0:
874878
return x * 0 # coerce zero to the type of x
875-
powers = list(accumulate(repeat(x, n - 1), operator.mul, initial=1))
876-
return math.sumprod(coefficients, reversed(powers))
879+
powers = accumulate(repeat(x, n - 1), operator.mul, initial=1)
880+
return math.sumprod(reversed(coefficients), powers)
877881

878882
def polynomial_from_roots(roots):
879883
"""Compute a polynomial's coefficients from its roots.

0 commit comments

Comments
 (0)