Search code examples
pythonmatrix-multiplication

How do I multiply a (2,4)-matrix with a (4,1)-matrix?


I'm trying to multiply two matrices, but the (4,1) matrix is read by python as a (4,1,1). How do I ensure that python reads my matrices correctly and are able to multiply them? I want to be able to get numbers from a list, put them in a matrix and multiply it with another one.

This if for a part of an assignment. I've tried numpys .dot and .matmul functions, but neither work. And I don't think I'm missing any brackets. Even if I replace list-indexes with the actual number, it won't work.

def Md(a,b,c,d,e,f):
    Md = np.array([[a,b,0,0],[c,d,e,f]])
    return Md


pl = [np.array([[2],[3]])]
u = np.array([[pl[0][0]],[pl[0][1]],[pl[0][0]**2],[pl[0][1]**2]])
print(np.matmul(Md(1,-1,1,0,0.5,0.5),u))

I expect the output of this multiplication to be [[-1],[8.5]], but instead i get this error message:

ValueError: shapes (2,4) and (4,1,1) not aligned: 4 (dim 1) != 1 (dim 1)


Solution

  • u = np.array([[pl[0][0]],[pl[0][1]],[pl[0][0]**2],[pl[0][1]**2]]).reshape(4,1)