这个不是很难,看看就好。
丢代码:
1 # 矩阵乘法函数 2 def matrix(a1, a2): 3 h1 = len(a1) 4 w1 = len(a1[0]) 5 h2 = len(a2) 6 w2 = len(a2[0]) 7 # 定矩阵大小 8 h3 = h1 9 w3 = w2 10 a3 = [[0 for j in range(w3)] for i in range(h3)] 11 # print((h3,w3)) 12 # print(a3) 13 for i in range(h3): 14 for j in range(w3): 15 sum = 0 16 for k in range(h3): 17 sum += a1[i][k] * a2[k][j] 18 # print(a1[i][k],a2[k][j]) 19 # print("sum += a1[{}][{}] * a2[{}][{}] -> {}".format(i,k,k,j,a1[i][k] * a2[k][j])) 20 a3[i][j] = sum 21 # print("a3[{}][{}] = {}".format(i,j,sum)) 22 # print(a3) 23 return a3 24 25 26 # mat = eval(input("请输入矩阵:")) 27 # 测试数据 28 mat = [[3,-4,3],[-4,6,3],[3,3,1]] 29 mat = [[2,3,2],[10,3,4],[3,6,1]] 30 31 v = u = [[0],[0],[1]] 32 round = 10 33 for i in range(round): 34 u = matrix(mat, v) 35 print(u, end=" --- ") 36 # 求max(u) 37 _max = 0 38 for i in u: 39 for j in i: 40 if abs(j) > _max: 41 _max = j 42 # 除_max 43 for i in range(len(u)): 44 for j in range(len(u[0])): 45 v[i][j] = u[i][j] / _max 46 print(v, end=" --- ") 47 print(_max)