我有一个4D阵列,基本上是一系列立方体。除了我知道位置的值的子立方体之外,这些立方体大多用零填充。我需要将所有这些立方体合并为一个立方体。我可以简单地使用np.sum沿轴= 3来做这个,但这是monte carlo过程的一部分,并且完成了很多次。我想知道,因为我知道立方体在立方体中的位置可以更有效地对它们求和,因为大多数求和操作将添加零 - 这些立方体具有相当大的尺寸(> 100 ^ 3)所以如果我能这将是一个巨大的节省。我对Python / Numpy很新,我发现很难摆脱循环思维!一般来说,我正在寻找一种方法来处理大型n维阵列,但仅限于某些部分。我意识到掩盖的阵列在这里浮现在脑海中,但我已经尝试过,并且不认为它在这种情况下提供任何加速;除非我离开那里!
编辑:以下是我想要做的三个可怕的版本 - 可能在上下文中没有多大意义 - 基本上涉及计算彼此相距一定距离的多个电荷的影响但是每个电荷都不会影响自身。其中一个确定了飞行中的距离,两个使用预先计算的数组,但是它必须“对齐”并加总 - 再次可能没有意义脱离背景但这是我到目前为止
def coloumbicForces3(carrierArray, cubeEnergeticDisorderArray, array):
cubeLen=100
offsetArray=np.array([[0,0,0],[0,0,+1],[0,0,-1],[0,+1,0],[0,-1,0],[+1,0,0],[-1,0,0]])
indices=np.zeros((7,3,len(carrierArray)), dtype=np.int32)
indices1=a=indices.reshape((7*len(carrierArray),3))
tIndices=cubeLen-indices
superimposedArray=np.zeros((cubeLen,cubeLen,cubeLen,2+2), dtype=myFloat)
sumArray=np.zeros((cubeLen,cubeLen,cubeLen))
for i in range(len(carrierArray)):
indices[:,:,i]=offsetArray+carrierArray[i,1:4]
for c, carrierC in enumerate(carrierArray[:,0]):
for k, carrierK in enumerate(carrierArray[:,0]):
if c==k:
continue
for (x,y,z) in indices[:,:,k]:
#print c, indices[:,:,c]
if(carrierC==1):
superimposedArray[x,y,z,c]=cubeEnergeticDisorderArray[x,y,z,c]=-1*array[cubeLen-x,cubeLen-y, cubeLen-z]
else:
superimposedArray[x,y,z,c]=cubeEnergeticDisorderArray[x,y,z,c]=array[cubeLen-x,cubeLen-y, cubeLen-z]
b = np.ascontiguousarray(a).view(np.dtype((np.void, a.dtype.itemsize * a.shape[1])))
_,idx = np.unique(b, return_index=True)
aUnique=a[idx]
for (i,j,k) in aUnique:
sumArray[i,j,k]=np.sum(superimposedArray[i,j,k])
for c, carrierC in enumerate(carrierArray[:,0]):
for (i,j,k) in aUnique:
cubeEnergeticDisorderArray[i,j,k,c]=cubeEnergeticDisorderArray[i,j,k,-2]+sumArray[i,j,k]
return cubeEnergeticDisorderArray
def coloumbicForces(carrierArray, cubeEnergeticDisorderArray, array):
cubeLen= len(cubeEnergeticDisorderArray[0,:,:,0])
superimposedArray=np.zeros((cubeLen,cubeLen,cubeLen,2+2), dtype=myFloat)
for k, carrier in enumerate(carrierArray[:,0]):
superimposedArray[:,:,:,k]=cubeEnergeticDisorderArray[:,:,:,k]=array[cubeLen-carrierArray[k,1]:2*cubeLen-carrierArray[k,1],cubeLen-carrierArray[k,2]:2*cubeLen-carrierArray[k,2],cubeLen-carrierArray[k,3]:2*cubeLen-carrierArray[k,3]]
if (carrier==1):
a=superimposedArray[:,:,:,k]
b=cubeEnergeticDisorderArray[:,:,:,k]
superimposedArray[:,:,:,k]=ne.evaluate("a*-1")
cubeEnergeticDisorderArray[:,:,:,k]=ne.evaluate("b*-1")
sumArray=ne.evaluate("sum(superimposedArray, axis=3)")
for k, carrier in enumerate(carrierArray[:,0]):
a=cubeEnergeticDisorderArray[:,:,:,k]
b=cubeEnergeticDisorderArray[:,:,:,-2]
cubeEnergeticDisorderArray[:,:,:,k]=ne.evaluate("sumArray-a+b")
return cubeEnergeticDisorderArray
def coloumbicForces2(carrierArray, cubeEnergeticDisorderArray, array):
x0=carrierArray[0,1]
y0=carrierArray[0,2]
z0=carrierArray[0,3]
x1=carrierArray[1,1]
y1=carrierArray[1,2]
z1=carrierArray[1,3]
cubeEnergeticDisorderArray[x0,y0,z0,0]=cubeEnergeticDisorderArray[x0,y0,z0,-2]-(1.60217657e-19)*2995850595.79/(distance([x0,y0,z0], carrierArray[1,1:4])*1e-9)
cubeEnergeticDisorderArray[x0-1,y0,z0,0]=cubeEnergeticDisorderArray[x0-1,y0,z0,-2]-(1.60217657e-19)*2995850595.79/(distance([x0-1,y0,z0], carrierArray[1,1:4])*1e-9)
cubeEnergeticDisorderArray[x0+1,y0,z0,0]=cubeEnergeticDisorderArray[x0+1,y0,z0,-2]-(1.60217657e-19)*2995850595.79/(distance([x0+1,y0,z0], carrierArray[1,1:4])*1e-9)
cubeEnergeticDisorderArray[x0,y0-1,z0,0]=cubeEnergeticDisorderArray[x0,y0-1,z0,-2]-(1.60217657e-19)*2995850595.79/(distance([x0,y0-1,z0], carrierArray[1,1:4])*1e-9)
cubeEnergeticDisorderArray[x0,y0+1,z0,0]=cubeEnergeticDisorderArray[x0,y0+1,z0,-2]-(1.60217657e-19)*2995850595.79/(distance([x0,y0+1,z0], carrierArray[1,1:4])*1e-9)
cubeEnergeticDisorderArray[x0,y0,z0-1,0]=cubeEnergeticDisorderArray[x0,y0,z0-1,-2]-(1.60217657e-19)*2995850595.79/(distance([x0,y0,z0-1], carrierArray[1,1:4])*1e-9)
cubeEnergeticDisorderArray[x0,y0,z0+1,0]=cubeEnergeticDisorderArray[x0,y0,z0+1,-2]-(1.60217657e-19)*2995850595.79/(distance([x0,y0,z0+1], carrierArray[1,1:4])*1e-9)
cubeEnergeticDisorderArray[x1,y1,z1,1]=cubeEnergeticDisorderArray[x1,y1,z1,-2]+(1.60217657e-19)*2995850595.79/(distance([x1,y1,z1], carrierArray[0,1:4])*1e-9)
cubeEnergeticDisorderArray[x1-1,y1,z1,1]=cubeEnergeticDisorderArray[x1-1,y1,z1,-2]+(1.60217657e-19)*2995850595.79/(distance([x1-1,y1,z1], carrierArray[0,1:4])*1e-9)
cubeEnergeticDisorderArray[x1+1,y1,z1,1]=cubeEnergeticDisorderArray[x1+1,y1,z1,-2]+(1.60217657e-19)*2995850595.79/(distance([x1+1,y1,z1], carrierArray[0,1:4])*1e-9)
cubeEnergeticDisorderArray[x1,y1-1,z1,1]=cubeEnergeticDisorderArray[x1,y1-1,z1,-2]+(1.60217657e-19)*2995850595.79/(distance([x1,y1-1,z1], carrierArray[0,1:4])*1e-9)
cubeEnergeticDisorderArray[x1,y1+1,z1,1]=cubeEnergeticDisorderArray[x1,y1+1,z1,-2]+(1.60217657e-19)*2995850595.79/(distance([x1,y1+1,z1], carrierArray[0,1:4])*1e-9)
cubeEnergeticDisorderArray[x1,y1,z1-1,1]=cubeEnergeticDisorderArray[x1,y1,z1-1,-2]+(1.60217657e-19)*2995850595.79/(distance([x1,y1,z1-1], carrierArray[0,1:4])*1e-9)
cubeEnergeticDisorderArray[x1,y1,z1+1,1]=cubeEnergeticDisorderArray[x1,y1,z1+1,-2]+(1.60217657e-19)*2995850595.79/(distance([x1,y1,z1+1], carrierArray[0,1:4])*1e-9)
return cubeEnergeticDisorderArray
答案 0 :(得分:1)
如果您知道子立方体是否可以使用花式索引仅在您需要的位置求和,例如:
import numpy as np
from numpy.random import random
c1 = random((10, 10, 10, 10))
c2 = random((10, 10, 10, 10))
c3 = np.zeros_like(c2)
以下是我要总结的指数:
i1 = [0, 2, 4, 6]
i2 = [0, 1, 3, 7]
i3 = [1, 5, 8, 9]
i4 = [1, 6, 7, 8]
c3[i1,i2,i3,i4] = c1[i1,i2,i3,i4] + c2[i1,i2,i3,i4]
仅在积分上加以总结:p1(0,0,1,1)
,p2(2,1,5,6)
,p3(4,3,8,7)
和p4(6,7,9,8)
。