Skip to content
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

Refactored arithmetic_analysis/secant_method.py #2263

Open
wants to merge 1 commit into
base: master
from

Conversation

@spamegg1
Copy link
Contributor

spamegg1 commented Aug 2, 2020

  • added missing Wikipedia link
  • added type hints and few more doctests
  • moved the function f to inside secant_method as a parameter func, instead of calling a globally defined function inside the algorithm body
  • renamed some variables and introduced some intermediate variables, to make it more readable and avoid recalculating f(x1)
  • replaced unused for-loop variable i with _

Describe your change:

  • Fix a bug or typo in an existing algorithm?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- added missing Wikipedia link
- added type hints and few more doctests
- moved the function `f` to inside `secant_method` as a parameter `func`, instead of calling a globally defined function inside the algorithm body
- renamed some variables and introduced some intermediate variables, to make it more readable and avoid recalculating `f(x1)`
- replaced unused for-loop variable `i` with `_`
@stale
Copy link

stale bot commented Sep 2, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Sep 2, 2020
lower: float = lower_bound
upper: float = upper_bound
Comment on lines +27 to +28

This comment has been minimized.

@cclauss

cclauss Sep 2, 2020

Member

Why do this? Why not just use lower_bound instead of lower?

@stale stale bot removed the wontfix label Sep 2, 2020
value: float = func(upper)
numerator: float = value * (upper - lower)
denominator: float = value - func(lower)
Comment on lines +30 to +32

This comment has been minimized.

@cclauss

cclauss Sep 2, 2020

Member
Suggested change
value: float = func(upper)
numerator: float = value * (upper - lower)
denominator: float = value - func(lower)
value = func(upper)
numerator = value * (upper - lower)
denominator = value - func(lower)

Type hints on local variables are not needed and they create a lot of visual and cognitive clutter. Python is smart enough to figure out that func() returns a float so value is a float, etc.

try:
lower, upper = upper, upper - numerator / denominator
except ZeroDivisionError:
raise ZeroDivisionError("Could not find root")
Comment on lines +33 to +36

This comment has been minimized.

@cclauss

cclauss Sep 2, 2020

Member
Suggested change
try:
lower, upper = upper, upper - numerator / denominator
except ZeroDivisionError:
raise ZeroDivisionError("Could not find root")
if denominator == 0.0:
raise ZeroDivisionError("Could not find root")
lower, upper = upper, upper - numerator / denominator
Copy link
Member

cclauss left a comment

Looking good...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.