Skip to content

Commit a9fa057

Browse files
committed
Merge branch 'master' of https://github.com/Dibbu-cell/Python
2 parents 3fad6df + 804b9af commit a9fa057

1 file changed

Lines changed: 37 additions & 13 deletions

File tree

maths/numerical_analysis/brents_method.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from collections.abc import Callable
1717

18+
1819
def brent_method(
1920
function: Callable[[float], float],
2021
lower_bound: float,
@@ -54,8 +55,7 @@ def brent_method(
5455
function_upper = function(upper_bound)
5556
if function_lower * function_upper >= 0:
5657
error_message = (
57-
"Root is not bracketed in the interval "
58-
f"[{lower_bound}, {upper_bound}]."
58+
f"Root is not bracketed in the interval [{lower_bound}, {upper_bound}]."
5959
)
6060
raise ValueError(error_message)
6161

@@ -74,21 +74,44 @@ def brent_method(
7474
if function_previous not in {function_lower, function_upper}:
7575
# Inverse quadratic interpolation
7676
s = (
77-
lower_bound * function_upper * function_previous
78-
/ ((function_lower - function_upper) * (function_lower - function_previous))
79-
+ upper_bound * function_lower * function_previous
80-
/ ((function_upper - function_lower) * (function_upper - function_previous))
81-
+ previous_bound * function_lower * function_upper
82-
/ ((function_previous - function_lower) * (function_previous - function_upper))
77+
lower_bound
78+
* function_upper
79+
* function_previous
80+
/ (
81+
(function_lower - function_upper)
82+
* (function_lower - function_previous)
83+
)
84+
+ upper_bound
85+
* function_lower
86+
* function_previous
87+
/ (
88+
(function_upper - function_lower)
89+
* (function_upper - function_previous)
90+
)
91+
+ previous_bound
92+
* function_lower
93+
* function_upper
94+
/ (
95+
(function_previous - function_lower)
96+
* (function_previous - function_upper)
97+
)
8398
)
8499
else:
85100
# Secant method
86-
s = upper_bound - function_upper * (upper_bound - lower_bound) / (function_upper - function_lower)
101+
s = upper_bound - function_upper * (upper_bound - lower_bound) / (
102+
function_upper - function_lower
103+
)
87104

88105
conditions = [
89-
not ((3 * lower_bound + upper_bound) / 4 < s < upper_bound if upper_bound > lower_bound else upper_bound < s < (3 * lower_bound + upper_bound) / 4),
90-
bisect_flag and abs(s - upper_bound) >= abs(upper_bound - previous_bound) / 2,
91-
not bisect_flag and abs(s - upper_bound) >= abs(previous_bound - previous_step) / 2,
106+
not (
107+
(3 * lower_bound + upper_bound) / 4 < s < upper_bound
108+
if upper_bound > lower_bound
109+
else upper_bound < s < (3 * lower_bound + upper_bound) / 4
110+
),
111+
bisect_flag
112+
and abs(s - upper_bound) >= abs(upper_bound - previous_bound) / 2,
113+
not bisect_flag
114+
and abs(s - upper_bound) >= abs(previous_bound - previous_step) / 2,
92115
bisect_flag and abs(upper_bound - previous_bound) < tolerance,
93116
not bisect_flag and abs(previous_bound - previous_step) < tolerance,
94117
]
@@ -121,4 +144,5 @@ def brent_method(
121144

122145
if __name__ == "__main__":
123146
import doctest
124-
doctest.testmod()
147+
148+
doctest.testmod()

0 commit comments

Comments
 (0)