如何在python中进行点生产?
编写函数dot_乘积(M,N),该函数将两个矩阵M和N作为参数,并返回一个包含这些矩阵的点积的新矩阵 我理解逻辑,但不知道如何将每一行和每一列相乘,这就是我目前所知道的如何在python中进行点生产?,python,linear-algebra,Python,Linear Algebra,编写函数dot_乘积(M,N),该函数将两个矩阵M和N作为参数,并返回一个包含这些矩阵的点积的新矩阵 我理解逻辑,但不知道如何将每一行和每一列相乘,这就是我目前所知道的 def dot_product(M, N): """ takes as parameters two matrices M and N, and returns a new matrix containing dot product of these matrices """ asse
def dot_product(M, N):
""" takes as parameters two matrices M and N, and returns a new matrix
containing dot product of these matrices
"""
assert( len(M[0]) == len(N)), " innter length differnet "
new_matrix = zeros(len(M[0]) , len(N))
transP = transpose(N)
storing_values = []
for r in range(len(M)):
for c in range(len(M[0])):
storing_values += M[r][c] * transP[r][c]
我使用辅助函数转置来旋转矩阵N,所以当我迭代时,我会看到两个矩阵的相同列和行
输出应如下所示:
[[26.00, 4.00]
[62.00,13.00]使用numpy内置的点积函数看起来很简单: 应具有以下效果:
import numpy as np
def dot_product(M, N):
return np.dot(np.asarray(M), np.asarray(N))
一般来说,python中的许多功能已经在numpy之类的包中实现了,我尽量不重新发明轮子。不确定这在您的情况下是否可以接受,因为这似乎是一个家庭作业问题,但这是获取两个矩阵的点积的最简单方法。顺便说一下,我通过简单的谷歌搜索得到了这个答案:“python中的点积”谢谢您的评论,但我不允许使用numpy