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

added type hints and doctests to arithmetic_analysis/newton_method.py #2259

Merged
merged 4 commits into from
Aug 1, 2020
Merged

Conversation

spamegg1
Copy link
Contributor

@spamegg1 spamegg1 commented Aug 1, 2020

Continuing #2128
Also changed some variable names, made them more descriptive.

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.

Continuing #2128
Also changed some variable names, made them more descriptive.

# function is the f(x) and derivative is the f'(x)
def newton(
function: Callable[[float], float],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to defined a type for Callable[[float], float] and then use that type to simplify the call parameters?

Copy link
Contributor Author

@spamegg1 spamegg1 Aug 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I think there is. I could try defining a type hint alias as described here:
https://docs.python.org/3/library/typing.html#type-aliases
Let me try and see if it passes mypy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you know? It does work!

Comment on lines 34 to 37
if derivative(prev_guess) == 0:
raise ZeroDivisionError("float division by zero, could not find root")

next_guess: float = prev_guess - function(prev_guess) / derivative(prev_guess)
Copy link
Member

@cclauss cclauss Aug 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if derivative(prev_guess) == 0:
raise ZeroDivisionError("float division by zero, could not find root")
next_guess: float = prev_guess - function(prev_guess) / derivative(prev_guess)
try:
next_guess: float = prev_guess - function(prev_guess) / derivative(prev_guess)
except ZeroDivisionError:
raise ZeroDivisionError("Could not find root")

Avoid calculating the derivative twice. The error type already says that we have a division by zero -- we do not need to repeat.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, got it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @spamegg1, from OSSU here. I was just going through this issue and looking at the changes made and this will create a problem where if there is a ZeroDivisionError then two exceptions will be raised. This PR is merged so it's better for @cclauss to make this small change.

>>> try:
...     a = 1/0
... except ZeroDivisionError:
...     raise ZeroDivisionError("Not possible")
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
ZeroDivisionError: Not possible

Why? the except block is used to handle the exceptions, if you want to raise an exception inside it you need to use the from clause like so:

>>> try:
...     a = 1/0
... except ZeroDivisionError:
...     raise ZeroDivisionError("Not possible") from None
...
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
ZeroDivisionError: Not possible

You can also use from in other ways which I won't go into detail much here but just wanted to point this out. 😁

added a type alias for Callable[[float], float] and cleaned up the exception handling
Comment on lines 36 to 38
if abs(prev_guess - next_guess) < 10 ** -5:
return next_guess
prev_guess = next_guess
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 36-38 can not raise a ZeroDivisionError so they should not be in the try block as discussed in PEP8.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, didn't know that. Understood.

@cclauss cclauss merged commit 473072b into TheAlgorithms:master Aug 1, 2020
@TravisBuddy
Copy link

Hey @spamegg1,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

View build log

TravisBuddy Request Identifier: 7f7e1bf0-d3f9-11ea-9b7b-6b7bbdba586d

@spamegg1
Copy link
Contributor Author

spamegg1 commented Aug 1, 2020

Hi @cclauss I know this got merged, but something weird must have happened during the merging process. In your last commit d55491a, Line 30 changed from prev_guess: float = starting_int (which is what I had in my last commit 8e20e61) to prev_guess float(starting_int) which makes the file currently broken. I already deleted my forked repo and realized this only later. If you could change it back it would be greatly appreciated. I probably should not make another PR over this.

@cclauss
Copy link
Member

cclauss commented Aug 1, 2020

It was my mistake... It is now fixed on master. Thanks!

stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
…TheAlgorithms#2259)

* added type hints and doctests to arithmetic_analysis/newton_method.py

Continuing TheAlgorithms#2128
Also changed some variable names, made them more descriptive.

* Added type hints and doctests to arithmetic_analysis/newton_method.py

added a type alias for Callable[[float], float] and cleaned up the exception handling

* added type hints and doctests to arithmetic_analysis/newton_method.py

improved exception handling

* Update newton_method.py

Co-authored-by: Christian Clauss <cclauss@me.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants