Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vectorize np.dot(vector_a, vector_b[:,i]) for i in range?

I have a numpy array vector_a of shape (3,1). If I multiply it with a vector_b of shape (1,3) I get a result of shape (3,3).

Now, vector_b is actually an (3,N) numpy array of column vectors. I want to multiply each of these column vectors by vector_a to produce N 3x3 matrices, result of shape (N,3,3)

I have done the following:

r = np.dot(vector_a.reshape(1,3,1), vector_b.T.reshape(N, 1, 3))

and I was expecting r to be of shape (N,3,3) but I got a shape of (1,3,64,3)??? I don't know why I'm getting this shape. Both vector_a and vector_b are C contiguous. I tried to convert vector_b to F contiguous before doing the vector_b.T.reshape(N, 1, 3) but I still get the same r shape (1,3,64,3).

Does anybody know how to write the right expression?

like image 777
martinako Avatar asked Nov 16 '25 05:11

martinako


1 Answers

As an alternative solution, if you use einsum, you can avoid having to reshape the array for the dot product:

np.einsum('ij,jk->kij', vector_a, vector_b)
like image 54
Alex Riley Avatar answered Nov 18 '25 17:11

Alex Riley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!