python - Outer product with arrays of multiple dimensions -


i have d numpy arrays of shape (2, s, t, ...), , i'd multiply each of them each other such output has shape (2, ..., 2, s, t, ...) d 2s. example, d==3:

import numpy  d = 3 = numpy.random.rand(d, 2, 7, 8)  out = numpy.empty((2, 2, 2, 7, 8)) in range(2):     j in range(2):         k in range(2):             out[i, j, k] = a[0][i]*a[1][j]*a[2][k] 

if s, t, ... not present (which use case), classical outer product.

i thought meshgrid can't quite work.

any hints?

i use numpy.einsum

c = a[0] in range(d-1): #adds 1 dimension in each iteration     c = np.einsum('i...,...->i...', a[i+1],c) 

this gives same result yours, axes in reverse order:

c.swapaxes(0,2)==out #yields true 

you can either reverse first few axes or adapt rest of code, whatever works better you.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -