Python教程

Python——矩阵相乘(采用矩阵相乘数学计算方法实现)(tkinter实现)【2021-08-03】

本文主要是介绍Python——矩阵相乘(采用矩阵相乘数学计算方法实现)(tkinter实现)【2021-08-03】,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Python——矩阵相乘(采用矩阵相乘数学计算方法实现)(tkinter实现)

  • 1.multipmatrix.py
  • 2.结果示例

CSDN用户(TDTX),TDTX主页
CSDN用户(philo42),philo42主页
【矩阵相乘】采用线性代数中,计算矩阵相乘的方法实现
1.在点击“矩阵A左乘矩阵B”之前,要先点击其余所有按钮
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的运算条件,则会输出结论提示】
请添加图片描述

这篇关于Python——矩阵相乘(采用矩阵相乘数学计算方法实现)(tkinter实现)【2021-08-03】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!