Skip to content

Commit

Permalink
add default value of Earth's radius to Haversine (#176)
Browse files Browse the repository at this point in the history
* add default value of Earth's radius to Haversine

I don't think it should be necessary to look up the radius of Earth every time one wants to use the Haversine formula, so I've added the equatorial radius of Earth in meters as a default. I hope most imperial unit users will agree that metric is a reasonable default for scientific libraries. Of course Earth has different radii depending on where it is measured, as it isn't a perfect sphere. Uses sensitive to this should use more specific values. FYI this same default value is used in R. source https://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html

* reinsert value after rebase

* document default in readme

* Make radius a Float32

* update docstring
  • Loading branch information
mkborregaard authored Jan 20, 2021
1 parent e7e8070 commit 23b0023
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Each distance corresponds to a distance type. The type name and the correspondin
| SpanNormDist | `spannorm_dist(x, y)` | `max(x - y) - min(x - y)` |
| BhattacharyyaDist | `bhattacharyya(x, y)` | `-log(sum(sqrt(x .* y) / sqrt(sum(x) * sum(y)))` |
| HellingerDist | `hellinger(x, y) ` | `sqrt(1 - sum(sqrt(x .* y) / sqrt(sum(x) * sum(y))))` |
| Haversine | `haversine(x, y, r)` | [Haversine formula](https://en.wikipedia.org/wiki/Haversine_formula) |
| Haversine | `haversine(x, y, r = 6371000)` | [Haversine formula](https://en.wikipedia.org/wiki/Haversine_formula) |
| SphericalAngle | `spherical_angle(x, y)` | [Haversine formula](https://en.wikipedia.org/wiki/Haversine_formula) |
| Mahalanobis | `mahalanobis(x, y, Q)` | `sqrt((x - y)' * Q * (x - y))` |
| SqMahalanobis | `sqmahalanobis(x, y, Q)` | `(x - y)' * Q * (x - y)` |
Expand Down
7 changes: 5 additions & 2 deletions src/haversine.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
"""
Haversine(radius)
Haversine([radius])
The haversine distance between two locations on a sphere of given `radius`.
Locations are described with longitude and latitude in degrees.
The computed distance has the same units as that of the radius.
The default value is 6371000 meters, which is the mean volumetric
radius of Earth (source https://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html).
"""
struct Haversine{T<:Real} <: Metric
radius::T
end
Haversine() = Haversine(Float32(6371000))

function (dist::Haversine)(x, y)
length(x) == length(y) == 2 || haversine_error(dist)
Expand All @@ -30,7 +33,7 @@ function (dist::Haversine)(x, y)
2 * dist.radius * asin( min(a, one(a)) ) # take care of floating point errors
end

haversine(x, y, radius::Real) = Haversine(radius)(x, y)
haversine(x, y, radius::Real = Float32(6371000)) = Haversine(radius)(x, y)

@noinline haversine_error(dist) = throw(ArgumentError("expected both inputs to have length 2 in $dist distance"))

Expand Down

0 comments on commit 23b0023

Please sign in to comment.