Created
October 29, 2012 13:28
-
-
Save martinogden/3973545 to your computer and use it in GitHub Desktop.
n-dimensional Euclidean Distance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def euclidean_distance(p, q): | |
""" Calculate distance between 2 points in n-dimensional space | |
>>> euclidean_distance((3, 0), (0, 4)) | |
5.0 | |
""" | |
n = len(p) # dimensions | |
return sum([(p[i] - q[i]) ** 2 for i in xrange(n)]) ** 0.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment