diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2023-03-21 01:40:04 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-21 01:40:04 (GMT) |
commit | 4075fe1d8d609edbfff11526f2b1a9a7ce817d07 (patch) | |
tree | 94d13d9cdf1c43b5b4ac7cb794f1d7879cf40044 /Doc | |
parent | ef000eb3e2a8d0ecd51b6d44b390fefd820a61a6 (diff) | |
download | cpython-4075fe1d8d609edbfff11526f2b1a9a7ce817d07.zip cpython-4075fe1d8d609edbfff11526f2b1a9a7ce817d07.tar.gz cpython-4075fe1d8d609edbfff11526f2b1a9a7ce817d07.tar.bz2 |
Remove itermediate list. Expand docstring. (GH-102862)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/itertools.rst | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 9364f72..2427a8d 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -867,13 +867,17 @@ which incur interpreter overhead. yield math.sumprod(kernel, window) def polynomial_eval(coefficients, x): - "Evaluate a polynomial at a specific value." - # polynomial_eval([1, -4, -17, 60], x=2.5) --> 8.125 x³ -4x² -17x + 60 + """Evaluate a polynomial at a specific value. + + Computes with better numeric stability than Horner's method. + """ + # Evaluate x³ -4x² -17x + 60 at x = 2.5 + # polynomial_eval([1, -4, -17, 60], x=2.5) --> 8.125 n = len(coefficients) if n == 0: return x * 0 # coerce zero to the type of x - powers = list(accumulate(repeat(x, n - 1), operator.mul, initial=1)) - return math.sumprod(coefficients, reversed(powers)) + powers = accumulate(repeat(x, n - 1), operator.mul, initial=1) + return math.sumprod(reversed(coefficients), powers) def polynomial_from_roots(roots): """Compute a polynomial's coefficients from its roots. |