Open alexbprofit opened 3 years ago
import sys
import matplotlib
from PyQt5 import QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import top
import numpy as np
from matplotlib.backends.backend_qt5agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
matplotlib.use('qt5agg')
from matplotlib.figure import Figure
class SetType(QMainWindow):
def __init__(self):
super(SetType, self).__init__()
self.params = []
self.rbtn1 = QRadioButton("Поліном")
self.rbtn2 = QRadioButton("Рівняння Кеплера")
self.rbtn3 = QRadioButton("доля інфікованого населення")
self.rbtn4 = QRadioButton("y = bx^m - ln(x) - a")
self.widget = QWidget()
self.l = QVBoxLayout()
self.l.maximumSize()
self.l.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.saveChange = QPushButton("Зберегти")
self.saveChange.pressed.connect(self.change)
self.priority = QLineEdit()
self.priority.setDisabled(True)
self.check = 0
self.l.addWidget(self.rbtn1)
self.l.addWidget(self.rbtn2)
self.l.addWidget(self.rbtn3)
self.l.addWidget(self.rbtn4)
self.l.addWidget(self.priority)
self.l.addWidget(self.saveChange)
self.r = QVBoxLayout()
self.r.setAlignment(Qt.AlignmentFlag.AlignRight)
self.rb1 = QRadioButton("Ньютона")
self.rb2 = QRadioButton("Половинного поділу")
self.rb3 = QRadioButton("Простої ітерації")
self.rb4 = QRadioButton("Січних")
self.rb5 = QRadioButton("Стефенсена")
self.rb6 = QRadioButton("Ньютона(спрощений)")
self.rb7 = QRadioButton("Ньютона(модиф.)")
self.rb9 = QRadioButton("Ньютона(розшир.)")
self.rb10 = QRadioButton("Хорд")
self.rb11 = QRadioButton("Ньютона(різницевий)")
self.r.addWidget(self.rb1)
self.r.addWidget(self.rb2)
self.r.addWidget(self.rb3)
self.r.addWidget(self.rb4)
self.r.addWidget(self.rb5)
self.r.addWidget(self.rb6)
self.r.addWidget(self.rb7)
self.r.addWidget(self.rb9)
self.r.addWidget(self.rb10)
self.r.addWidget(self.rb11)
self.saveMode = QPushButton("Зберегти")
self.saveMode.pressed.connect(self.changes)
self.r.addWidget(self.saveMode)
self.centralLayout = QVBoxLayout()
self.nums = 0
self.descLayout = QVBoxLayout()
self.descLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
self.desc = QLabel()
self.inputDataLayout = QHBoxLayout()
self.inputDataLayout.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.hint = QLabel("a0: ")
self.hint.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.inputParameter = QLineEdit()
self.inputParameter.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.accuracy = QLabel("Точність")
self.accuracy.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.accuracyParameter = QLineEdit()
self.accuracyParameter.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.confirmButton = QPushButton("Зберегти")
self.confirmButton.pressed.connect(self.saveCheck)
self.inputDataLayout.addWidget(self.hint)
self.inputDataLayout.addWidget(self.inputParameter)
self.inputDataLayout.addWidget(self.accuracy)
self.inputDataLayout.addWidget(self.accuracyParameter)
self.inputDataLayout.addWidget(self.confirmButton)
self.root = QLabel()
self.buttons = QHBoxLayout()
self.addParamButton = QPushButton("Ввести")
self.addParamButton.pressed.connect(self.accept)
self.addParamButton.setFixedSize(200, 32)
self.printResultButton = QPushButton("Обчислити")
self.printResultButton.setFixedSize(200, 32)
self.printResultButton.pressed.connect(self.confirm)
self.buttons.addWidget(self.addParamButton)
self.buttons.addWidget(self.printResultButton)
self.descLayout.addWidget(self.desc)
self.descLayout.addLayout(self.inputDataLayout)
self.descLayout.addLayout(self.buttons)
self.descLayout.addWidget(self.root)
self.static_canvas = FigureCanvas(Figure(figsize=(300, 180)))
self.faceResult = QVBoxLayout()
self.result = QLabel()
self.resetWindow = QPushButton("Очистити")
self.resetWindow.pressed.connect(self.repeat)
self.exitProgram = QPushButton("Завершити роботу")
self.exitProgram.pressed.connect(sys.exit)
self.faceResult.addWidget(self.result)
self.faceResult.addWidget(self.resetWindow)
self.faceResult.addWidget(self.exitProgram)
self.centralLayout.addLayout(self.descLayout)
self.centralLayout.addWidget(self.static_canvas)
self.centralLayout.addLayout(self.faceResult)
self.aaa = 0
self.bbb = 0
self.h = QHBoxLayout()
self.h.addLayout(self.l)
self.h.addLayout(self.centralLayout)
self.h.addLayout(self.r)
self.widget.setLayout(self.h)
self.setCentralWidget(self.widget)
self.show()
def hover(self):
self.style = QStyle()
self.style.setProperty("background-color", QColor().green())
def saveCheck(self):
self.acc = float(self.accuracyParameter.text())
def repeat(self):
self.removeToolBar(self.toolBar)
self.params = []
self.rbtn1 = QRadioButton("Поліном")
self.rbtn2 = QRadioButton("Рівняння Кеплера")
self.rbtn3 = QRadioButton("доля інфікованого населення")
self.rbtn4 = QRadioButton("y = bx^m - ln(x) - a")
self.widget = QWidget()
self.l = QVBoxLayout()
self.l.maximumSize()
self.l.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.saveChange = QPushButton("Зберегти")
self.saveChange.pressed.connect(self.change)
self.priority = QLineEdit()
self.priority.setDisabled(True)
self.check = 0
self.l.addWidget(self.rbtn1)
self.l.addWidget(self.rbtn2)
self.l.addWidget(self.rbtn3)
self.l.addWidget(self.rbtn4)
self.l.addWidget(self.priority)
self.l.addWidget(self.saveChange)
self.r = QVBoxLayout()
self.r.setAlignment(Qt.AlignmentFlag.AlignRight)
self.rb1 = QRadioButton("Ньютона")
self.rb2 = QRadioButton("Половинного поділу")
self.rb3 = QRadioButton("Простої ітерації")
self.rb4 = QRadioButton("Січних")
self.rb5 = QRadioButton("Стефенсена")
self.rb6 = QRadioButton("Ньютона(спрощений)")
self.rb7 = QRadioButton("Ньютона(модиф.)")
self.rb9 = QRadioButton("Ньютона(розшир.)")
self.rb10 = QRadioButton("Хорд")
self.rb11 = QRadioButton("Ньютона(різницевий)")
self.r.addWidget(self.rb1)
self.r.addWidget(self.rb2)
self.r.addWidget(self.rb3)
self.r.addWidget(self.rb4)
self.r.addWidget(self.rb5)
self.r.addWidget(self.rb6)
self.r.addWidget(self.rb7)
self.r.addWidget(self.rb9)
self.r.addWidget(self.rb10)
self.r.addWidget(self.rb11)
self.saveMode = QPushButton("Зберегти")
self.saveMode.pressed.connect(self.changes)
self.r.addWidget(self.saveMode)
self.centralLayout = QVBoxLayout()
self.nums = 0
self.descLayout = QVBoxLayout()
self.descLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
self.desc = QLabel()
self.inputDataLayout = QHBoxLayout()
self.inputDataLayout.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.hint = QLabel("a0: ")
self.hint.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.inputParameter = QLineEdit()
self.inputParameter.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.accuracy = QLabel("Точність")
self.accuracy.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.accuracyParameter = QLineEdit()
self.accuracyParameter.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.confirmButton = QPushButton("Зберегти")
self.confirmButton.pressed.connect(self.saveCheck)
self.inputDataLayout.addWidget(self.hint)
self.inputDataLayout.addWidget(self.inputParameter)
self.inputDataLayout.addWidget(self.accuracy)
self.inputDataLayout.addWidget(self.accuracyParameter)
self.inputDataLayout.addWidget(self.confirmButton)
self.root = QLabel()
self.buttons = QHBoxLayout()
self.addParamButton = QPushButton("Ввести")
self.addParamButton.pressed.connect(self.accept)
self.addParamButton.setFixedSize(200, 32)
self.printResultButton = QPushButton("Обчислити")
self.printResultButton.setFixedSize(200, 32)
self.printResultButton.pressed.connect(self.confirm)
self.buttons.addWidget(self.addParamButton)
self.buttons.addWidget(self.printResultButton)
self.descLayout.addWidget(self.desc)
self.descLayout.addLayout(self.inputDataLayout)
self.descLayout.addLayout(self.buttons)
self.descLayout.addWidget(self.root)
self.static_canvas = FigureCanvas(Figure(figsize=(300, 180)))
self.faceResult = QVBoxLayout()
self.result = QLabel()
self.resetWindow = QPushButton("Очистити")
self.resetWindow.pressed.connect(self.repeat)
self.exitProgram = QPushButton("Завершити роботу")
self.exitProgram.pressed.connect(sys.exit)
self.faceResult.addWidget(self.result)
self.faceResult.addWidget(self.resetWindow)
self.faceResult.addWidget(self.exitProgram)
self.centralLayout.addLayout(self.descLayout)
self.centralLayout.addWidget(self.static_canvas)
self.centralLayout.addLayout(self.faceResult)
self.aaa = 0
self.bbb = 0
self.h = QHBoxLayout()
self.h.addLayout(self.l)
self.h.addLayout(self.centralLayout)
self.h.addLayout(self.r)
self.widget.setLayout(self.h)
self.setCentralWidget(self.widget)
self.show()
def change(self):
self.title = "Вибране рівняння: "
if self.rbtn1.isChecked():
self.desc.setText(self.title + "y = a0 + a1*x + a2*x^2 + ..., a0, a1, ... \n - параметри")
self.check = 1
self.priority.setDisabled(False)
self.aaa += 1
if self.aaa == 2:
self.xy = int(self.priority.text())
elif self.rbtn2.isChecked():
self.desc.setText(self.title + "y = x - a - b*sin(x), a, b - параметри")
self.check = 2
self.priority.setDisabled(True)
elif self.rbtn3.isChecked():
self.desc.setText(self.title + "y = x - e^(a*(x-1)), a - параметр")
self.check = 3
self.priority.setDisabled(True)
elif self.rbtn4.isChecked():
self.desc.setText(self.title + self.rbtn4.text() + ",a,b,m - параметри")
self.check = 4
self.priority.setDisabled(True)
else:
self.warning("Вкажіть тип рівняння")
def changes(self):
if self.rb1.isChecked():
self.nums = 1
elif self.rb2.isChecked():
self.nums = 2
elif self.rb3.isChecked():
self.nums = 3
elif self.rb4.isChecked():
self.nums = 4
elif self.rb5.isChecked():
self.nums = 5
elif self.rb6.isChecked():
self.nums = 6
elif self.rb7.isChecked():
self.nums = 7
elif self.rb9.isChecked():
self.nums = 9
elif self.rb10.isChecked():
self.nums = 10
elif self.rb11.isChecked():
self.nums = 11
else:
self.warning("Вкажіть метод знаходження числовго розв'язку")
def plot(self):
self.mainWidget.close()
self.bbb += 1
if self.bbb >= 2:
self.removeToolBar(self.toolBar)
if self.check == 0 or self.nums == 0:
self.warning("Вкажіть тип рівняння та/або метод знаходження числового розв'язку")
else:
self._static_ax = self.static_canvas.figure.subplots()
t = np.linspace(-10, 10, 120)
xs = []
for i in range(len(t)):
x = top.f(self.params, t[i], self.check)
xs.append(x)
z = [0 for _ in range(len(t))]
self._static_ax.plot(t, xs)
self._static_ax.plot(z, xs)
self._static_ax.plot(t, z)
self.toolBar = NavigationToolbar(self.static_canvas, self)
self.addToolBar(self.toolBar)
self.perform()
def perform(self):
string = ''
result = top.main(top.f, self.params, self.nums, self.acc, self.check)
print(result)
for i in range(len(result)):
string += "%.8f" % (result[i][0]) + "\t" + str(result[i][1]) + "\n"
self.result.setText("Корені:" + "\n" + string)
def confirm(self):
if len(self.params) != self.xy + 1:
self.warning("Введіть всі параметри")
else:
self.mainWidget = QMainWindow()
self.windows = QWidget()
self.msg = QLabel()
self.msg.setFont(QFont("Arial", 18, 50, False))
self.msg.setText("Підтвердити введені дані?")
self.msgVLayout = QVBoxLayout()
self.msgHLayout = QHBoxLayout()
self.acceptButton = QPushButton("Так")
self.rejectButton = QPushButton("Ні")
self.acceptButton.pressed.connect(self.plot)
self.rejectButton.pressed.connect(self.redirectToBack)
self.msgHLayout.addWidget(self.acceptButton)
self.msgHLayout.addWidget(self.rejectButton)
self.msgVLayout.addWidget(self.msg)
self.msgVLayout.addLayout(self.msgHLayout)
self.windows.setLayout(self.msgVLayout)
self.mainWidget.setCentralWidget(self.windows)
self.mainWidget.show()
def warning(self, texts):
self.warnWindow = QMainWindow()
self.warnMsg = QWidget()
self.warnlayout = QVBoxLayout()
self.text = QLabel(texts)
self.acceptButtons = QPushButton("ОК")
self.acceptButtons.pressed.connect(self.warnWindow.close)
self.text.setFont(QFont("Arial", 18, 50, False))
self.warnlayout.addWidget(self.text)
self.warnlayout.addWidget(self.acceptButtons)
self.warnMsg.setLayout(self.warnlayout)
self.warnWindow.setCentralWidget(self.warnMsg)
self.warnWindow.show()
def redirectToBack(self):
self.params = []
self.mainWidget.close()
def accept(self):
self.title = "Кінцеве рівняння: "
string = self.title + 'y = '
if self.nums == 0 or self.check == 0:
self.warning("Вкажіть тип рівняння та/або метод знаходження числовго розв'язку")
else:
if self.check == 1:
if len(self.params) == self.xy + 1:
self.addParamButton.setText("Друк рівняння")
for i in range(len(self.params)):
if i == 0:
string += "%.6f*x^%.2f" % (self.params[i], i)
elif self.params[i] < 0:
string += "-%.6f*x^%.2f" % (abs(self.params[i]), i)
else:
string += "+%.6f*x^%.2f" % (abs(self.params[i]), i)
self.root.setText(string)
else:
try:
self.data = compile(self.inputParameter.text(), 'string', 'eval')
except SyntaxError:
self.warning("Поле введення параметру не може бути порожнім")
else:
self.params.append(eval(self.data))
self.inputParameter.clear()
self.hint.setText("a{}: ".format(len(self.params)))
else:
if self.check == 2:
self.xy = 1
elif self.check == 4:
self.xy = 2
else:
self.xy = 0
if len(self.params) == self.xy + 1:
if self.check == 2:
self.addParamButton.setText("Друк рівняння")
if self.params[0] < 0:
string += "x + %.6f - %.6f*sin(x)" % (self.params[0], self.params[1])
elif self.params[1] < 0:
string += "x - %.6f + %.6f*sin(x)" % (self.params[0], self.params[1])
elif self.params[0] < 0 and self.params[1] < 0:
string += "x + %.6f + %.6f*sin(x)" % (self.params[0], self.params[1])
else:
string += "x - %.6f - %.6f*sin(x)" % (self.params[0], self.params[1])
elif self.check == 4:
self.addParamButton.setText("Друк рівняння")
if self.params[0] < 0:
string += "-%.6f - ln(x) - %.6f*x^%.6f" % (self.params[0], self.params[1], self.params[2])
elif self.params[1] < 0:
string += "%.6f - ln(x) + %.6f*x^%.6f" % (self.params[0], self.params[1], self.params[2])
elif self.params[0] < 0 and self.params[1] < 0:
string += "-%.6f - ln(x) + %.6f*x^%.6f" % (self.params[0], self.params[1], self.params[2])
else:
string += "%.6f - ln(x) - %.6f*x^%.6f" % (self.params[0], self.params[1], self.params[2])
else:
if self.params[0] < 0:
string += "x - e^(-%.6f*(x - 1))" % (abs(self.params[0]))
else:
string += "x - e^(-%.6f*(x - 1))" % (abs(self.params[0]))
self.root.setText(string)
else:
try:
self.data = compile(self.inputParameter.text(), 'string', 'eval')
except SyntaxError:
self.warning("Поле введення параметру не може бути порожнім")
else:
self.params.append(eval(self.data))
self.inputParameter.clear()
self.hint.setText("a{}: ".format(len(self.params)))
if __name__ == '__main__':
qapp = QApplication(sys.argv)
start = SetType()
sys.exit(qapp.exec_())