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的运算条件,输出结论】
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现