apbs
apbs copied to clipboard
Investigate using the numpy.dot function to calculate the Euclidean distance.
Describe the bug Numpy may be a better way to implement the Euclidean distance .
numpy.linalg.norm
would work well, though Nathan mentioned he wanted to avoid using the square root for as long as possible.
@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?
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 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))
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 .