[算法导论] 矩阵乘法

import numpy as np


def matmul(a, b):
    return np.matmul(a, b)


def manual_matmul(a, b):
    C = []
    for x in range(len(a)):
        tp = []
        for y in range(len(b[0])):
            tp.append(sum(a[x][k]*b[k][y] for k in range(len(b))))
        C.append(tp)
    return np.array(C)


if __name__ == '__main__':
    # A=[],B=[] 这是错的
    A = []
    B = []
    na, ma = map(int, input().split())
    # 按行存储就可以
    for i in range(na):
        tmp = []
        tmp = list(map(int, input().split()))
        A.append(tmp)
    nb, mb = map(int, input().split())
    for i in range(nb):
        tmp = []
        tmp = list(map(int, input().split()))
        B.append(tmp)

    print("熊熊")
    print(A)
    print(B)
    #print(matmul(A, B))
    print(manual_matmul(A, B))

上一篇:torch.matmul


下一篇:Min-Max容斥及其推广和应用