Skip to content

added type hints and doctests to arithmetic_analysis/bisection.py #2241

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

Merged
merged 5 commits into from
Jul 27, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions arithmetic_analysis/bisection.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import math
from typing import Callable


def bisection(
function, a, b
): # finds where the function becomes 0 in [a,b] using bolzano

start = a
end = b
def bisection(function: Callable[[float], float], a: float, b: float) -> float:
"""
finds where function becomes 0 in [a,b] using bolzano
>>> bisection(lambda x: x ** 3 - 1, -5, 5)
1.0000000149011612
>>> bisection(lambda x: x ** 3 - 1, 2, 1000)
Traceback (most recent call last):
...
Exception: could not find root in given interval.
>>> bisection(lambda x: x ** 2 - 4 * x + 3, 0, 2)
1.0
>>> bisection(lambda x: x ** 2 - 4 * x + 3, 2, 4)
3.0
>>> bisection(lambda x: x ** 2 - 4 * x + 3, 4, 1000)
Traceback (most recent call last):
...
Exception: could not find root in given interval.
Copy link
Member

Choose a reason for hiding this comment

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

ValueError please, not generic Exception.

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.

"""
start: float = a
end: float = b
if function(a) == 0: # one of the a or b is a root for the function
return a
elif function(b) == 0:
return b
elif (
function(a) * function(b) > 0
): # if none of these are root and they are both positive or negative,
# then his algorithm can't find the root
print("couldn't find root in [a,b]")
return
# then this algorithm can't find the root
raise Exception("could not find root in given interval.")
else:
mid = start + (end - start) / 2.0
while abs(start - mid) > 10 ** -7: # until we achieve precise equals to 10^-7
mid: float = start + (end - start) / 2.0
while abs(start - mid) > 10 ** -7: # until precisely equals to 10^-7
if function(mid) == 0:
return mid
elif function(mid) * function(start) < 0:
Expand All @@ -30,9 +43,13 @@ def bisection(
return mid


def f(x):
return math.pow(x, 3) - 2 * x - 5
def f(x: float) -> float:
return x ** 3 - 2 * x - 5


if __name__ == "__main__":
print(bisection(f, 1, 1000))
Copy link
Member

Choose a reason for hiding this comment

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

Why remove this code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I thought I was supposed to move all tests into the doctest. I guess I misunderstood. I can put it back in for sure.


import doctest

doctest.testmod()