| import numpy as np
|
| X = rand.rand(10, 2)
|
| dist_sq = np.sum((X[:, np.newaxis, :] - X[np.newaxis, :, :]) ** 2, axis=-1)
|
| differences = X[:, np.newaxis, :] - X[np.newaxis, :, :] # for each pair of points, compute differences in their coordinates
|
| sq_differences = differences ** 2 # square the coordinate differences
|
| dist_sq = sq_differences.sum(-1) # sum the coordinate differences to get the squared distance
|
| dist_sq.shape
|
|
|
| ---------
|
| output:
|
| (10, 10)
|