MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / horner

Function horner

maths/polynomial_evaluation.py:18–39  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

16
17
18def 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
42if __name__ == "__main__":

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected