|
发表于 2017-10-23 17:02:35
|
显示全部楼层
本帖最后由 qwc3000 于 2017-10-23 17:04 编辑
from he import Ui_Form
from PyQt5 import QtWidgets , QtCore
import re
import sys
import copy
class mywindow(QtWidgets.QMainWindow):
def __init__(self,parent=None):
QtWidgets.QWidget.__init__(self,parent)
self.ui=Ui_Form()
self.ui.setupUi(self)
# 加法
def hello(self):
# self.ui.textEdit_A.setText("hello")
# 获取A B的文本框的相关信息列表
self.tmpA = self.charsplit(self.ui.textEdit_A.toPlainText())
self.tmpB = self.charsplit(self.ui.textEdit_B.toPlainText())
# 先输入要相加的两个矩阵,大小必须一致为mxn,一般矩阵加法才有定义;
# 所以需要首先判断A B 矩阵的行列数是否相同
# 相同 继续 不相同跳出
self.L_A_mn = self.tmpA.pop()
self.L_B_mn = self.tmpB.pop()
if self.L_B_mn[0] != self.L_A_mn[0] or self.L_A_mn[1] != self.L_B_mn[1]:
return
print("L_A:", self.tmpA)
print("L_B:", self.tmpB)
# 定义几个中间转换用变量
L_tmp_a=[]
L_tmp_b=[]
int_tmp_d = 0
cha_tmp_c = ""
# a = [1, 2, 3]
# b = [4, 5, 6]
# c = [a + b for i in range(min(len(a), len(b)))]
# a=[self.tmpA + self.tmpB for i in range(min(len(self.tmpA), len(self.tmpB)))]
# 矩阵加法计算
for i in range(self.L_A_mn[0]):
for j in range(self.L_A_mn[1]):
int_tmp_d = self.tmpA[j]+self.tmpB[j]
print(int_tmp_d)
L_tmp_a.append(str(int_tmp_d))
L_tmp_b.append(L_tmp_a)
L_tmp_a=[]
# print(L_tmp_b)
# 矩阵转换
for i in range(self.L_A_mn[0]):
cha_tmp_c += ",".join(L_tmp_b)
cha_tmp_c += "\n"
self.ui.textEdit_C.setText(cha_tmp_c)
# 减法
def sub(self):
# self.ui.textEdit_A.setText("hello")
# 获取A B的文本框的相关信息列表
self.tmpA = self.charsplit(self.ui.textEdit_A.toPlainText())
self.tmpB = self.charsplit(self.ui.textEdit_B.toPlainText())
# 先输入要相加的两个矩阵,大小必须一致为mxn,一般矩阵加法才有定义;
# 所以需要首先判断A B 矩阵的行列数是否相同
# 相同 继续 不相同跳出
self.L_A_mn = self.tmpA.pop()
self.L_B_mn = self.tmpB.pop()
if self.L_B_mn[0] != self.L_A_mn[0] or self.L_A_mn[1] != self.L_B_mn[1]:
return
print("L_A:", self.tmpA)
print("L_B:", self.tmpB)
# 定义几个中间转换用变量
L_tmp_a = []
L_tmp_b = []
int_tmp_d = 0
cha_tmp_c = ""
# 矩阵减法计算
for i in range(self.L_A_mn[0]):
for j in range(self.L_A_mn[1]):
int_tmp_d = self.tmpA[j] - self.tmpB[j]
print(int_tmp_d)
L_tmp_a.append(str(int_tmp_d))
L_tmp_b.append(L_tmp_a)
L_tmp_a = []
# print(L_tmp_b)
# 矩阵转换
for i in range(self.L_A_mn[0]):
cha_tmp_c += ",".join(L_tmp_b)
cha_tmp_c += "\n"
self.ui.textEdit_C.setText(cha_tmp_c)
def mult(self):
# 获取A B 矩阵信息
self.tmpA = self.charsplit(self.ui.textEdit_A.toPlainText())
self.tmpB = self.charsplit(self.ui.textEdit_B.toPlainText())
# 乘法运算
self.Str_AB = self.Matrix_operator(self.tmpA,self.tmpB)
# 赋值显示
self.ui.textEdit_C.setText(self.Str_AB)
# 除法
def div(self):
self.tmpA = self.charsplit(self.ui.textEdit_A.toPlainText())
self.tmpB = self.charsplit(self.ui.textEdit_B.toPlainText())
result=self.Matrix_division(self.tmpA,self.tmpB)
self.ui.textEdit_C.setText(result)
# 矩阵乘法
def Matrix_operator(self, L_A, L_B):
# 处理矩阵信息
# 使用pop 弹出 L_A的最后一个元素 将弹出的元素赋值给L_A_mn
# 此L_A_mn元素为A矩阵的mn行列数,同时得到L_A的矩阵列表内容
self.L_A_mn = L_A.pop()
self.L_B_mn = L_B.pop()
# print("abc")
# 判断AB矩阵是否满足 A为m*p的矩阵,B为p*n的矩阵
if self.L_A_mn[1] != self.L_B_mn[0]:
return None
# 满足继续
# 取A的第i行
# 定义一个m*n的列表
# print("self.L_B_mn[1]) ", self.L_B_mn[1])
L_AB=[[] for i in range(self.L_A_mn[0])]
print("L_AB ", L_AB)
tmp=0
# 取A的第i行
for i in range(self.L_A_mn[0]):
# 取B的j列
for j in range(self.L_B_mn[1]):
# 分别取a的ik元素 和 b的kj元素 做乘法 再求和
for k in range(self.L_B_mn[0]):
tmp += L_A[k]*L_B[k][j]
print(tmp)
# 将tmp赋值给AB的ij元素
L_AB.append(str(tmp))
# 定义一个字符串 返回使用
Str_AB=""
# 将ab列表转为字符串
for i in range(len(L_AB)):
Str_AB += ",".join(L_AB)
Str_AB += "\n"
# 返回
return Str_AB
# 文本拆分
def charsplit(self, text):
tmp_mn = []
tmp=[]
m = 1
n = 0
# 将文本输入框的内容 按照行分成m个元素的列表
tmp_m = text.split("\n")
# 获取矩阵有多少行 m行
m = len(tmp_m)
# 将m交给mn列表,后面使用
tmp_mn.append(m)
# 创建tmp_Ultimate(终极)多维数组
tmp_Ultimate = [[] for i in range(m)]
# 将mn列表中的每一个元素分解成n个元素的列表
for i in range(0, m):
# 获取m列表的
tmp_n = tmp_m.split(",")
n = len(tmp_n)
# 将n列表中的所有字符串转变为数字
for j in range(0, n):
tmp.append(int(tmp_n[j]))
# 清空
tmp_n.clear()
# 赋值给n列表
# 切片赋值
# tmp_n=tmp[:]
# 深度复制
tmp_n = copy.deepcopy(tmp)
# print("tmp_n", tmp_n)
# 清空
tmp.clear()
# print("tmp_n b", tmp_n)
# 赋值给m列表的i组
tmp_Ultimate += tmp_n
# 清空
tmp_n.clear()
# print("biaozhi ", tmp,tmp1,tmp2)
# 将m交给mn列表,后面使用
tmp_mn.append(n)
tmp_Ultimate.append(tmp_mn)
# 返回终极列表,此列表包括矩阵的行列元素以及行列数
return tmp_Ultimate
# 逆矩阵判断
def is_Inverse_Matrix(self,L_A):
a=1
# 如果不是n阶矩阵,则无逆矩阵
if len(L_A) == len(L_A[0]):
determinant = self.Triangle_determinant(L_A)
for i in range(len(L_A)):
a = a*determinant
if a!=0:
return True
return False
# 形成E阵
# Le参数为矩阵的阶数列表
def E_matrix(self,L_e):
# 1、增加E阵
a = 0
L_E = [[] for i in range(L_e[0])]
for i in range(L_e[0]):
for j in range(L_e[1]):
if j == a:
L_E.append(1)
else:
L_E.append(0)
a = a + 1
print(L_E)
return L_E
# 三角行列式计算
def Triangle_determinant(self,L_A):
# 将行列式的某一行(列)乘以倍数,加到另一行,其行列式等于原行列式
# 三角行列式等主线上的成绩
# L_A = self.Matrix_decomposition(L_A, 0)
determinant = copy.deepcopy(L_A)
determinant_tmp = copy.deepcopy(L_A)
a = len(determinant)
for k in range(len(L_A)):
for i in range(1 + k, a):
b = determinant[k] / determinant[k][k]
for j in range(k, a):
c = determinant[j] - determinant[k][j] * b
determinant_tmp[j] = c
determinant = determinant_tmp
return determinant
# 逆矩阵计算
def Inverse_matrix(self,L_A,L_E):
# 无逆矩阵返回
print("LA go",L_A)
if self.is_Inverse_Matrix(L_A)==False:
print("无逆矩阵")
return
L_tmp_a=len(L_A)
a=1
# 项目数
for k in range(L_tmp_a):
# 第一步将矩阵(0,0)项变为1,同时其E矩阵也除以相应的倍数
for i in range(len(L_A[0])):
if i==0:
a=L_A[k][k]
L_A[k]=L_A[k]/a
L_E[k] = L_E[k] / a
# 第二步将(0,0)以外的第0列的其他项变为0
for i in range(0,len(L_A)):
if i!=k:
# 计算倍数
multiple=L_A[k]/L_A[k][k]
print("hang",i,"lie",k,"beishu",L_A[k],L_A[k][k],multiple)
# mn号元素=mn号元素减去 (m-1)n元素乘以倍数
# 让mn号元素变为1
for j in range(len(L_A[1])):
L_A[j] = L_A[j]-L_A[k][j]*multiple
L_E[j] = L_E[j]-L_E[k][j]*multiple
print("E-matrix:",L_E)
return L_E
# 除法
def Matrix_division(self, L_A, L_B):
# 需要将除数B转变逆矩阵
# 首先需要判断B是否有逆变矩阵,其充要条件是 B的行列式不为
L_Result=""
if self.is_Inverse_Matrix(L_B):
return
else:
# 1、增加E阵
L_A.pop()
L_E=self.E_matrix(L_B.pop())
# 2、计算逆矩阵
L_N_B=self.Inverse_matrix(L_B,L_E)
# 3、计算LA和LB逆矩阵的乘法
L_mn=[]
L_mn.append(len(L_A))
L_mn.append(len(L_A[0]))
L_A.append(L_mn)
L_mn.append(len(L_N_B))
L_mn.append(len(L_N_B[0]))
L_N_B.append(L_mn)
L_Result=self.Matrix_operator(L_A , L_N_B)
return L_Result
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = mywindow()
myapp.show()
sys.exit(app.exec_())
##++++++++++++++++++++++++++++++++++##
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'he.ui'
#
# Created by: PyQt5 UI code generator 5.8.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 243)
self.Button_add = QtWidgets.QPushButton(Form)
self.Button_add.setGeometry(QtCore.QRect(310, 30, 51, 41))
self.Button_add.setObjectName("Button_add")
self.Button_sub = QtWidgets.QPushButton(Form)
self.Button_sub.setGeometry(QtCore.QRect(310, 80, 51, 41))
self.Button_sub.setObjectName("Button_sub")
self.Button_multi = QtWidgets.QPushButton(Form)
self.Button_multi.setGeometry(QtCore.QRect(310, 130, 51, 41))
self.Button_multi.setObjectName("Button_multi")
self.Button_divi = QtWidgets.QPushButton(Form)
self.Button_divi.setGeometry(QtCore.QRect(310, 180, 51, 41))
self.Button_divi.setObjectName("Button_divi")
self.textEdit_A = QtWidgets.QTextEdit(Form)
self.textEdit_A.setGeometry(QtCore.QRect(20, 40, 104, 71))
self.textEdit_A.setObjectName("textEdit_A")
self.textEdit_B = QtWidgets.QTextEdit(Form)
self.textEdit_B.setGeometry(QtCore.QRect(170, 40, 104, 71))
self.textEdit_B.setObjectName("textEdit_B")
self.textEdit_C = QtWidgets.QTextEdit(Form)
self.textEdit_C.setGeometry(QtCore.QRect(20, 140, 104, 71))
self.textEdit_C.setObjectName("textEdit_C")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(20, 6, 61, 21))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(170, 0, 61, 31))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(30, 110, 61, 31))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.retranslateUi(Form)
# 加
self.Button_add.clicked.connect(Form.hello)
# 减
self.Button_sub.clicked.connect(Form.sub)
# 乘法
self.Button_multi.clicked.connect(Form.mult)
# 除法
self.Button_divi.clicked.connect(Form.div)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.Button_add.setText(_translate("Form", "+"))
self.Button_sub.setText(_translate("Form", "-"))
self.Button_multi.setText(_translate("Form", "*"))
self.Button_divi.setText(_translate("Form", "/"))
self.label.setText(_translate("Form", "A1"))
self.label_2.setText(_translate("Form", "B2"))
self.label_3.setText(_translate("Form", "C3"))
“”“”“”“”“”“”“”“”“”“”“”“”“
参数命名比较混乱,嘿嘿额嘿嘿 2阶除法不好使 |
|