同步操作将从 编程语言算法集/Python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
from typing import Sequencedef evaluate_poly(poly: Sequence[float], x: float) -> float:"""Evaluate a polynomial f(x) at specified point x and return the value.Arguments:poly -- the coefficients of a polynomial as an iterable in order ofascending degreex -- the point at which to evaluate the polynomial>>> evaluate_poly((0.0, 0.0, 5.0, 9.3, 7.0), 10.0)79800.0"""return sum(c * (x ** i) for i, c in enumerate(poly))def horner(poly: Sequence[float], x: float) -> float:"""Evaluate a polynomial at specified point using Horner's method.In terms of computational complexity, Horner's method is an efficient methodof evaluating a polynomial. It avoids the use of expensive exponentiation,and instead uses only multiplication and addition to evaluate the polynomialin O(n), where n is the degree of the polynomial.https://en.wikipedia.org/wiki/Horner's_methodArguments:poly -- the coefficients of a polynomial as an iterable in order ofascending degreex -- the point at which to evaluate the polynomial>>> horner((0.0, 0.0, 5.0, 9.3, 7.0), 10.0)79800.0"""result = 0.0for coeff in reversed(poly):result = result * x + coeffreturn resultif __name__ == "__main__":"""Example:>>> poly = (0.0, 0.0, 5.0, 9.3, 7.0) # f(x) = 7.0x^4 + 9.3x^3 + 5.0x^2>>> x = -13.0>>> # f(-13) = 7.0(-13)^4 + 9.3(-13)^3 + 5.0(-13)^2 = 180339.9>>> print(evaluate_poly(poly, x))180339.9"""poly = (0.0, 0.0, 5.0, 9.3, 7.0)x = 10.0print(evaluate_poly(poly, x))print(horner(poly, x))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。