1

I am still new to python and have practiced python as automation tool to get used to.

Now I want to try mathematical calculation in python.

I have tried to calculate distance of two cartesian coordinate I extracted from a hand practice data:

in temp2:

-0.329637489 3.481200000 1.740200000

2.389059814 1.000230000 8.653210000

N.B the numbers are in a text file, line 1 and 2.

My initial thought was append each number as a variable. Then, calculate them. However, I am struggling assign each number as a variable.

or

Is there are more effective way to calculate the distance of coordination?

If there is I would like to know both ways.

THANK YOU SO MUCH IN ADVANCE!

Community
  • 1
  • 1
DGKang
  • 163
  • 12
  • 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
  • 5,942
  • 3
  • 14
  • 35
  • 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_
  • 349
  • 3
  • 10