matrix乘法在Python?
我正在尝试使用纯python将两个matrix相乘。 input(X1是3×3,Xt是3×2):
X1 = [[1.0016, 0.0, -16.0514], [0.0, 10000.0, -40000.0], [-16.0514, -40000.0, 160513.6437]] Xt = [(1.0, 1.0), (0.0, 0.25), (0.0, 0.0625)]
其中Xt是另一个matrix的zip转置。 现在这里是代码:
def matrixmult (A, B): C = [[0 for row in range(len(A))] for col in range(len(B[0]))] for i in range(len(A)): for j in range(len(B[0])): for k in range(len(B)): C[i][j] += A[i][k]*B[k][j] return C
python给我的错误是:IndexError:列表索引超出范围。 现在我不确定Xt是否被识别为一个matrix,并且仍然是一个列表对象,但从技术上讲,这应该工作。
如果你真的不想使用numpy
你可以这样做:
def matmult(a,b): zip_b = zip(*b) # uncomment next line if python 3 : # zip_b = list(zip_b) return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) for col_b in zip_b] for row_a in a] x = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] y = [[1,2],[1,2],[3,4]] import numpy as np # I want to check my solution with numpy mx = np.matrix(x) my = np.matrix(y)
结果:
>>> matmult(x,y) [[12, 18], [27, 42], [42, 66], [57, 90]] >>> mx * my matrix([[12, 18], [27, 42], [42, 66], [57, 90]])
这是不正确的初始化。 你与col交换行!
C = [[0 for row in range(len(A))] for col in range(len(B[0]))]
正确的初始化将是
C = [[0 for col in range(len(B[0]))] for row in range(len(A))]
另外我会build议使用更好的命名约定。 在debugging中会帮助你很多。 例如:
def matrixmult (A, B): rows_A = len(A) cols_A = len(A[0]) rows_B = len(B) cols_B = len(B[0]) if cols_A != rows_B: print "Cannot multiply the two matrices. Incorrect dimensions." return # Create the result matrix # Dimensions would be rows_A x cols_B C = [[0 for row in range(cols_B)] for col in range(rows_A)] print C for i in range(rows_A): for j in range(cols_B): for k in range(cols_A): C[i][j] += A[i][k] * B[k][j] return C
你可以做更多,但你明白了…
这里是一个简单的纯Python代码matrix/向量例子的链接:
故障发生在这里:
C[i][j]+=A[i][k]*B[k][j]
它在k = 2时崩溃。 这是因为元组A[i]
只有2个值,因此在出错之前只能调用A [i] [1]。
编辑:也听杰拉德的答案,你的C是错的。 它应该是C=[[0 for row in range(len(A))] for col in range(len(A[0]))]
。
只是一个提示:你可以用一个乘法replace第一个循环,所以C=[[0]*len(A) for col in range(len(A[0]))]
,它将是C=[[0]*len(A) for col in range(len(A[0]))]
你的matrixC
的形状是错误的; 这是你真正想要的东西的转置。 (但我同意ulmangt:正确的事情几乎肯定是使用numpy,真的。)
m=input("row") n=input("col") X=[] for i in range (m): m1=[] for j in range (n): m1.append(input("num")) X.append(m1) Y=[] for i in range (m): n1=[] for j in range (n): n1.append(input("num")) Y.append(n1) # result is 3x3 result = [[0,0,0], [0,0,0], [0,0,0]] # iterate through rows of X for i in range(len(X)): # iterate through columns of Y for j in range(len(Y[0])): # iterate through rows of Y for k in range(len(Y)): result[i][j] += X[i][k] * Y[k][j] for r in result: print(r)
纯Python中的matrix乘法。
def matmult(m1,m2): r=[] m=[] for i in range(len(m1)): for j in range(len(m2[0])): sums=0 for k in range(len(m2)): sums=sums+(m1[i][k]*m2[k][j]) r.append(sums) m.append(r) r=[] return m
所有下面的答案将返回给你的列表。你需要将其转换为matrix
def MATMUL(X, Y): rows_A = len(X) cols_A = len(X[0]) rows_B = len(Y) cols_B = len(Y[0]) if cols_A != rows_B: print "Matrices are not compatible to Multiply. Check condition C1==R2" return # Create the result matrix # Dimensions would be rows_A x cols_B C = [[0 for row in range(cols_B)] for col in range(rows_A)] print C for i in range(rows_A): for j in range(cols_B): for k in range(cols_A): C[i][j] += A[i][k] * B[k][j] C = numpy.matrix(C).reshape(len(A),len(B[0])) return C