Python 算法导论代码 矩阵的乘法

Python 算法导论代码 矩阵的乘法,第1张

def Matrix_multiply(A,B):
    if len(A[0])!=len(B):
        return "error"
    C=[[0 for j in range( len(A[0]))] for i in range( len(B))]
    for i in range(len(A[0])):
        for j in range(len(B)):
            C[i][j]=0
            for k in range(len(A)):
                C[i][j]=C[i][j]+A[i][k]*B[k][j]
                print(C[i][j])
    return C
A=[[1,2],[3,4]]
B=[[1,2],[3,4]]

Matrix_multiply(A,B)

import numpy as np
A =np.array([[1,2,3]])
B= np.array([[1,2,3],[3,4,1],[3,4,1]])

print(np.multiply(B,A),"numpy")

def Matrix_multiply(A,B):
    if A.shape[1]!=B.shape[0]:
        return "error"
    c=d=np.ones((1,3))
    for i in range(A.shape[0]):
        for j in range(B.shape[1]):
            c[i,j]=0
            for k in range (A.shape[1]):
                c[i,j]=c[i,j]+A[i,k]*B[k,j]


    return c
print(Matrix_multiply(A,B))

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/786209.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存