6

Sorry for the badly explained title. I am trying to parallelise a part of my code and got stuck on a dot product. I am looking for an efficient way of doing what the code below does, I'm sure there is a simple linear algebra solution but I'm very stuck:

puy = np.arange(8).reshape(2,4)
puy2 = np.arange(12).reshape(3,4)

print puy, '\n'
print puy2.T

zz = np.zeros([4,2,3])

for i in range(4):
    zz[i,:,:] = np.dot(np.array([puy[:,i]]).T,
                np.array([puy2.T[i,:]]))
0

2 Answers 2

6

One way would be to use np.einsum, which allows you to specify what you want to happen to the indices:

>>> np.einsum('ik,jk->kij', puy, puy2)
array([[[ 0,  0,  0],
        [ 0, 16, 32]],

       [[ 1,  5,  9],
        [ 5, 25, 45]],

       [[ 4, 12, 20],
        [12, 36, 60]],

       [[ 9, 21, 33],
        [21, 49, 77]]])
>>> np.allclose(np.einsum('ik,jk->kij', puy, puy2), zz)
True
Sign up to request clarification or add additional context in comments.

Comments

3

Here's another way with broadcasting -

(puy[None,...]*puy2[:,None,:]).T

Comments

Your Answer

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.