Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

einsum equivalent for ndarray multiplication

I have the following multiplication routine:

import numpy as np
a = np.random.rand(3,3)
b = np.random.rand(3,50,50)
res = np.zeros((3, 50, 50))
for i in range(50):
    for j in range(50):
        res[:,i,j] = a @ b[:,i,j]

What is the einsum equivalent expression?

Best regards

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Might want to brush up on Einstein summation notation:

res = np.einsum('ij, jkl -> ikl', a, b)

In this case, np.tensordot is also useful:

np.tensordot(a ,b, 1).shape
Out[]: (3, 50, 50)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading