I have a 2D lattice with U × U number of grids. My goal is to create an array – of shape U^2 × U^2 – where each entry denotes the least-distance between each grid and any other grid.
Currently, for U=60, my Python script takes ~5 minutes to complete. As a novice programmer, I suspect there must be a more efficient approach – I've read about performance issues related to numpy loops. My objective is to handle U=1000.
import numpy as np
np.random.seed(13)
import matplotlib.pyplot as plt
import time
start_time = time.time()
U = 60
def FromGridToList(i_, j_):
return i_*U + j_
def FromListToGrid(l_):
i = np.floor(l_/U)
j = l_ - i*U
return np.array((i,j))
Ulist = range(U**2)
dist_array = []
for l in Ulist:
print l, 'of', U**2
di = np.array([np.abs(FromListToGrid(l)[0]-FromListToGrid(i)[0]) for i, x in enumerate(Ulist)])
di = np.minimum(di, U-di)
dj = np.array([np.abs(FromListToGrid(l)[1]-FromListToGrid(i)[1]) for i, x in enumerate(Ulist)])
dj = np.minimum(dj, U-dj)
d = np.sqrt(di**2+dj**2)
dist_array.append(d)
dist_array = np.vstack(dist_array)
print time.time() - start_time, 'seconds'
1000² x 1000²
array? Do you realize that'll take1-8 TB
of memory? Good luck... \$\endgroup\$