Created
June 17, 2022 03:11
-
-
Save fasiha/bbc168b1de9c9708f3157252a696dcb1 to your computer and use it in GitHub Desktop.
Haversine and pseudo-Haversine (up to a constant) formula
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
import numpy as np | |
from numpy import sqrt, sin, cos | |
asin = np.arcsin | |
def simp(rlat1, rlon1, rlat2, rlon2): | |
dLat = rlat2 - rlat2 | |
dLon = rlon2 - rlon1 | |
return ((sin(dLat / 2)**2 + sin(dLon / 2)**2 * cos(rlat1) * cos(rlat2))) | |
def full(rlat1, rlon1, rlat2, rlon2): | |
return asin(sqrt(simp(rlat1, rlon1, rlat2, rlon2))) | |
n = 100 | |
r = np.vstack([ | |
np.random.rand(n) * np.pi - np.pi / 2, | |
np.random.rand(n) * np.pi * 2, | |
np.random.rand(n) * np.pi - np.pi / 2, | |
np.random.rand(n) * np.pi * 2 | |
]).T | |
ys = [simp(*v) for v in r] | |
yf = [full(*v) for v in r] | |
idxs = np.argsort(ys) | |
idxf = np.argsort(yf) | |
assert all(idxs == idxf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment