简体   繁体   中英

numpy linear algebra on diagonal arrays without explicit duplication

I have an array w (shape (3000, 100, 100) ) which I want to multiply with another array e (shape (5, 3000) ) such that the result k has shape (5, 5, 100, 100) and

k[:, :, i, j] = e @ np.diag(w[:, i, j]) @ e.T

Since w is so large, it's not practical to make some super_w array with shape (3000, 3000, 100, 100) and explicitly populate the main diagonal. It is also not terribly efficient to loop over i and j . Is there a memory-efficient way to do this, other than breaking up w into chunks?

使用np.einsum -

k = np.einsum('li,ijk,mi->lmjk',e,w,e)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM