apbs icon indicating copy to clipboard operation
apbs copied to clipboard

Investigate using the numpy.dot function to calculate the Euclidean distance.

Open intendo opened this issue 4 years ago • 5 comments

Describe the bug Numpy may be a better way to implement the Euclidean distance .

intendo avatar Aug 04 '20 07:08 intendo

numpy.linalg.norm would work well, though Nathan mentioned he wanted to avoid using the square root for as long as possible.

ashermancinelli avatar Aug 04 '20 14:08 ashermancinelli

@sobolevnrm, what would you recommend for replacing return np.sum((self.position._data - other) ** 2) to calculate the euclidean distance in https://github.com/Electrostatics/apbs/blob/master/apbs/chemistry/atom.py?

You had mentioned using https://numpy.org/doc/stable/reference/generated/numpy.dot.html but the Googles seems to recommend numpy.linalg.norm like the answer on https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy.

Can you give us some guidance?

intendo avatar Aug 04 '20 14:08 intendo

I recommended the dot function because it avoids square roots. In general, when we can compare squared distances to squared distances, we will save a lot of computational effort. That's why I wanted to avoid the norm for now.

sobolevnrm avatar Aug 05 '20 01:08 sobolevnrm

@sobolevnrm is this what you were thinking (with optionally not performing the square root)?

import numpy as np 
  
# intializing points in numpy arrays 
point1 = np.array((1, 2, 3)) 
point2 = np.array((1, 1, 1)) 
  
# subtracting vector 
temp = point1 - point2 
  
# doing dot product for finding sum of the squares 
sum_sq = np.dot(temp.T, temp) 
print(sum_sq)
  
# Doing squareroot and printing Euclidean distance 
print(np.sqrt(sum_sq)) 

intendo avatar Sep 29 '20 18:09 intendo

Yes, more or less. I was actually thinking of mapping atom coordinates to (N, 3)-size arrays, where N is the number of atoms, so we could quickly apply operations like dot, etc. to those arrays .

sobolevnrm avatar Sep 29 '20 19:09 sobolevnrm