Python——矩陣相乘(采用矩陣相乘數學計算方法實現)(tkinter實現)


本功能作者:
1.CSDN用戶(TDTX),TDTX主頁
   博客園用戶(TDTX),TDTX主頁

【矩陣相乘】采用線性代數中,計算矩陣相乘的方法實現
1.在點擊“矩陣相乘”之前,要先點擊其余所有按鈕
2.本文只實現了核心的計算原理,讀者可自加輸入檢查的代碼

1.multipmatrix.py

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()

2.結果示例

請添加圖片描述
【不符合矩陣A左乘矩陣B的運算條件,輸出結論】
請添加圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM