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

Product and summation with 3d and 1d arrays

Given a 3d array X with dimensions (K,n,m) that can be considered as a stack of K (n,m) matrices and a 1d vector b (dim n), the goal is to obtain the resulting vector r (dim n) each component of which is calculated as:
enter image description here

It is easy to see that the expression under the k-summation (i.e. two internal sums) is just a dot product X_k b X_k (and, therefore, can easily be calculated using numpy). So, the desired vector r is

enter image description here

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

where X_k is the k-th 2d (n,m) ‘layer’ of 3d array X.

I.e. the current solution is

r = 0
for k in range(K):
    r += x[k,:,:] @ (b @ x[k, :, :])

Can r be efficiently calculated avoiding a for-loop by k?
Or maybe there is another efficient way to calculate r?

(I tried np.tensordot but since it is just pure summation by k I didn’t get a correct result yet.)

>Solution :

This looks like a perfect usecase for einsum:

r = np.einsum('kij,l,klj->i', x, b, x)

which will vectorize the operation, e.g. it’s more optimal than a for loop.

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