Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of distances from a point to all other points

I have two lists

available_points = [[2,3], [4,5], [1,2], [6,8], [5,9], [51,35]]

and

solution = [[3,5], [2,1]]

I'm trying to pop a point in available_points and append it to solution for which the sum of euclidean distances from that point, to all points in the solution is the greatest.

So, I would get this

solution = [[3,5], [2,1], [51,35]]


I was able to select the initial 2 furthest points like this, but not sure how to proceed.

import numpy as np
from scipy.spatial.distance import pdist, squareform

available_points = np.array([[2,3], [4,5], [1,2], [6,8], [5,9], [51,35]])

D = squareform(pdist(available_points)
I_row, I_col = np.unravel_index(np.argmax(D), D.shape)
solution = available_points[[I_row, I_col]]

which gives me

solution = array([[1, 2], [51, 35]])

like image 740
jfran Avatar asked Jan 12 '18 04:01

jfran


People also ask

What do you call the set of all points such that the sum of its distances from the two fixed points is constant?

An ellipse is "the set of all points in a plane such that the sum of the distances from two fixed points (foci) is constant".

What is distance sum?

The distance between the two points is defined as follows: d i s t ( a , b ) = s i n ( a + b ) × c o s ( a − b ) Your task is to determine the sum of the distances between all the points, where the first distance is greater than the second. Input format. First line: denoting the number of points on a line.

What do you call a distance between two points?

The distance between two points is called the length of the line segment. Segments having the same length are called congruent segments. We can calculate the distance between two points by drawing a line using a ruler.

What is the formula to find the distance of a point from the origin?

√(0−x)2+(0−y)2=√x2+y2. Hence the distance between the origin and the point P is √x2+y2.


1 Answers

You can use cdist -

In [1]: from scipy.spatial.distance import cdist

In [2]: max_pt=available_points[cdist(available_points, solution).sum(1).argmax()]

In [3]: np.vstack((solution, max_pt))
Out[3]: 
array([[ 3,  5],
       [ 2,  1],
       [51, 35]])
like image 167
Divakar Avatar answered Sep 28 '22 19:09

Divakar