-
Notifications
You must be signed in to change notification settings - Fork 4
/
bolzano.py
36 lines (30 loc) · 1.24 KB
/
bolzano.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Bolzano:
def __init__(self, func, bottomBound:float = 1, topBound:float = 3):
self.func = func
self.bottomBound = bottomBound
self.topBound = topBound
self.data = []
self.generate()
def check(x):
return 0 if not x else x/abs(x)
def computeNext(self, bottom, top):
next = bottom + (top - bottom) / 2
return {
"bottomBound": bottom,
"f_bottomBound": self.func(bottom),
"topBound": top,
"f_topBound": self.func(top),
"nextBound": next,
"f_nextBound": self.func(next)
}
def getNextBound(self, data: dict):
return (
data['nextBound'] if Bolzano.check(data['f_bottomBound']) == Bolzano.check(data['f_nextBound']) else data['bottomBound'],
data['nextBound'] if Bolzano.check(data['f_bottomBound']) != Bolzano.check(data['f_nextBound']) else data['topBound']
)
def generate(self, iteration:int = 5):
self.data = []
self.data.append(self.computeNext(self.bottomBound, self.topBound))
for i in range(iteration):
bottom, top = self.getNextBound(self.data[i])
self.data.append(self.computeNext(bottom, top))