Skip to content

Instantly share code, notes, and snippets.

@hcarvalhoalves
Last active September 13, 2016 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hcarvalhoalves/b099dd5872e9a005588edbcd1b3010ef to your computer and use it in GitHub Desktop.
Save hcarvalhoalves/b099dd5872e9a005588edbcd1b3010ef to your computer and use it in GitHub Desktop.
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