-
Notifications
You must be signed in to change notification settings - Fork 2
/
gui.py
250 lines (225 loc) · 10.1 KB
/
gui.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Copyright (C) 2017 Rohit Goswami
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
"""
GUI Portion of the Numerical Swissknife
"""
import sys
import numpy as np
from io import StringIO
from sympy import *
from PyQt5 import uic
from PyQt5.QtWidgets import (QApplication, QWidget, qApp, QDesktopWidget,
QMessageBox, QPushButton, QToolTip, QMainWindow)
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import QCoreApplication
from modules import *
qtCreatorFile = "testUI.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyApp(QMainWindow, Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.btnQuit.clicked.connect(qApp.quit)
self.btnCalcRoot.clicked.connect(self.calcRootMenu)
self.btnCalcLAS.clicked.connect(self.calcLASMenu)
self.btnCalcODE.clicked.connect(self.calcODEMenu)
def calcRootMenu(self):
try:
if self.inpNR.isChecked() == true:
# self.label_22.setEnabled(False)
# self.upperBound.setEnabled(False)
self.calcRootNR()
if self.inpBi.isChecked() == true:
self.calcRootBi()
if self.inpSec.isChecked() == true:
self.calcRootSec()
if self.inpRegF.isChecked() == true:
self.calcRootRegFal()
if self.btnGrpRF.checkedId() == -1:
QMessageBox.warning(self, "User Warning","Choose a method.")
except Exception as e:
raise
finally:
pass
def calcRootNR(self):
f = self.funcInpRoot.text()
if not f:
QMessageBox.warning(self, "User Warning","Enter an equation.")
else:
gX = self.guessX.value()
if self.precisionRF.text():
preci = float(self.precisionRF.text())
x,niter,xval = roots.newtonRaphson(f,gX,preci)
else:
x,niter,xval = roots.newtonRaphson(f,gX)
self.outTextRoot.append("<b> Newton-Raphson Method </b><br> \
For the function " + self.funcInpRoot.text())
self.outTextRoot.append("The root is approximately " + repr(x))
self.outTextRoot.append("After " + str(niter) + " iterations.")
self.outTextRoot.append("At the approximate root, the function is " \
+ repr(xval))
def calcRootBi(self):
f = self.funcInpRoot.text()
if not f:
QMessageBox.warning(self, "User Warning","Enter an equation.")
else:
lBound = self.lowerBound.value()
uBound = self.upperBound.value()
if self.precisionRF.text():
preci = float(self.precisionRF.text())
x,niter,xval = roots.bisection(f,lBound,uBound,tol=preci)
else:
x,niter,xval = roots.bisection(f,lBound,uBound)
self.outTextRoot.append("<b> Bisection Method </b><br> \
For the function " + self.funcInpRoot.text())
self.outTextRoot.append("The root is approximately " + repr(x))
self.outTextRoot.append("After " + str(niter) + " iterations.")
self.outTextRoot.append("At the approximate root, the function is " \
+ repr(xval))
def calcRootRegFal(self):
# Needs user input for max iter.
f = self.funcInpRoot.text()
if not f:
QMessageBox.warning(self, "User Warning","Enter an equation.")
else:
approxOne = self.lowerBound.value()
approxTwo = self.upperBound.value()
if self.precisionRF.text():
preci = float(self.precisionRF.text())
x,niter,xval = roots.regulaFalsi(f,approxOne,approxTwo,tol=preci)
else:
x,niter,xval = roots.regulaFalsi(f,approxOne,approxTwo)
self.outTextRoot.append("<b> Regula Falsi Method </b><br> \
For the function " + self.funcInpRoot.text())
self.outTextRoot.append("The root is approximately " + repr(x))
self.outTextRoot.append("After " + str(niter) + " iterations.")
self.outTextRoot.append("At the approximate root, the function is " \
+ repr(xval))
def calcRootSec(self):
# Needs user input for max iter.
f = self.funcInpRoot.text()
if not f:
QMessageBox.warning(self, "User Warning","Enter an equation.")
else:
approxOne = self.lowerBound.value()
approxTwo = self.upperBound.value()
if self.precisionRF.text():
preci = float(self.precisionRF.text())
x,niter,xval = roots.secant(f,approxOne,approxTwo,tol=preci)
else:
x,niter,xval = roots.secant(f,approxOne,approxTwo)
self.outTextRoot.append("<b> Secant Method </b><br> \
For the function " + self.funcInpRoot.text())
self.outTextRoot.append("The root is approximately " + repr(x))
if niter < 100:
self.outTextRoot.append("After " + str(niter) + " iterations.")
else:
self.outTextRoot.append("After the maximum allowed iterations.")
self.outTextRoot.append("At the approximate root, the function is " \
+ repr(xval))
def calcLASMenu(self):
try:
if self.inpGE.isChecked() == true:
# self.label_22.setEnabled(False)
# self.upperBound.setEnabled(False)
self.calcLASGEP()
if self.inpGJ.isChecked() == true:
self.calcLASGJ()
if self.inpGS.isChecked() == true:
self.calcLASGS()
if self.btnGrpLAS.checkedId() == -1:
QMessageBox.warning(self, "User Warning","Choose a method.")
except Exception as e:
raise
finally:
pass
def calcLASGEP(self):
A = np.matrix(np.loadtxt(StringIO(self.inpTextLA.toPlainText())))
b = np.fromstring(self.inpVecB.text(),sep=' ')
classAns = las.gaussElim(A,b)
self.outTextLA.append("<b> Gauss Elimination Method </b><br>")
self.outTextLA.append("The matrix A is \n" + str(A))
self.outTextLA.append("\nThe vector b is \n" + str(b.reshape((-1, 1))))
self.outTextLA.append("\nThe X vector is \n" + str(classAns.x.reshape((-1,1))))
def calcLASGJ(self):
A = np.matrix(np.loadtxt(StringIO(self.inpTextLA.toPlainText())))
b = np.fromstring(self.inpVecB.text(),sep=' ')
classAns = las.gaussJordan(A,b)
self.outTextLA.append("<b> Gauss Jordan Method </b><br>")
self.outTextLA.append("The matrix A is \n" + str(A))
self.outTextLA.append("\nThe vector b is \n" + str(b.reshape((-1, 1))))
self.outTextLA.append("\nThe X vector is \n" + str(classAns.reshape((-1,1))))
def calcLASGS(self):
A = np.matrix(np.loadtxt(StringIO(self.inpTextLA.toPlainText())))
b = np.fromstring(self.inpVecB.text(),sep=' ')
if self.inpLASMaxItr.text():
maxIter = int(self.inpLASMaxItr.text())
x,itr = las.gaussSeidel(A,b, itrMax = maxIter)
else:
x,itr = las.gaussSeidel(A,b)
x,itr = las.gaussSeidel(A,b)
self.outTextLA.append("<b> Gauss Seidel Method </b><br>")
self.outTextLA.append("The matrix A is \n" + str(A))
self.outTextLA.append("\nThe vector b is \n" + str(b.reshape((-1, 1))))
self.outTextLA.append("\nThe X vector is \n" + str(x.reshape((-1,1))))
self.outTextLA.append("\nObtained in \n" + str(itr) + " iterations.")
def calcODEMenu(self):
try:
if self.inpEu.isChecked() == true:
self.calcODERK1()
if self.inpRK2.isChecked() == true:
self.calcODERK2()
if self.inpRK3.isChecked() == true:
self.calcODERK3()
if self.inpRK4.isChecked() == true:
self.calcODERK4()
if self.inpMP.isChecked() == true:
self.calcODEMP()
if self.btnGrpRF.checkedId() == -1:
QMessageBox.warning(self, "User Warning","Choose a method.")
except Exception as e:
raise
finally:
pass
def calcODERK1(self):
# Needs to be modularized
f = self.funcInpODE.text()
if not f:
QMessageBox.warning(self, "User Warning","Enter an equation.")
else:
startX = self.startX.value()
startY = self.startY.value()
endX = self.endX.value()
if self.stepSize.text():
stepSize = float(self.stepSize.text())
MRKOde.rk1(f,startX,startY,endX,h=stepSize)
else:
self.outTextRoot.append("<b> Euler's Method </b><br> \
For the function " + self.funcInpRoot.text())
MRKOde.rk1(f,startX,startY,endX)
self.outTextRoot.append("The root is approximately " + repr(x))
if niter < 100:
self.outTextRoot.append("After " + str(niter) + " iterations.")
else:
self.outTextRoot.append("After the maximum allowed iterations.")
self.outTextRoot.append("At the approximate root, the function is " \
+ repr(xval))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())