1

I have cartesian coordinates of two points in a txt file.

-0.329637489    3.481200000     1.740200000
2.389059814     1.000230000     8.653210000

I have read the file

with open('coord.txt', 'r') as f:
    lines = f.readlines()
    
lines_1 = [x.strip() for x in lines]
lines_2 = [x.split() for x in lines_1]
lines_2 = np.array(lines_2).astpye(float)
>> array([[-0.32963749,  3.4812    ,  1.7402    ],
       [ 2.38905981,  1.00023   ,  8.65321   ]])

I can select each entity of the matrix to calculate the distance between the points. But I want to learn what API/library would give the most effective result and how/why.

If there is a computational cost difference, the difference would come from the language they used for the API/library?

DGKang
  • 172
  • 1
  • 13
  • 2
    This question is too broad as it includes reading a file, process and convert strings and doing calculations. Show your code and ask for a specific issue. – Michael Butscher Dec 18 '19 at 16:42
  • 1
    Does this answer your question? [How can the Euclidean distance be calculated with NumPy?](https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy) – rpanai Dec 18 '19 at 16:45

2 Answers2

1

You can load the data directly in a numpy array using np.loadtxt:

a= np.loadtxt('../filename.txt')
a
array([[-0.32963749,  3.4812    ,  1.7402    ],
       [ 2.38905981,  1.00023   ,  8.65321   ]])

Then perform operations to compute distance:

d = np.sqrt(np.sum((a[0]-a[1])**2))

Let's go through this step by step:

  • a[0] is [-0.32963749, 3.4812, 1.7402], namely the 1st row
  • a[1] is [ 2.38905981, 1.00023, 8.65321], the 2nd row
  • a[0] - a[1] is the elementwise difference: [-2.7186973, 2.48097, -6.91301]
  • (a[0]-a[1])**2 is the elementwise difference squared [7.39131503, 6.15521214, 47.78970726]
  • Finally np.sqrt(np.sum()) sums all the components together, and then take square root, hence d is 7.831745298867902

That said, numpy already has a build in function to compute this distance once you load the array:

d = np.linalg.norm(a[0]-a[1])
FBruzzesi
  • 6,385
  • 3
  • 15
  • 37
  • Thank you so much! So, if I want to calculate any number through python do I have to use numpy array(?)? – DGKang Dec 18 '19 at 16:52
  • 1
    numpy has a lot of build in function to compute a lot of things, typically in a vectorized way, which leads to faster performance than vanilla python. Therefore I suggest you look into it :) – FBruzzesi Dec 18 '19 at 16:55
1

Maybe you could try this:

import math

def calcDist(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist       

distance = calcDist(x1, y1, x2, y2)
print(distance)

The function takes the 4 coordinates, and returns the distance between them using math.sqrt. Let me know if you have any other questions about how to possibly read in the variables. I would recommend reading in the lines, and using line.split() to get each variable.

def_init_
  • 337
  • 3
  • 10