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()