Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] A generalized PeriodicEuclidean (allows specifying the periods as a parallelogon) #237

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
*.jl.*.cov
*.jl.mem
*.ji
.vscode/
benchmark/params.json
Manifest.toml
18 changes: 18 additions & 0 deletions src/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,24 @@ weuclidean(a, b, w) = WeightedEuclidean(w)(a, b)
s3 = min(s2, p - s2)
abs2(s3)
end

function _evaluate(dist::PeriodicEuclidean, a::AbstractVector, b::AbstractVector, p::AbstractMatrix)
ndim = LinearAlgebra.checksquare(p)
@boundscheck if length(a) != length(b)
throw(DimensionMismatch("first array has length $(length(a)) which does not match the length of the second, $(length(b))."))
end
@boundscheck if length(a) != ndim
throw(DimensionMismatch("arrays have length $(length(a)) but basis vectors have length $ndim."))
end
s = p \ (a - b) # decompose to p-basis, a - b = sx x⃗ + sy y⃗ + ...
mindistance = Inf
s = mod.(s, 1) # move to first "quadrant"
@inbounds for k = 0:1<<ndim-1 # 2^{# of dimensions} possible choices
loc = sum(i->view(p,:,i) .* (k>>(i-1) & 1 == 0 ? s[i] : s[i]-1), 1:ndim)
mindistance = min(norm(loc), mindistance)
end
return mindistance
end
eval_end(::PeriodicEuclidean, s) = sqrt(s)
peuclidean(a, b, p) = PeriodicEuclidean(p)(a, b)

Expand Down
8 changes: 8 additions & 0 deletions test/test_dists.jl
Original file line number Diff line number Diff line change
Expand Up @@ -982,3 +982,11 @@ end
end
end
=#

@testset "periodic - non-rectangular" begin
# basis vectors: [10, 10], [0, 10]
p = PeriodicEuclidean([10 0.0; 10.0 10.0]) # each column is a basis vector.
@test p([10.1, 10.2], [-10.0, 0.0]) ≈ sqrt(0.1^2 + 0.2^2)
@test p([9.9, 10.2], [-10.0, 0.0]) ≈ sqrt(0.1^2 + 0.2^2)
@test p([9.9, 9.8], [-10.0, 0.0]) ≈ sqrt(0.1^2 + 0.2^2)
end