Python大数组元素比较

时间:2017-05-25 15:26:03

标签: arrays performance python-3.x numpy comparison

我正在尝试生成3D坐标列表,然后仅选择欧几里德范数间隔大于设定值的坐标。坐标列表可以长达10 ^ 6,我想有效地执行此操作(如果可能,使用numpy)。

编辑不包括尝试递归实施(这不起作用)

内存密集型Numpy实施

改编自http://www.arvindravichandran.com/articles/distance/

L = int(1e3)  # simulation box dimension
dim = 3  # Dimensions
N = myran.randint(1, L**2, size=1)[0]  # Number of coords
rangN = np.arange(N)

# Generate random positions
r = (np.random.randint(0, L - 1, size=(N, dim)))  # coords

# uti is a list of two (1-D) numpy arrays
# containing the indices of the upper triangular matrix
uti = np.triu_indices(N, k=1) # k=1 eliminates diagonal indices

# uti[0] is i, and uti[1] is j from the previous example
dr = r[uti[0]] - r[uti[1]]  # computes differences between positions

D = np.sqrt(np.einsum('...i,...i', dr, dr))  # computes distances; flat array

ind = D <= 1  # getting grouped indices

good = [x for x in rangN if x not in set((*uti[0][ind], *uti[1][ind]))] +\
       [x for x in set(uti[0][ind]) if x not in uti[1][ind]]

locs = r[good]

警告。运行上面的代码块会使用很多的内存

0 个答案:

没有答案