fork download
  1. def matmul(X, Y):
  2. i, k1 = len(X), len(X[0])
  3. k2, j = len(Y), len(Y[0])
  4. if k1 != k2 :
  5. print("size error!")
  6. return None
  7. return [[sum([x_ik*y_kj for x_ik, y_kj in zip(x, [*y])]) for y in zip(*Y)] for x in X]
  8.  
  9. A = [[1,2,3,4],[2,3,4,5],[3,4,5,6]]
  10. B = [[1,3,5,7,9],[2,4,6,8,10],[0,1,2,3,4],[5,4,3,2,1]]
  11.  
  12. print(matmul(A, B))
Success #stdin #stdout 0.05s 9636KB
stdin
Standard input is empty
stdout
[[25, 30, 35, 40, 45], [33, 42, 51, 60, 69], [41, 54, 67, 80, 93]]