0

X and Y are both 3d arrays with dimensions (a,b,c). My goal is to do a dot product.

Consider that case where index i and j are scalar, and (X[i,:,j].T).dot(Y[i,:,j]) would be simple and return a scalar.

However, if I try to do vectorization, i and j become 1d arrays, and (X[i,:,j].T).dot(Y[i,:,j]) return a matrix but I am expecting a 1d array as result. How do I get around this problem ?

asked Mar 20, 2020 at 12:42
2
  • 1
    you can use np.einsum to calculate the product. if you can, add some random values with expected output Commented Mar 20, 2020 at 12:49
  • it would be nice if you could give us coherent code Commented Mar 20, 2020 at 12:53

1 Answer 1

1

Naive implementation using list comprehension:

a,b,c = X.shape
r1 = [(X[i,:,j].T).dot(Y[i,:,j]) for i in range(a) for j in range(c)]

Implementation using np.einsum:

r2 = np.einsum('ijk,ijk->ik', X,Y).flatten()
answered Mar 20, 2020 at 13:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.