Last active
September 13, 2016 06:13
-
-
Save hcarvalhoalves/b099dd5872e9a005588edbcd1b3010ef to your computer and use it in GitHub Desktop.
This file contains 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
from scipy.spatial.distance import euclidean | |
import numpy as np | |
from __future__ import division | |
def periodic_distance(a, b, period=12): | |
""" | |
Euclidean distance w/ periodic boundaries. | |
>>> A = np.array([1, 12, 1]) | |
>>> B = np.array([12, 1, 1]) | |
>>> periodic_distance(A, B) | |
array([ 0.51763809, 0.51763809, 0. ]) | |
""" | |
A = 2 * np.math.pi * (a / period) | |
B = 2 * np.math.pi * (b / period) | |
rA = np.array((np.sin(A), np.cos(A))) | |
rB = np.array((np.sin(B), np.cos(B))) | |
return np.linalg.norm(rA - rB, axis=0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment