/
distance.jl
85 lines (68 loc) · 2.3 KB
/
distance.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
export distance
"""
distance(H1::AbstractHyperrectangle, H2::AbstractHyperrectangle;
[p]::Real=2)
Compute the standard distance between two hyperrectangular sets, defined as
```math
\\inf_{x ∈ H_1, y ∈ H_2} \\{ d(x, y) \\}.
```
### Input
- `H1` -- hyperrectangular set
- `H2` -- hyperrectangular set
- `p` -- (optional; default: `2`) value of the ``p``-norm
### Output
The distance, which is zero if the sets intersect and otherwise the ``p``-norm
of the shortest line segment between any pair of points.
### Notes
See also [`hausdorff_distance`](@ref) for an alternative distance notion.
"""
function distance(H1::AbstractHyperrectangle, H2::AbstractHyperrectangle;
p::Real=2)
n = dim(H1)
@assert n == dim(H2) "incompatible set dimensions $n and $(dim(H2))"
N = promote_type(eltype(H1), eltype(H2))
d = Vector{N}(undef, n)
@inbounds for i in 1:n
# find relative position in dimension i
# (if c1 == c2, the results are equivalent independent of the branch)
if center(H1, i) >= center(H2, i)
lhs = low(H1, i)
rhs = high(H2, i)
else
lhs = low(H2, i)
rhs = high(H1, i)
end
if _leq(lhs, rhs)
d[i] = zero(N)
else
d[i] = rhs - lhs
end
end
return norm(d, p)
end
"""
distance(S::AbstractSingleton, X::LazySet; [p]::Real=2.0)
Compute the distance between the singleton `S` and the set `X` with respect to
the given `p`-norm.
### Input
- `S` -- singleton, i.e., a set with one element
- `X` -- set
- `p` -- (optional, default: `2.0`) the `p`-norm used; `p = 2.0` corresponds to
the usual Euclidean norm
### Output
A scalar representing the distance between the element wrapped by `S` and the
set `X`.
"""
@commutative function distance(S::AbstractSingleton, X::LazySet; p::Real=2.0)
return distance(element(S), X; p=p)
end
# disambiguation
function distance(S1::AbstractSingleton, S2::AbstractSingleton; p::Real=2.0)
return distance(element(S1), element(S2); p=p)
end
function distance(S::AbstractSingleton, H::AbstractHyperrectangle; p::Real=2.0)
return distance(element(S), H; p=p)
end
function distance(H::AbstractHyperrectangle, S::AbstractSingleton; p::Real=2.0)
return distance(element(S), H; p=p)
end