Distances.jl icon indicating copy to clipboard operation
Distances.jl copied to clipboard

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

Open GiggleLiu opened this issue 3 years ago • 2 comments

In physics, we often use lattices, the periodic boundary condition is non-rectangular in most cases. It can be an arbitrary parallelogon. It would be nice if the periods can be a matrix, it will make NearestNeighbors.jl more useful to condensed matter physicists.

I am not very familiar with the design pattern of this package, if someone feel the added code is not qualified according to your standard, please feel free to modify this PR. Thanks!

Example

julia> using Distances

julia> p = PeriodicEuclidean([10 0.0; 10.0 10.0])
PeriodicEuclidean{Matrix{Float64}}([10.0 0.0; 10.0 10.0])

julia> evaluate(p, [0.1, 0.2], [10.1, 10.3])
0.1000000000000012

UPDATE: somehow, the NearestNeighbors.jl does not work properly with this update. Still checking why it is so. UPDATE 2: they redefined some functions in their own package... this is so wield. https://github.com/KristofferC/NearestNeighbors.jl/blob/1de39f57fcf5e51ce51592e382f530e514a59952/src/evaluation.jl#L9 https://github.com/KristofferC/NearestNeighbors.jl/blob/1de39f57fcf5e51ce51592e382f530e514a59952/src/ball_tree.jl#L64 @KristofferC UPDATE3: This issue can be fixed by this monkey patch (https://github.com/KristofferC/NearestNeighbors.jl/pull/130). I tested it in my lattice package, it is a correct fix.

GiggleLiu avatar Dec 20 '21 00:12 GiggleLiu

I notice that the distance computed in this way (shifting the parallelogon for 2^ndim times towards all directions) does not cover all cases (actually I found a counter-example). I need to do more research and will comment back later.

Note: So far, I haven't found any information useful. Maybe finding the shortest distance in non-rectangular periodic space is an open problem?

https://math.stackexchange.com/questions/1364594/distance-in-a-periodic-system

GiggleLiu avatar Jan 26 '22 20:01 GiggleLiu

Note: The problem of finding the distance with periodic boundary condition is called the closest vector problem, it is known as NP-hard. However, for low-dimensional system, solving it regorously might be possible, check this paper:

https://arxiv.org/abs/1504.01995

GiggleLiu avatar Jan 27 '22 01:01 GiggleLiu