前言
最近上《计算机控制系统》课,涉及许多矩阵运算(乘法居多),觉得手算不过来,按计算器太慢,于是写一个Python小程序做做。
二、代码
import numpy as np
from numpy import shape
m = int(input("Enter m: "))
n = int(input("Enter n: "))
k = int(input("Enter k: "))
A = np.zeros((m, n), dtype=float)
print(A)
print(shape(A))
B = np.zeros((n, k), dtype=float)
print(B)
print(shape(B))
print("Enter Matrix A: ")
for i in range(0, m):
for j in range(0, n):
A[i][j] = float(input())
print("Enter Matrix B: ")
for i in range(0, n):
for j in range(0, k):
B[i][j] = float(input())
print("Here are your matrices A and B: ")
print(A)
print(B)
C = np.zeros((m, k), dtype=float)
print(C)
print(shape(C))
for i in range(0, m):
for j in range(0, k):
for l in range(0, n):
C[i][j] += A[i][l] * B[l][j]
print("Here is your answer matrix C: ")
print(C)
三、一个简单例子
程序运行结果:
计算器计算结果:
四、思考过程
总结
比计算器快。