我想在python中优化以下代码:
for imode in N.arange(3*natom): #Loop on perturbation (6 for 2 atoms)
for ikpt in N.arange(nkpt):
for iband in N.arange(nband):
for iatom1 in N.arange(natom):
for iatom2 in N.arange(natom):
for idir1 in N.arange(0,3):
for idir2 in N.arange(0,3):
fan_corrQ[imode,ikpt,iband] += EIG2D[ikpt,iband,idir1,iatom1,idir2,iatom2]*\
displ_red_FAN2[imode,iatom1,iatom2,idir1,idir2]
ddw_corrQ[imode,ikpt,iband] += ddw_save[ikpt,iband,idir1,iatom1,idir2,iatom2]*\
displ_red_DDW2[imode,iatom1,iatom2,idir1,idir2]
如您所见,我想对我的多空间 python 数组的一些索引求和。我想要类似的东西:
for imode in N.arange(3*natom): #Loop on perturbation (6 for 2 atoms)
for ikpt in N.arange(nkpt):
for iband in N.arange(nband):
fan_corrQ[imode,ikpt,iband] = N.dot(EIG2D[ikpt,iband,:,:,:,:],displ_red_FAN2.T[imode,:,:,:,:])
ddw_corrQ[imode,ikpt,iband] = N.dot(ddw_save[ikpt,iband,:,:,:,:],displ_red_DDW2.T[imode,:,:,:,:])
当然,我有一个问题是不能乘以相同的索引,所以我重新定义了它。我还必须指出,我正在处理复数:
displ_red_DDW2 = N.zeros((3*natom,3,natom,3,natom),dtype=complex)
我还在 ipython 中尝试了一个小的虚拟程序来测试它:
import numpy as N
atom =2
displ_red_FAN2 = N.zeros((3*natom,3,natom,3,natom),dtype=complex)
EIG2D = N.zeros((216,12,3,2,3,2))
所以我有 displ_red_FAN2.shape = (6, 3, 2, 3, 2) 和 EIG2D.shape = (216, 12, 3, 2, 3, 2)
因此,如果我执行以下操作:
N.dot(EIG2D[1,1,:,:,:,:],displ_red_FAN2[1,:,:,:,:].T).shape
它应该给出 (3,2,3,2) 但它给出的是 (3, 2, 3, 2, 3, 3) ??? 然后当乘法完成时,我想我将不得不做某种求和来降低维数。
任何帮助都会很棒!
干杯!
塞缪尔