Well, I want to implement a multiplication matrix by a vector in Python without NumPy. So given a matrix for example (2x2) in this format:
A = [ [2, 1],
[5, 7] ]
And given a vector for example (2x1) in this format:
b = [ [11],
[13] ]
And I want to get this vector (2x1):
с = [ [35],
[146] ]
What I tried:
def myzeros(n): # create zero vector
res = []
for i in range(n):
res.append([0])
return res
def mydot(A, B):
res = myzeros(len(B)) # create zero vector of size B
for i in range(len(A)):
res.append( sum(A[i][j]*B[j] for j in range(len(A[0]))) )
return res
And corresponding error:
res.append( sum(A[i][j]*B[j] for j in range(len(A[0]))) )
TypeError: unsupported operand type(s) for +: 'int' and 'list'
Where's the mistake?
sumon a generator that is returninglistobjects. That is becauseA[i][j]*B[j]evaluates to alist, I believe you meantA[i][j]*B[j][i]- juanpa.arrivillaga