-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
maths-polynomial_evalutation #1214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for i, c in enumerate(poly): # then delete lines 14 and 10 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, the whole function can be a one-liner: return sum(c * x ** i for i, c in enumerate(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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a doctest:
Then makes sure it passes locally with: python3 -m doctest -v maths/polynomial_evaluation.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cclauss thanks for the suggestion!
will do the changes.