python - Using einsum without explicit replication -


i have matrix of shape (n, n) , matrix b of shape (p, n). need matrix c such

c[i] = (a * b[i, np.newaxis, :]) * b[i, :, np.newaxis] 

i'm doing doing np.matlib.repmat(a) , using np.einsum follows

a1 = np.matlib.repmat(a, p, 1).reshape(p, n, n) c = np.einsum('ijk, ij..., ik... -> ijk', a1, b, b) 

but can see creating a1 waste of resources it's using same values on , over. there anyway can without creating intermediate matrix a1?

use np.einsum -

np.einsum('ij,kij,kij->kij',a,b[:,none,:],b[...,none]) 

Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -