2018-10-03 18:59:01 +00:00
|
|
|
# setup: import numpy as np ; N = 50 ; X, Y = np.random.randn(100,N), np.random.randn(40,N) # noqa
|
|
|
|
# run: allpairs_distances(X, Y)
|
2018-04-05 22:07:33 +00:00
|
|
|
|
2018-10-03 18:59:01 +00:00
|
|
|
# pythran export allpairs_distances(float64[][], float64[][])
|
2018-04-05 22:07:33 +00:00
|
|
|
import numpy as np
|
|
|
|
|
2018-10-03 12:38:48 +00:00
|
|
|
|
|
|
|
def allpairs_distances(A, B):
|
|
|
|
""" This returns the euclidean distances squared
|
|
|
|
dist2(x, y) = dot(x, x) - 2 * dot(x, y) + dot(y, y)
|
|
|
|
"""
|
|
|
|
A2 = np.einsum('ij,ij->i', A, A)
|
|
|
|
B2 = np.einsum('ij,ij->i', B, B)
|
2018-10-03 18:59:01 +00:00
|
|
|
return A2[:, None] + B2[None, :] - 2 * np.dot(A, B.T)
|