-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrator.py
85 lines (58 loc) · 2.95 KB
/
integrator.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from abc import ABC, abstractmethod
from functions import Function
from integration import *
from chart import *
from utils import newLine, separator
class Integrator(ABC):
def __init__(self, numInt: NumericIntegration, chart: Chart) -> None:
self.numericIntegrator = numInt
self.chart = chart
@abstractmethod
def integrate(self, f: Function, a: float, b: float, resolution: int) -> float:
return self.numericIntegrator.integrate(f, a, b, resolution)
@abstractmethod
def showChart(self, f: Function, a: float, b: float, resolution: int) -> None:
self.chart.render(f, a, b, resolution)
@abstractmethod
def printAll(self, f: Function, a: float, b: float, resolution: int) -> None:
result = self.integrate(f, a, b, resolution)
print(f'[{self.numericIntegrator.name}] definite integral from {b} to {a} of {f.name}')
print(f'results: {result}, calculated by cycling {resolution} times')
print(f'while exact result is {f.integral(a,b)}')
separator()
newLine()
self.showChart(f, a, b, resolution)
class RectIntegrator(Integrator):
def __init__(self) -> None:
super().__init__(RectangleIntegration(), RectangleChart())
def integrate(self, f: Function, a: float, b: float, resolution: int) -> float:
return super().integrate(f, a, b, resolution)
def showChart(self, f: Function, a: float, b: float, resolution: int) -> None:
return super().showChart(f, a, b, resolution)
def printAll(self, f: Function, a: float, b: float, resolution: int) -> None:
return super().printAll(f, a, b, resolution)
class TrapIntegrator(Integrator):
def __init__(self) -> None:
super().__init__(TrapezoidIntegration(), TrapezoidChart())
def integrate(self, f: Function, a: float, b: float, resolution: int) -> float:
return super().integrate(f, a, b, resolution)
def showChart(self, f: Function, a: float, b: float, resolution: int) -> None:
return super().showChart(f, a, b, resolution)
def printAll(self, f: Function, a: float, b: float, resolution: int) -> None:
return super().printAll(f, a, b, resolution)
class SimpIntegrator(Integrator):
def __init__(self) -> None:
super().__init__(SimpsonIntegration(), SimpsonChart())
def integrate(self, f: Function, a: float, b: float, resolution: int) -> float:
return super().integrate(f, a, b, resolution)
def showChart(self, f: Function, a: float, b: float, resolution: int) -> None:
return super().showChart(f, a, b, resolution)
def printAll(self, f: Function, a: float, b: float, resolution: int) -> None:
return super().printAll(f, a, b, resolution)
class IntCreator:
def getRect() -> Integrator:
return RectIntegrator()
def getTrap() -> Integrator:
return TrapIntegrator()
def getSimp() -> Integrator:
return SimpIntegrator()