Python中的@符号也是矩阵乘法操作符
class Matrix:
def __init__(self, matrix):
self.values = matrix
def __matmul__(self, other):
a = self.values
b = other.values
return [
[
sum([float(i) * float(j) for i, j in zip(row, col)])
for col in zip(*b)
]
for row in a
]
A = [[1,2,3],[4,5,6]]
B = [[1,1], [1,1], [1,1]]
Matrix(A) @ Matrix(B)
#Output:
[[6.0, 6.0], [15.0, 15.0]]