Repository navigation
Expand file tree
/
Copy pathgui-op.py
More file actions
303 lines (301 loc) · 18 KB
/
Copy pathgui-op.py
File metadata and controls
303 lines (301 loc) · 18 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# coding:utf-8
# 导入matplotlib模块并使用Qt5Agg
from gui import Ui_Dialog #导入新建好的框架
import matplotlib
matplotlib.use('Qt5Agg')
# 使用 matplotlib中的FigureCanvas (在使用 Qt5 Backends中 FigureCanvas继承自QtWidgets.QWidget)
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import *
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import sys
import numpy as np
from PyQt5.QtWidgets import QWidget, QRadioButton, QApplication, QPushButton, QMessageBox, QButtonGroup
from numpy import *
import time
from scipy import integrate
import sip
class MyFigure(FigureCanvas):# 通过继承FigureCanvas类,使得该类既是一个PyQt5的Qwidget,又是一个matplotlib的FigureCanvas,这是连接pyqt5与matplotlib的关键
def __init__(self, width=5, height=4, dpi=100):
# 第一部:创建一个Figure
self.fig = Figure(figsize=(width, height), dpi=dpi)
# 第二步:在父类中激活Figure窗口
super(MyFigure, self).__init__(self.fig)
class MainDialogImgBW(QDialog,Ui_Dialog):
num = 3
l_max = 1
l_min =-1
str_hs = 'x'
f_1 =False
f_2 =False
f=0
def __init__(self):
super(MainDialogImgBW,self).__init__()
self.setupUi(self)
self.setMinimumSize(0,0)
self.gridlayout = QGridLayout(self.groupBox_5) # 继承容器groupBox
def operation(self):
self.num = self.bg.checkedId()
self.l_max = eval(self.lineEdit.text())
self.l_min = eval(self.lineEdit_2.text())
self.str_hs = self.lineEdit_3.text()
self.f_1 = self.checkBox.isChecked()
self.f_2 = self.checkBox_2.isChecked()
# 画图
self.n_plot()
if self.bd():
self.textBrowser.setText("")
if self.f_1:
self.grass_lagrange()
if self.f_2:
self.scipy_jf()
# 高斯-勒让德求积
def grass_lagrange(self):
def l_change(l_min,l_max, t): # 积分区域变换
return (t + 1) * (l_max - l_min) / 2 + l_min
n = [ # n=0
{'x1': 0, 'A1': 2},
# n=1
{'x1': -0.5773502691896250, 'x2': 0.5773502691896250, 'A1': 1, 'A2': 1},
# n=2
{'x1': -0.7745966692414830, 'x2': 0, 'x3': 0.7745966692414830, 'A1': 0.5555555555555550,
'A2': 0.8888888888888880, 'A3': 0.5555555555555550},
# n=3
{'x1': -0.8611363115940520, 'x2': -0.3399810435848560, 'x3': 0.3399810435848560, 'x4': 0.8611363115940520,
'A1': 0.3478548451374530, 'A2': 0.6521451548625460, 'A3': 0.6521451548625460, 'A4': 0.3478548451374530},
# n=4
{'x1': -0.9061798459386640, 'x2': -0.5384693101056830, 'x3': 0, 'x4': 0.5384693101056830,
'x5': 0.9061798459386640,
'A1': 0.2369268850561890, 'A2': 0.4786286704993660, 'A3': 0.5688888888888880, 'A4': 0.4786286704993660,
'A5': 0.2369268850561890},
# n=5
{'x1': -0.9324695142031520, 'x2': -0.6612093864662640, 'x3': -0.2386191860831960, 'x4': 0.2386191860831960,
'x5': 0.6612093864662640,
'x6': 0.9324695142031520,
'A1': 0.1713244923791700, 'A2': 0.3607615730481380, 'A3': 0.4679139345726910, 'A4': 0.4679139345726910,
'A5': 0.3607615730481380,
'A6': 0.1713244923791700},
# n=6
{'x1': -0.9491079123427580, 'x2': -0.7415311855993940, 'x3': -0.4058451513773970, 'x4': 0.0000000000000000,
'x5': 0.4058451513773970,
'x6': 0.7415311855993940, 'x7': 0.9491079123427580,
'A1': 0.1294849661688690, 'A2': 0.2797053914892760, 'A3': 0.3818300505051180, 'A4': 0.4179591836734690,
'A5': 0.3818300505051180,
'A6': 0.2797053914892760, 'A7': 0.1294849661688690},
# n=7
{'x1': -0.9602898564975360, 'x2': -0.7966664774136260, 'x3': -0.5255324099163290, 'x4': -0.1834346424956490,
'x5': 0.1834346424956490,
'x6': 0.5255324099163290, 'x7': 0.7966664774136260, 'x8': 0.9602898564975360,
'A1': 0.1012285362903760, 'A2': 0.2223810344533740, 'A3': 0.3137066458778870, 'A4': 0.3626837833783620,
'A5': 0.3626837833783620,
'A6': 0.3137066458778870, 'A7': 0.2223810344533740, 'A8': 0.1012285362903760},
# n=8
{'x1': -0.9681602395076260, 'x2': -0.8360311073266350, 'x3': -0.6133714327005900, 'x4': -0.3242534234038080,
'x5': 0.0000000000000000,
'x6': 0.3242534234038080, 'x7': 0.6133714327005900, 'x8': 0.8360311073266350, 'x9': 0.9681602395076260,
'A1': 0.0812743883615744, 'A2': 0.1806481606948570, 'A3': 0.2606106964029350, 'A4': 0.3123470770400020,
'A5': 0.3302393550012590,
'A6': 0.3123470770400020, 'A7': 0.2606106964029350, 'A8': 0.1806481606948570, 'A9': 0.0812743883615744},
# n=9
{'x1': -0.9739065285171710, 'x2': -0.8650633666889840, 'x3': -0.6794095682990240, 'x4': -0.4333953941292470,
'x5': -0.1488743389816310,
'x6': 0.1488743389816310, 'x7': 0.4333953941292470, 'x8': 0.6794095682990240, 'x9': 0.8650633666889840,
'x10': 0.9739065285171710,
'A1': 0.0666713443086881, 'A2': 0.1494513491505800, 'A3': 0.2190863625159820, 'A4': 0.2692667193099960,
'A5': 0.2955242247147520,
'A6': 0.2955242247147520, 'A7': 0.2692667193099960, 'A8': 0.2190863625159820, 'A9': 0.1494513491505800,
'A10': 0.0666713443086881},
# n=10
{'x1': -0.9782286581460570, 'x2': -0.8870625997680950, 'x3': -0.7301520055740490, 'x4': -0.5190961292068110,
'x5': -0.2695431559523440,
'x6': 0.0000000000000000, 'x7': 0.2695431559523440, 'x8': 0.5190961292068110, 'x9': 0.7301520055740490,
'x10': 0.8870625997680950,
'x11': 0.9782286581460570,
'A1': 0.0556685671161736, 'A2': 0.1255803694649040, 'A3': 0.1862902109277340, 'A4': 0.2331937645919900,
'A5': 0.2628045445102460,
'A6': 0.2729250867779000, 'A7': 0.2628045445102460, 'A8': 0.2331937645919900, 'A9': 0.1862902109277340,
'A10': 0.1255803694649040,
'A11': 0.0556685671161736},
# n=11
{'x1': -0.9815606342467190, 'x2': -0.9041172563704740, 'x3': -0.7699026741943040, 'x4': -0.5873179542866170,
'x5': -0.3678314989981800,
'x6': -0.1252334085114680, 'x7': 0.1252334085114680, 'x8': 0.3678314989981800, 'x9': 0.5873179542866170,
'x10': 0.7699026741943040,
'x11': 0.9041172563704740, 'x12': 0.9815606342467190,
'A1': 0.0471753363865118, 'A2': 0.1069393259953180, 'A3': 0.1600783285433460, 'A4': 0.2031674267230650,
'A5': 0.2334925365383540,
'A6': 0.2491470458134020, 'A7': 0.2491470458134020, 'A8': 0.2334925365383540, 'A9': 0.2031674267230650,
'A10': 0.1600783285433460,
'A11': 0.1069393259953180, 'A12': 0.0471753363865118},
# n=12
{'x1': -0.9841830547185880, 'x2': -0.9175983992229770, 'x3': -0.8015780907333090, 'x4': -0.6423493394403400,
'x5': -0.4484927510364460,
'x6': -0.2304583159551340, 'x7': 0.0000000000000000, 'x8': 0.2304583159551340, 'x9': 0.4484927510364460,
'x10': 0.6423493394403400,
'x11': 0.8015780907333090, 'x12': 0.9175983992229770, 'x13': 0.9841830547185880,
'A1': 0.0404840047653158, 'A2': 0.0921214998377284, 'A3': 0.1388735102197870, 'A4': 0.1781459807619450,
'A5': 0.2078160475368880,
'A6': 0.2262831802628970, 'A7': 0.2325515532308730, 'A8': 0.2262831802628970, 'A9': 0.2078160475368880,
'A10': 0.1781459807619450,
'A11': 0.1388735102197870, 'A12': 0.0921214998377284, 'A13': 0.0404840047653158},
# n=13
{'x1': -0.9862838086968120, 'x2': -0.9284348836635730, 'x3': -0.8272013150697650, 'x4': -0.6872929048116850,
'x5': -0.5152486363581540,
'x6': -0.3191123689278890, 'x7': -0.1080549487073430, 'x8': 0.1080549487073430, 'x9': 0.3191123689278890,
'x10': 0.5152486363581540,
'x11': 0.6872929048116850, 'x12': 0.8272013150697650, 'x13': 0.9284348836635730, 'x14': 0.9862838086968120,
'A1': 0.0351194603317518, 'A2': 0.0801580871597602, 'A3': 0.1215185706879030, 'A4': 0.1572031671581930,
'A5': 0.1855383974779370,
'A6': 0.2051984637212950, 'A7': 0.2152638534631570, 'A8': 0.2152638534631570, 'A9': 0.2051984637212950,
'A10': 0.1855383974779370,
'A11': 0.1572031671581930, 'A12': 0.1215185706879030, 'A13': 0.0801580871597602,
'A14': 0.0351194603317518},
# n=14
{'x1': -0.9879925180204850, 'x2': -0.9372733924007060, 'x3': -0.8482065834104270, 'x4': -0.7244177313601700,
'x5': -0.5709721726085380,
'x6': -0.3941513470775630, 'x7': -0.2011940939974340, 'x8': 0.0000000000000000, 'x9': 0.2011940939974340,
'x10': 0.3941513470775630,
'x11': 0.5709721726085380, 'x12': 0.7244177313601700, 'x13': 0.8482065834104270, 'x14': 0.9372733924007060,
'x15': 0.9879925180204850,
'A1': 0.0307532419961172, 'A2': 0.0703660474881081, 'A3': 0.1071592204671710, 'A4': 0.1395706779261540,
'A5': 0.1662692058169930,
'A6': 0.1861610000155620, 'A7': 0.1984314853271110, 'A8': 0.2025782419255610, 'A9': 0.1984314853271110,
'A10': 0.1861610000155620,
'A11': 0.1662692058169930, 'A12': 0.1395706779261540, 'A13': 0.1071592204671710, 'A14': 0.0703660474881081,
'A15': 0.0307532419961172},
# n=15
{'x1': -0.9894009349916490, 'x2': -0.9445750230732320, 'x3': -0.8656312023878310, 'x4': -0.7554044083550030,
'x5': -0.6178762444026430,
'x6': -0.4580167776572270, 'x7': -0.2816035507792580, 'x8': -0.0950125098376374, 'x9': 0.0950125098376374,
'x10': 0.2816035507792580,
'x11': 0.4580167776572270, 'x12': 0.6178762444026430, 'x13': 0.7554044083550030, 'x14': 0.8656312023878310,
'x15': 0.9445750230732320,
'x16': 0.9894009349916490,
'A1': 0.0271524594117540, 'A2': 0.0622535239386478, 'A3': 0.0951585116824927, 'A4': 0.1246289712555330,
'A5': 0.1495959888165760,
'A6': 0.1691565193950020, 'A7': 0.1826034150449230, 'A8': 0.1894506104550680, 'A9': 0.1894506104550680,
'A10': 0.1826034150449230,
'A11': 0.1691565193950020, 'A12': 0.1495959888165760, 'A13': 0.1246289712555330, 'A14': 0.0951585116824927,
'A15': 0.0622535239386478,
'A16': 0.0271524594117540},
# n=16
{'x1': -0.9905754753144170, 'x2': -0.9506755217687670, 'x3': -0.8802391537269850, 'x4': -0.7815140038968010,
'x5': -0.6576711592166900,
'x6': -0.5126905370864760, 'x7': -0.3512317634538760, 'x8': -0.1784841814958470, 'x9': 0.0000000000000000,
'x10': 0.1784841814958470,
'x11': 0.3512317634538760, 'x12': 0.5126905370864760, 'x13': 0.6576711592166900, 'x14': 0.7815140038968010,
'x15': 0.8802391537269850,
'x16': 0.9506755217687670, 'x17': 0.9905754753144170,
'A1': 0.0241483028685479, 'A2': 0.0554595293739872, 'A3': 0.0850361483171791, 'A4': 0.1118838471934030,
'A5': 0.1351363684685250,
'A6': 0.1540457610768100, 'A7': 0.1680041021564500, 'A8': 0.1765627053669920, 'A9': 0.1794464703562060,
'A10': 0.1765627053669920,
'A11': 0.1680041021564500, 'A12': 0.1540457610768100, 'A13': 0.1351363684685250, 'A14': 0.1118838471934030,
'A15': 0.0850361483171791,
'A16': 0.0554595293739872, 'A17': 0.0241483028685479},
# n=17
{'x1': -0.9915651684209300, 'x2': -0.9558239495713970, 'x3': -0.8926024664975550, 'x4': -0.8037049589725230,
'x5': -0.6916870430603530,
'x6': -0.5597708310739470, 'x7': -0.4117511614628420, 'x8': -0.2518862256915050, 'x9': -0.0847750130417353,
'x10': 0.0847750130417353,
'x11': 0.2518862256915050, 'x12': 0.4117511614628420, 'x13': 0.5597708310739470, 'x14': 0.6916870430603530,
'x15': 0.8037049589725230,
'x16': 0.8926024664975550, 'x17': 0.9558239495713970, 'x18': 0.9915651684209300,
'A1': 0.0216160135264833, 'A2': 0.0497145488949698, 'A3': 0.0764257302548890, 'A4': 0.1009420441062870,
'A5': 0.1225552067114780,
'A6': 0.1406429146706500, 'A7': 0.1546846751262650, 'A8': 0.1642764837458320, 'A9': 0.1691423829631430,
'A10': 0.1691423829631430,
'A11': 0.1642764837458320, 'A12': 0.1546846751262650, 'A13': 0.1406429146706500, 'A14': 0.1225552067114780,
'A15': 0.1009420441062870,
'A16': 0.0764257302548890, 'A17': 0.0497145488949698, 'A18': 0.0216160135264833},
# n=18
{'x1': -0.9924068438435840, 'x2': -0.9602081521348300, 'x3': -0.9031559036148170, 'x4': -0.8227146565371420,
'x5': -0.7209661773352290,
'x6': -0.6005453046616810, 'x7': -0.4645707413759600, 'x8': -0.3165640999636290, 'x9': -0.1603586456402250,
'x10': 0.0000000000000000,
'x11': 0.1603586456402250, 'x12': 0.3165640999636290, 'x13': 0.4645707413759600, 'x14': 0.6005453046616810,
'x15': 0.7209661773352290,
'x16': 0.8227146565371420, 'x17': 0.9031559036148170, 'x18': 0.9602081521348300, 'x19': 0.9924068438435840,
'A1': 0.0194617882297264, 'A2': 0.0448142267656996, 'A3': 0.0690445427376412, 'A4': 0.0914900216224500,
'A5': 0.1115666455473330,
'A6': 0.1287539625393360, 'A7': 0.1426067021736060, 'A8': 0.1527660420658590, 'A9': 0.1589688433939540,
'A10': 0.1610544498487830,
'A11': 0.1589688433939540, 'A12': 0.1527660420658590, 'A13': 0.1426067021736060, 'A14': 0.1287539625393360,
'A15': 0.1115666455473330,
'A16': 0.0914900216224500, 'A17': 0.0690445427376412, 'A18': 0.0448142267656996,
'A19': 0.0194617882297264},
# n=19
{'x1': -0.9931285991850940, 'x2': -0.9639719272779130, 'x3': -0.9122344282513260, 'x4': -0.8391169718222180,
'x5': -0.7463319064601500,
'x6': -0.6360536807265150, 'x7': -0.5108670019508270, 'x8': -0.3737060887154190, 'x9': -0.2277858511416450,
'x10': -0.0765265211334973,
'x11': 0.0765265211334973, 'x12': 0.2277858511416450, 'x13': 0.3737060887154190, 'x14': 0.5108670019508270,
'x15': 0.6360536807265150,
'x16': 0.7463319064601500, 'x17': 0.8391169718222180, 'x18': 0.9122344282513260, 'x19': 0.9639719272779130,
'x20': 0.9931285991850940,
'A1': 0.0176140071391521, 'A2': 0.0406014298003869, 'A3': 0.0626720483341090, 'A4': 0.0832767415767047,
'A5': 0.1019301198172400,
'A6': 0.1181945319615180, 'A7': 0.1316886384491760, 'A8': 0.1420961093183820, 'A9': 0.1491729864726030,
'A10': 0.1527533871307250,
'A11': 0.1527533871307250, 'A12': 0.1491729864726030, 'A13': 0.1420961093183820, 'A14': 0.1316886384491760,
'A15': 0.1181945319615180,
'A16': 0.1019301198172400, 'A17': 0.0832767415767047, 'A18': 0.0626720483341090, 'A19': 0.0406014298003869,
'A20': 0.0176140071391521}
] # 积分
temp = 0 # 积分结果
start = time.perf_counter() # 获取积分开始时间
for i in range(self.num + 1): # 循环遍历
t = n[self.num].get('x' + str(i + 1))
x = l_change(self.l_min, self.l_max, t) # 积分区域变换
temp = n[self.num].get('A' + str(i + 1)) * eval(self.str_hs) * (self.l_max - self.l_min) / 2 + temp
end = time.perf_counter()
print('-' * 50)
print("高斯-勒让德求积公式运算结果为:")
print("积分结果为:", temp)
print("计算时间为:", end - start) # 输出运算时间
self.textBrowser.append("高斯-勒让德求积公式运算结果为:")
self.textBrowser.append("积分结果为:"+str(temp))
self.textBrowser.append("计算时间为:"+str(end - start))
def scipy_jf(self):
def f(x):
return eval(self.str_hs)
start = time.perf_counter()
v, err = integrate.quad(f, self.l_min, self.l_max)
end = time.perf_counter()
print('-'*50)
print("scipy库运算结果为:")
print("积分结果为:", v)
print("误差为:", err)
print("运算时间为", end - start)
self.textBrowser.append("scipy库运算结果为:")
self.textBrowser.append("积分结果为:"+str(v))
self.textBrowser.append("误差为:" + str(err))
self.textBrowser.append("计算时间为:"+str(end - start))
def bd(self):
if self.f_1 ==0 and self.f_2 ==0 :
QMessageBox.information(self, "Erro","您没有选择计算方法")
return 0
return 1
def n_plot(self):
if self.f==1:
for i in range(self.gridlayout.count()):
self.gridlayout.itemAt(i).widget().deleteLater()
#定义MyFigure类的一个实例
self.F = MyFigure(width=5, height=7, dpi=100)
#在GUI的groupBox中创建一个布局,用于添加MyFigure类的实例(即图形)后其他部件。
# 创建一个子图,用于绘制图形用,111代表子图的编号
self.axes = self.F.fig.add_subplot(111)
self.axes.clear() # 每次绘制一个函数时清空绘图
x = np.linspace(self.l_min, self.l_max, 10000)
y_p = eval(self.str_hs)
self.axes.plot(x,y_p,)
self.axes.set_xlabel("$x$", fontsize=18)
self.axes.set_ylabel("$f(x)$", fontsize=18)
self.gridlayout.addWidget(self.F,0,1)
self.f=1
if __name__ == "__main__":
app = QApplication(sys.argv)
n = MainDialogImgBW()
n.show()
sys.exit(app.exec_())