From 0aab12b1770cd7a53a7fdd9b805c3d7408c09896 Mon Sep 17 00:00:00 2001 From: spamegg Date: Mon, 27 Jul 2020 13:23:16 +0300 Subject: [PATCH 1/5] added type hints and doctests to arithmetic_analysis/bisection.py continuing in line with #2128 --- arithmetic_analysis/bisection.py | 41 +++++++++++++++++++------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/arithmetic_analysis/bisection.py b/arithmetic_analysis/bisection.py index 78582b025880..affdedccd4ee 100644 --- a/arithmetic_analysis/bisection.py +++ b/arithmetic_analysis/bisection.py @@ -1,12 +1,24 @@ -import math +from typing import Callable, Optional def bisection( - function, a, b -): # finds where the function becomes 0 in [a,b] using bolzano - - start = a - end = b + function: Callable[[float], float], a: float, b: float +) -> Optional[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) + couldn't find root in [ 2 , 1000 ] + >>> 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) + couldn't find root in [ 4 , 1000 ] + """ + 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: @@ -14,12 +26,12 @@ def bisection( 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 + print("couldn't find root in [", a, ",", b, "]") + return None 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: @@ -30,9 +42,6 @@ def bisection( return mid -def f(x): - return math.pow(x, 3) - 2 * x - 5 - - if __name__ == "__main__": - print(bisection(f, 1, 1000)) + import doctest + doctest.testmod() From 548e94626cbcc7f79fddf000fc0b954df99eb325 Mon Sep 17 00:00:00 2001 From: spamegg Date: Mon, 27 Jul 2020 15:32:49 +0300 Subject: [PATCH 2/5] modified arithmetic_analysis/bisection.py Put back print statement at the end, replaced algorithm's print statement with an exception. --- arithmetic_analysis/bisection.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/arithmetic_analysis/bisection.py b/arithmetic_analysis/bisection.py index affdedccd4ee..4ec04457698f 100644 --- a/arithmetic_analysis/bisection.py +++ b/arithmetic_analysis/bisection.py @@ -1,21 +1,23 @@ from typing import Callable, Optional -def bisection( - function: Callable[[float], float], a: float, b: float -) -> Optional[float]: +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) - couldn't find root in [ 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) - couldn't find root in [ 4 , 1000 ] + Traceback (most recent call last): + ... + Exception: could not find root in given interval. """ start: float = a end: float = b @@ -27,8 +29,7 @@ def bisection( function(a) * function(b) > 0 ): # if none of these are root and they are both positive or negative, # then this algorithm can't find the root - print("couldn't find root in [", a, ",", b, "]") - return None + raise Exception("could not find root in given interval.") else: mid: float = start + (end - start) / 2.0 while abs(start - mid) > 10 ** -7: # until precisely equals to 10^-7 @@ -42,6 +43,13 @@ def bisection( return mid +def f(x: float) -> float: + return x ** 3 - 2 * x - 5 + + if __name__ == "__main__": + print(bisection(f, 1, 1000)) + import doctest + doctest.testmod() From 5b4385963113102e9d5af6cb82a1d6f593ef7dcc Mon Sep 17 00:00:00 2001 From: spamegg Date: Mon, 27 Jul 2020 15:38:29 +0300 Subject: [PATCH 3/5] modified arithmetic_analysis/bisection.py Removed unnecessary type import "Optional" --- arithmetic_analysis/bisection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arithmetic_analysis/bisection.py b/arithmetic_analysis/bisection.py index 4ec04457698f..6d63778f7bad 100644 --- a/arithmetic_analysis/bisection.py +++ b/arithmetic_analysis/bisection.py @@ -1,4 +1,4 @@ -from typing import Callable, Optional +from typing import Callable def bisection(function: Callable[[float], float], a: float, b: float) -> float: From d58068569eba157d78c1da0d03bba3c78c6dd6fe Mon Sep 17 00:00:00 2001 From: spamegg Date: Mon, 27 Jul 2020 15:44:07 +0300 Subject: [PATCH 4/5] modified arithmetic_analysis/bisection.py Replaced generic Exception with ValueError. --- arithmetic_analysis/bisection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arithmetic_analysis/bisection.py b/arithmetic_analysis/bisection.py index 6d63778f7bad..4e18bf60a483 100644 --- a/arithmetic_analysis/bisection.py +++ b/arithmetic_analysis/bisection.py @@ -29,7 +29,7 @@ def bisection(function: Callable[[float], float], a: float, b: float) -> float: function(a) * function(b) > 0 ): # if none of these are root and they are both positive or negative, # then this algorithm can't find the root - raise Exception("could not find root in given interval.") + raise ValueError("could not find root in given interval.") else: mid: float = start + (end - start) / 2.0 while abs(start - mid) > 10 ** -7: # until precisely equals to 10^-7 From 4c3358e4830cff2bc8a23a5d80084e03245da9ec Mon Sep 17 00:00:00 2001 From: spamegg Date: Mon, 27 Jul 2020 15:45:47 +0300 Subject: [PATCH 5/5] modified arithmetic_analysis/bisection.py fixed doctests --- arithmetic_analysis/bisection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arithmetic_analysis/bisection.py b/arithmetic_analysis/bisection.py index 4e18bf60a483..0ef691678702 100644 --- a/arithmetic_analysis/bisection.py +++ b/arithmetic_analysis/bisection.py @@ -9,7 +9,7 @@ def bisection(function: Callable[[float], float], a: float, b: float) -> float: >>> bisection(lambda x: x ** 3 - 1, 2, 1000) Traceback (most recent call last): ... - Exception: could not find root in given interval. + ValueError: 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) @@ -17,7 +17,7 @@ def bisection(function: Callable[[float], float], a: float, b: float) -> float: >>> bisection(lambda x: x ** 2 - 4 * x + 3, 4, 1000) Traceback (most recent call last): ... - Exception: could not find root in given interval. + ValueError: could not find root in given interval. """ start: float = a end: float = b