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 1 commit
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
14 changes: 14 additions & 0 deletions src/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,20 @@ weuclidean(a, b, w) = WeightedEuclidean(w)(a, b)
s3 = min(s2, p - s2)
abs2(s3)
end

function (pe::PeriodicEuclidean{<:AbstractMatrix})(a::AbstractVector, b::AbstractVector)
p = parameters(pe)
ndim = length(a)
@assert size(p,1) == size(p, 2) == length(b) == ndim # check dimension
GiggleLiu marked this conversation as resolved.
Show resolved Hide resolved
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