根据另一个数组中包含的索引有效地求和 4D 数组
Posted
技术标签:
【中文标题】根据另一个数组中包含的索引有效地求和 4D 数组【英文标题】:Sum 4D array efficiently according to indices contained in another array 【发布时间】:2014-02-20 02:14:57 【问题描述】:我有一个 4D 数组,本质上是一系列立方体。除了我知道位置的值的子立方体之外,这些立方体大部分都用零填充。我需要将所有这些立方体加在一起成一个立方体。我可以简单地使用 np.sum 沿轴 = 3 执行此操作,但这是蒙特卡洛过程的一部分,并且执行了很多次。我想知道,因为我知道子多维数据集在多维数据集中的位置,我可以更有效地对它们求和,因为大多数求和操作都会添加零 - 这些多维数据集的大小很大(> 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
【问题讨论】:
您是否尝试使用稀疏矩阵,因为您的立方体包含很多零? 稀疏矩阵仅限于二维 【参考方案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)
。
【讨论】:
以上是关于根据另一个数组中包含的索引有效地求和 4D 数组的主要内容,如果未能解决你的问题,请参考以下文章
Numpy: arr[...,0,:] 有效。但是如何存储 slice 命令中包含的数据(...、0、:)?