我正在尝试编写一个具有多重运算符重载函数的类,该函数用于处理矩阵乘法。 我的方法是
我的问题:
老实说,我在课堂上对自我的运用感到困惑,是否有推荐的例子可以帮助像我这样的初学者理解这一概念?
先谢谢大家。
这是我的密码
# Constructor
def __init__(self, grid): # I don't know if i need another input here or not like def __init__(self, grid , matrixB)
self.g = grid
self.h = len(grid)
self.w = len(grid[0])
def __mul__(self, other):
af_trans = other.T() # Transpose the second matrix
for i in range(0,self.h):
every_row=[]
for j in range(0,self.h):
element_product = self.dot(self.g[i],other.g[j])
every_row.append(element_product)
return every_row
def T(self):
T_matrix = []
for i in range(0,self.h):
new_row = []
for j in range(0,self.w):
T_element = self.g[j][i]
new_row.append(T_element)
T_matrix.append(new_row)
return T_matrix
def dot(self,other):
sumproduct=0
for i in range(0,self.h):
element_product = self.g[i][i]*other.g[i][i] #The first element
of Matrix A times the first element of Matrix B
sum_product = sum_product+element_product
return sum_product