Evaluate a polynomial at specified point using Horner's method. In terms of computational complexity, Horner's method is an efficient method of evaluating a polynomial. It avoids the use of expensive exponentiation, and instead uses only multiplication and addition to evaluate the polyn
(poly: Sequence[float], x: float)
| 16 | |
| 17 | |
| 18 | def horner(poly: Sequence[float], x: float) -> float: |
| 19 | """Evaluate a polynomial at specified point using Horner's method. |
| 20 | |
| 21 | In terms of computational complexity, Horner's method is an efficient method |
| 22 | of evaluating a polynomial. It avoids the use of expensive exponentiation, |
| 23 | and instead uses only multiplication and addition to evaluate the polynomial |
| 24 | in O(n), where n is the degree of the polynomial. |
| 25 | |
| 26 | https://en.wikipedia.org/wiki/Horner's_method |
| 27 | |
| 28 | Arguments: |
| 29 | poly -- the coefficients of a polynomial as an iterable in order of |
| 30 | ascending degree |
| 31 | x -- the point at which to evaluate the polynomial |
| 32 | |
| 33 | >>> horner((0.0, 0.0, 5.0, 9.3, 7.0), 10.0) |
| 34 | 79800.0 |
| 35 | """ |
| 36 | result = 0.0 |
| 37 | for coeff in reversed(poly): |
| 38 | result = result * x + coeff |
| 39 | return result |
| 40 | |
| 41 | |
| 42 | if __name__ == "__main__": |
no outgoing calls
no test coverage detected