I want to compute distances between every pairs of nodes on a regular 3D-grid. The grid may be huge, so I want to optimize the computation (privilege to the CPU).
After many many tests, I gave up the ‘scitools’ module, that is convenient only for python2, and use a combination of numpy.meshgrid() and scipy.spatial.distance.pdist(). For instance, for a 20x20x20 grid:
distances = scipy.spatial.distance.pdist(np.transpose(np.array(np.meshgrid(range(-10,10,1),range(-10,10,1),range(-10,10,1)))).reshape([20**3,3]))
Is it optimized ?
first 'Maybe this is the way to go...': I see there is a ‘sparse’ option in meshgrid, so it would please me to use
np.meshgrid(range(-10,10,1),range(-10,10,1),range(-10,10,1),sparse=True)
rather than
np.meshgrid(range(-10,10,1),range(-10,10,1),range(-10,10,1))
Indeed, I could even keep the sparse shape in memory since it would be convenient later in the code. But I don’t find a satisfying syntaxe to combine pdist() with a sparse meshgrid.