From 8a1f84ef11be8c9158f4a21008a504adb06c2cc1 Mon Sep 17 00:00:00 2001 From: w2kzx80 <68893211+w2kzx80@users.noreply.github.com> Date: Tue, 3 Oct 2023 01:44:34 +0300 Subject: [PATCH 1/2] Update bisection.py alittle optimization?) --- arithmetic_analysis/bisection.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/arithmetic_analysis/bisection.py b/arithmetic_analysis/bisection.py index e359cc170072..bee393078de8 100644 --- a/arithmetic_analysis/bisection.py +++ b/arithmetic_analysis/bisection.py @@ -19,23 +19,26 @@ def bisection(function: Callable[[float], float], a: float, b: float) -> float: ... ValueError: could not find root in given interval. """ - start: float = a - end: float = b - if function(a) == 0: # one of the a or b is a root for the function + fna: float = function(a) + if fna == 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 + fna * 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 ValueError("could not find root in given interval.") else: + start: float = a + end: float = b mid: float = start + (end - start) / 2.0 + fnmid: float = 0 while abs(start - mid) > 10**-7: # until precisely equals to 10^-7 - if function(mid) == 0: + fnmid = function(mid) + if fnmid == 0: return mid - elif function(mid) * function(start) < 0: + elif fnmid * function(start) < 0: end = mid else: start = mid From 6f86409349272f54aa41844ee1946ed07bdf1868 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sun, 26 Jan 2025 17:39:09 +0300 Subject: [PATCH 2/2] Delete arithmetic_analysis/bisection.py --- arithmetic_analysis/bisection.py | 58 -------------------------------- 1 file changed, 58 deletions(-) delete mode 100644 arithmetic_analysis/bisection.py diff --git a/arithmetic_analysis/bisection.py b/arithmetic_analysis/bisection.py deleted file mode 100644 index bee393078de8..000000000000 --- a/arithmetic_analysis/bisection.py +++ /dev/null @@ -1,58 +0,0 @@ -from collections.abc import Callable - - -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): - ... - 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) - 3.0 - >>> bisection(lambda x: x ** 2 - 4 * x + 3, 4, 1000) - Traceback (most recent call last): - ... - ValueError: could not find root in given interval. - """ - fna: float = function(a) - if fna == 0: # one of the a or b is a root for the function - return a - elif function(b) == 0: - return b - elif ( - fna * 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 ValueError("could not find root in given interval.") - else: - start: float = a - end: float = b - mid: float = start + (end - start) / 2.0 - fnmid: float = 0 - while abs(start - mid) > 10**-7: # until precisely equals to 10^-7 - fnmid = function(mid) - if fnmid == 0: - return mid - elif fnmid * function(start) < 0: - end = mid - else: - start = mid - mid = start + (end - start) / 2.0 - 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()