I am trying to find the shortest distance between two sets of arrays. The x- arrays are identical and just contain integers. Here is an example of what I am trying to do:
import numpy as np
x1 = x2 = np.linspace(-1000, 1000, 2001)
y1 = (lambda x, a, b: a*x + b)(x1, 2, 1)
y2 = (lambda x, a, b: a*(x-2)**2 + b)(x2, 2, 10)
def dis(x1, y1, x2, y2):
return sqrt((y2-y1)**2+(x2-x1)**2)
min_distance = np.inf
for a, b in zip(x1, y1):
for c, d in zip(x2, y2):
if dis(a, b, c, d) < min_distance:
min_distance = dis(a, b, c, d)
>>> min_distance
2.2360679774997898
This solution works, but the problem is runtime. If x has a length of ~10,000, the solution is infeasible because the program ha O(n^2) runtime. Now, I tried making some approximations to speed the program up:
for a, b in zip(x1, y1):
cut = (x2 > a-20)*(x2 < a+20)
for c, d in zip(x2, y2):
if dis(a, b, c, d) < min_distance:
min_distance = dis(a, b, c, d)
But the program is still taking longer than I'd like. Now, from my understanding, it is generally inefficient to loop through a numpy array, so I'm sure there is still room for improvement. Any ideas on how to speed this program up?