python中三种矩阵乘法
此处不是对应元素相乘的矩阵乘法,主要是“行x列”这种。
① “@”符号的乘法
import numpy as np
a = np.array([[2,3],[1,2],[3,1]])
b = np.array([[1,2,3],[2,1,3]])
res1 = a@b
print(res1)
'''
output:[[ 8 7 15]
[ 5 4 9]
[ 5 7 12]]
'''
② numpy包中封装的函数dot()
res2 = np.dot(a,b)
print(res2)
'''
output:[[ 8 7 15]
[ 5 4 9]
[ 5 7 12]]
'''
③暴力破解 非函数式纯python代码
x1,mid = a.shape
_,x2 = b.shape
res3 = [[0]*x2 for _ in range(x1)]
for i in range(x1):
for j in range(x2):
temp_sum = 0
for k in range(mid):
temp = a[i][k]*b[k][j]
temp_sum += temp
res3[i][j] = temp_sum
print(res3)
'''
output:[[ 8 7 15]
[ 5 4 9]
[ 5 7 12]]
'''