Hong-Phuc Bui
2024-09-27 45c96fe258657363ef4ad1a2ae93513ca4139e26
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import sys
import matplotlib
 
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout
 
from numint.input_parser import parse_function, parse_value
from numint.riemann_sum import numint_epsilon, numint_section
from numint.ui_mainwindow import Ui_MainWindow
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
 
matplotlib.use('QtAgg')
 
 
class PlotCanvas(FigureCanvasQTAgg):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(PlotCanvas, self).__init__(fig)
        self.left = 0.0
        self.right = 0.0
 
 
class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.canvas = PlotCanvas(self)
        self._update_plot()
 
        toolbar = NavigationToolbar(self.canvas, self)
        layout = QVBoxLayout()
        layout.addWidget(toolbar)
        layout.addWidget(self.canvas)
        self.ui.plotBox.setLayout(layout)
        self.ui.plotBtn.clicked.connect(self._update_plot)
        self.ui.breakMethod.buttonClicked.connect(self._activate_break_rule)
        self.ui.numberOfSectionsMethod.click()
        self.ui.plotBtn.click()
 
    def _update_plot(self):
        fn_expr = self.ui.function.text()
        self.fn_expr = fn_expr
        start = self.ui.startValue.text()
        end = self.ui.endValue.text()
 
        try:
            f = parse_function(fn_expr)
            (a, b) = (parse_value(start), parse_value(end))
            method_id = self.ui.breakMethod.checkedId()
            if method_id == self.ui.breakMethod.id(self.ui.minEpsilonMethod):
                epsilon = self.ui.espilon.text()
                eps = abs(parse_value(epsilon))
                self.ui.espilon.setText(f"{eps}")
                print(f"plot f(x) = {fn_expr}, {a}, {b}, epsilon = {eps}")
                (self.x, self.y, self.left, self.right, self.num_of_iterations, *_) = numint_epsilon(f, a, b, eps)
                self._plot_canvas()
            elif method_id == self.ui.breakMethod.id(self.ui.numberOfSectionsMethod):
                section = self.ui.section.text()
                sec = int(abs(parse_value(section)))
                self.ui.section.setText(f"{sec}")
                print(f"plot f(x) = {fn_expr}, {a}, {b}, section = {sec}")
                (self.x, self.y, self.left, self.right, self.num_of_iterations, *_) = numint_section(f, a, b, sec)
                self._plot_canvas()
        except Exception as ex:
            print(ex)
            pass
 
    def _plot_canvas(self):
        self.canvas.axes.cla()  # Clear the canvas.
        # self.canvas.axes.stairs(self.y[1:],   self.x, baseline=0, fill=True, alpha=0.5, label=self.fn_expr)
        # self.canvas.axes.stairs(self.y[0:-1], self.x, baseline=0, fill=True, alpha=0.5, label=self.fn_expr)
        self.canvas.axes.step(self.x, self.y, color="blue",   where="pre",  label="right")
        self.canvas.axes.step(self.x, self.y, color="orange", where="post", label="left")
 
        self.canvas.axes.plot(self.x, self.y, color="black", label=self.fn_expr, linewidth=0.75)
        self.canvas.draw()
 
        self.ui.leftSum.setText(f"{self.left}")
        self.ui.rightSum.setText(f"{self.right}")
        self.ui.numOfSections.setText(f"{self.num_of_iterations}")
 
    def _activate_break_rule(self):
        method_id = self.ui.breakMethod.checkedId()
        print(method_id)
        if method_id == self.ui.breakMethod.id(self.ui.numberOfSectionsMethod):
            print("activate num of sec")
            self.ui.section.setEnabled(True)
            self.ui.section.setFocus()
            self.ui.espilon.setEnabled(False)
 
        elif method_id == self.ui.breakMethod.id(self.ui.minEpsilonMethod):
            print("activate minimum epsilon method")
            self.ui.section.setEnabled(False)
            self.ui.espilon.setEnabled(True)
            self.ui.espilon.setFocus()
 
 
 
 
def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
 
    sys.exit(app.exec())
 
 
if __name__ == "__main__":
    main()