Recursively compute closest pairs
By : Keitarou Okuda
Date : March 29 2020, 07:55 AM
wish of those help The steps are a bit misleading, in that steps 25 are all part of the recursion. At every level of recursion, you need to calculate dLmin, dRmin, and dLRmin. The minimum of these is returned as the answer for that level of recursion. To use your example, you would calculated dLmin as the distance between points 1 and 2, dRmin as the distance between points 3 and 4, and then dLRmin as the distance between points 2 and 3. Since dLRmin is the smallest in your hypothetical case, it would be returned.

finding all closest point pairs if multiple closest pairs exist
By : user2959992
Date : March 29 2020, 07:55 AM
around this issue The following code is for finding the closest point pairs problem in Introduction to Programming with C++ liang textbook. and I'm trying to edit it so it can find all closest point pairs if multiple closest pairs exist. , You may change your algorithm to stock results in vector: code :
std::vector<int> p1, p2;
double shortestDistance = getDistance(points[p1][0], points[p1][1],
points[p2][0], points[p2][1]); // Initialize
for (int i = 0; i < NUMBER_OF_POINTS; i++) {
for (int j = i + 1; j < NUMBER_OF_POINTS; j++) {
const double distance = getDistance(points[i][0], points[i][1],
points[j][0], points[j][1]);
if (shortestDistance >= distance) {
if (shortestDistance > distance) {
p1.clear();
p2.clear();
shortestDistance = distance; // Update shortestDistance
}
p1.push_back(i); // Update p1
p2.push_back(j); // Update p2
}
}
}

Finding the closest pair of points  How to implement split pairs inside recursive side pairs function calls
By : Asmaa Magdy
Date : March 29 2020, 07:55 AM
this one helps. you have a couple of bugs in the closest_side_pair routine. When you call eucledian_closest routine, the length of vector passed to that function is delimiter whereas that should be size. Similarly, when you call closest_split_pair, the length of vector passed is delimiter. That should be size.

Find the closest distance between every galaxy in the data and create pairs based on closest distance between them
By : Hugh
Date : March 29 2020, 07:55 AM
To fix this issue Your friend's code seems to be generating a 2D array of the distances between each pair, and taking advantage of the symmetry (distance(x,y) = distance(y,x)). It would be slightly better if it used itertools to generate combinations, and assigned your_formula(coords1, coords2) to separation_matrix[i,j] and separation_matrix[j,i] within the same iteration, rather than having separate iterations for both i,j and j,i. Even better would probably be this package that uses a treebased algorithm: https://docs.scipy.org/doc/scipy0.14.0/reference/generated/scipy.spatial.KDTree.html . It seems to be focused on rectilinear coordinates, but that should be addressable in linear time.

Find closest value pairs and calculate mean in Python
By : user2457934
Date : March 29 2020, 07:55 AM
seems to work fine Assuming that you require an additional column D having the mean of the value pair which has the least difference among the three possible pairs: (colA, colB), (colB, colC) and (colC, colA), following code should work: Updated: code :
def meanFunc(row):
nonNanValues = [x for x in list(row) if str(x) != 'nan']
numOfNonNaN = len(nonNanValues)
if(numOfNonNaN == 0): return 0
if(numOfNonNaN == 1): return nonNanValues[0]
if(numOfNonNaN == 2): return np.mean(nonNanValues)
if(numOfNonNaN == 3):
minDiffPairIndex = np.argmin( [abs(row['A']row['B']), abs(row['B']row['C']), abs(row['C']row['A']) ])
meanDict = {0: np.mean([row['A'], row['B']]), 1: np.mean([row['B'], row['C']]), 2: np.mean([row['C'], row['A']])}
return meanDict[minDiffPairIndex]
df['D'] = df.apply(meanFunc, axis=1)
def meanFunc(row):
minDiffPairIndex = np.argmin( [abs(row['A']row['B']), abs(row['B']row['C']), abs(row['C']row['A']) ])
meanDict = {0: np.mean([row['A'], row['B']]), 1: np.mean([row['B'], row['C']]), 2: np.mean([row['C'], row['A']])}
return meanDict[minDiffPairIndex]
df['D'] = df.apply(meanFunc, axis=1)

