﻿ python – 在numpy.einsum中进行省略号广播 - 代码日志

#### python – 在numpy.einsum中进行省略号广播

``````numpy.einsum('...lmn,lmno->...o', prefactor, dipoles)
``````

(在这里的例子中,prefactor.shape是(1,1,1,160,160,128),dipoles.shape是(160,160,128,3).执行时,我得到错误：

``````numpy.einsum('...lmn,...lmno->...o', prefactor, dipoles)
``````

https://github.com/numpy/numpy/issues/2455
einsum中索引表示法的改进(Trac#18​​62)

``````einsum('ij...,j->ij...',A,B)
``````

einsum( ‘IJ …,J …-> IJ …’,A,B)

https://github.com/numpy/numpy/issues/2619显示了如何使用nditer来复制einsum行为.通过这种方式,我可以复制你的计算：

``````prefactor = np.random.random((1, 1, 1, 160, 160, 128))
dipoles = np.random.random((160, 160, 128, 3))
x = numpy.einsum('...lmn,...lmno->...o', prefactor, dipoles)
#numpy.einsum('...lmn,lmno->...o', prefactor, dipoles)  # not work

op_axes = [[0,1,2,3,4,5,-1], [-1,-1,-1,0,1,2,3], [0,1,2,-1,-1,-1,3]]
flags = ['reduce_ok','buffered', 'external_loop', 'delay_bufalloc', 'grow_inner']
it = np.nditer([prefactor,dipoles,None], flags, op_flags, op_axes=op_axes)
it.operands[nops][...] = 0
it.reset()
#it.debug_print()
for (x,y,w) in it:
w[...] += x*y
print "\nnditer usage:"
print it.operands[nops] # == x
print it.operands[nops].shape # (1, 1, 1, 3)
``````

op_axes行表示einsum从’… lmn,… lmno-> … o’推断出的内容.