From d20c959fa05171b7e415a5c461e1530cc40b9f6d Mon Sep 17 00:00:00 2001 From: Shoaib Date: Tue, 1 Oct 2019 02:12:16 +0530 Subject: [PATCH 1/2] maths-polynomial_evalutation --- maths/polynomial_evaluation.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 maths/polynomial_evaluation.py diff --git a/maths/polynomial_evaluation.py b/maths/polynomial_evaluation.py new file mode 100644 index 000000000000..ec8ed396328a --- /dev/null +++ b/maths/polynomial_evaluation.py @@ -0,0 +1,26 @@ +def evaluate_poly(poly, x): + """ + Objective: Computes the polynomial function for a given value x. + Returns that value. + Input Prams: + poly: tuple of numbers - value of cofficients + x: value for x in f(x) + Return: value of f(x) + """ + i = 0 + res = 0 + for c in poly: + res += c * (x ** i) + i += 1 + return res + + +if __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 + print (evaluate_poly(poly, x)) # f(-13) = 7.0(-13)^4 + 9.3(-13)^3 + 5.0(-13)^2 = 180339.9 + """ + poly = (0.0, 0.0, 5.0, 9.3, 7.0) + x = 10 + print(evaluate_poly(poly, x)) From e84b7bde35f51b48e18ba4887b6180a9898fa654 Mon Sep 17 00:00:00 2001 From: Shoaib Date: Tue, 1 Oct 2019 11:09:55 +0530 Subject: [PATCH 2/2] added doctest and removed redundancy --- maths/polynomial_evaluation.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/maths/polynomial_evaluation.py b/maths/polynomial_evaluation.py index ec8ed396328a..b4f18b9fa106 100644 --- a/maths/polynomial_evaluation.py +++ b/maths/polynomial_evaluation.py @@ -6,13 +6,12 @@ def evaluate_poly(poly, x): poly: tuple of numbers - value of cofficients x: value for x in f(x) Return: value of f(x) + + >>> evaluate_poly((0.0, 0.0, 5.0, 9.3, 7.0), 10) + 79800.0 """ - i = 0 - res = 0 - for c in poly: - res += c * (x ** i) - i += 1 - return res + + return sum(c*(x**i) for i, c in enumerate(poly)) if __name__ == "__main__":