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
Post a Comment