| | |
| | | import sys |
| | | import matplotlib |
| | | |
| | | from PySide6.QtWidgets import QApplication, QMainWindow |
| | | 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 MplCanvas(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(MplCanvas, self).__init__(fig) |
| | | |
| | | |
| | | class MainWindow(QMainWindow): |
| | |
| | | super(MainWindow, self).__init__() |
| | | self.ui = Ui_MainWindow() |
| | | self.ui.setupUi(self) |
| | | self.canvas = MplCanvas(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) |
| | | |
| | | 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() |
| | | epsilon = self.ui.espilon.text() |
| | | try: |
| | | f = parse_function(fn_expr) |
| | | (a, b) = (parse_value(start), parse_value(end)) |
| | | if epsilon is not None and len(epsilon.strip()) > 0: |
| | | eps = parse_value(epsilon) |
| | | print(f"plot {fn_expr}, {a}, {b}, epsilon = {eps}") |
| | | (self.x, self.y, *_) = numint_epsilon(f, a, b, eps) |
| | | self._update_plot_eps() |
| | | else: |
| | | section = self.ui.section.text() |
| | | sec = parse_value(section) |
| | | print(f"plot f(x) = {fn_expr}, {a}, {b}, section = {sec}") |
| | | (self.x, self.y, *_) = numint_section(f, a, b, sec) |
| | | self._update_plot_eps() |
| | | except Exception as ex: |
| | | print(ex) |
| | | pass |
| | | |
| | | def _update_plot_eps(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.plot(self.x, self.y, color="black") |
| | | self.canvas.draw() |
| | | |
| | | |
| | | |
| | | def main(): |
| | |
| | | |
| | | if __name__ == "__main__": |
| | | main() |
| | | |