CSDN用户(TDTX),TDTX主页
CSDN用户(philo42),philo42主页
【矩阵相乘】采用线性代数中,计算矩阵相乘的方法实现
1.在点击“矩阵A左乘矩阵B”之前,要先点击其余所有按钮
2.本文只实现了核心的计算原理,在输入检查上读者可以自加内容
def multipmatrix(): import tkinter as tk linearwindow1=tk.Tk() linearwindow1.title("Multipmatrix") linearwindow1.geometry("600x700") def hanga(): global hang1 hang1=eval(e1.get()) def liea(): global lie1 lie1=eval(e2.get()) def hangb(): global hang2 hang2=eval(e3.get()) def lieb(): global lie2 lie2=eval(e4.get()) def smatrixa(): ma1=[] ma1=eval(e5.get()) # print(ma1) # print(ma1[0][1]) def smatrixb(): ma2=[] ma2=eval(e6.get()) # print(ma2) def mupm(): mupma=[] sum=0 ma1=[] ma1=eval(e5.get()) ma2=[] ma2=eval(e6.get()) if lie1!=hang2: tx.insert('insert',"结论:A不能左乘B"+'\n') else: tx.insert('insert','\n'+"结论:A可以左乘B"+'\n') i=0 j=0 k=0 c0=0 c=0 while k<hang1: for j in range(lie2): for i in range(hang2): sum=sum+ma1[k][i]*ma2[i][j] # print(sum) mupma.append(sum) sum=0 k=k+1 # print(mupma) for co in mupma: tx.insert('insert',co) c=c+1 if c%lie2==0: tx.insert('insert','\n') if c%lie2!=0: tx.insert('insert','\t') tx.insert('insert','\n') e1 = tk.Entry(linearwindow1,font=('Arial', 14),width=7) e1.grid(row=0, column = 1) bt1=tk.Button(linearwindow1,text='确认矩阵A行数',width=12,height=1,font=('Arial', 10),command=hanga) bt1.grid(row=1, column = 1) e2 = tk.Entry(linearwindow1,font=('Arial', 14),width=7) e2.grid(row=2, column = 1) bt2=tk.Button(linearwindow1,text='确认矩阵A列数',width=12,height=1,font=('Arial', 10),command=liea) bt2.grid(row=3, column = 1) e3 = tk.Entry(linearwindow1,font=('Arial', 14),width=7) e3.grid(row=5, column = 1) bt3=tk.Button(linearwindow1,text='确认矩阵B行数',width=12,height=1,font=('Arial', 10),command=hangb) bt3.grid(row=6, column = 1) e4 = tk.Entry(linearwindow1,font=('Arial', 14),width=7) e4.grid(row=7, column = 1) bt4=tk.Button(linearwindow1,text='确认矩阵B列数',width=12,height=1,font=('Arial', 10),command=lieb) bt4.grid(row=8, column = 1) lb1=tk.Label(linearwindow1, text='在[]中以[]分隔行,以逗号分隔元素:\nexamp:[[1,2],[3,4],[5,6],[7,8]]', bg='orange', font=('Arial', 12), width=30, height=2) lb1.grid(row=0, column = 6) e5 = tk.Entry(linearwindow1,font=('Arial', 14),width=40) e5.grid(row=1, column = 6) bt5=tk.Button(linearwindow1,text='确认矩阵A',width=12,height=1,font=('Arial', 10),command=smatrixa) bt5.grid(row=2, column = 6) lb2=tk.Label(linearwindow1,text='在[]中以[]分隔行,以逗号分隔元素:\nexamp:[[1,2,3,4],[5,6,7,8]]', bg='orange', font=('Arial', 12), width=30, height=2) lb2.grid(row=3, column = 6) e6 = tk.Entry(linearwindow1,font=('Arial', 14),width=40) e6.grid(row=4, column = 6) bt6=tk.Button(linearwindow1,text='确认矩阵B',width=12,height=1,font=('Arial', 10),command=smatrixb) bt6.grid(row=5, column = 6) lb3=tk.Label(linearwindow1,text='------------------------------------------------------', bg='orange', font=('Arial', 12), width=30, height=0) lb3.grid(row=6, column = 6) bt7=tk.Button(linearwindow1,text='矩阵A左乘矩阵B',width=12,height=1,font=('Arial', 10),command=mupm) bt7.grid(row=7, column = 6) lb4=tk.Label(linearwindow1,text='【AB=C】C=:', bg='orange', font=('Arial', 12), width=30, height=1) lb4.grid(row=8, column = 6) tx=tk.Text(linearwindow1,width=37,height=25) tx.grid(row=9, column = 6) linearwindow1.mainloop()
【若不符合A左乘B的运算条件,则会输出结论提示】