Skip to content

Commit

Permalink
merge API's 'distance' with existing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Jan 21, 2025
1 parent 2b6295c commit 8b2a7b0
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 71 deletions.
2 changes: 1 addition & 1 deletion docs/src/lib/interfaces/AbstractHyperrectangle.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ CurrentModule = LazySets
```
```@docs
volume(::AbstractHyperrectangle)
distance(::AbstractVector, ::AbstractHyperrectangle{N}; ::Real=N(2)) where {N}
```
```@meta
CurrentModule = LazySets.API
Expand Down Expand Up @@ -239,6 +238,7 @@ CurrentModule = LazySets.API
* [`high`](@ref high(::LazySet, ::Int))
* [`low`](@ref low(::LazySet))
* [`low`](@ref low(::LazySet, ::Int))
* [`distance`](@ref distance(::AbstractVector, ::LazySet))
* [`project`](@ref project(::LazySet, ::AbstractVector{Int}))
* [`ρ`](@ref ρ(::AbstractVector, ::LazySet))
* [`cartesian_product`](@ref cartesian_product(::LazySet, ::LazySet))
Expand Down
1 change: 1 addition & 0 deletions docs/src/lib/sets/BallInf.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ Inherited from [`AbstractZonotope`](@ref):

Inherited from [`AbstractHyperrectangle`](@ref):
* [`constraints_list`](@ref constraints_list(::AbstractHyperrectangle))
* [`distance`](@ref distance(::AbstractVector, ::AbstractHyperrectangle))
* [`extrema`](@ref extrema(::AbstractHyperrectangle))
* [`extrema`](@ref extrema(::AbstractHyperrectangle, ::Int))
* [`generators`](@ref generators(::AbstractHyperrectangle))
Expand Down
2 changes: 1 addition & 1 deletion docs/src/lib/sets/HalfSpace.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ rand(::Type{HalfSpace})
remove_redundant_constraints(::AbstractVector{<:HalfSpace})
remove_redundant_constraints!(::AbstractVector{<:HalfSpace})
tosimplehrep(::AbstractVector{<:HalfSpace})
distance(::AbstractVector, ::HalfSpace)
```
```@meta
CurrentModule = LazySets.API
Expand Down Expand Up @@ -170,6 +169,7 @@ Undocumented implementations:
* [`isbounded`](@ref isbounded(::LazySet))
* [`isempty`](@ref isempty(::LazySet))
* [`isoperationtype`](@ref isoperationtype(::Type{LazySet}))
* [`distance`](@ref distance(::AbstractVector, ::LazySet))
* [`permute`](@ref permute(::LazySet, ::AbstractVector{Int}))

```@meta
Expand Down
2 changes: 1 addition & 1 deletion docs/src/lib/sets/Hyperplane.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ CurrentModule = LazySets.HyperplaneModule
```
```@docs
rand(::Type{Hyperplane})
distance(::AbstractVector, ::Hyperplane)
```
```@meta
CurrentModule = LazySets.API
Expand Down Expand Up @@ -134,6 +133,7 @@ Undocumented implementations:
* [`dim`](@ref dim(::LazySet))
* [`isempty`](@ref isempty(::LazySet))
* [`isoperationtype`](@ref isoperationtype(::Type{LazySet}))
* [`distance`](@ref distance(::AbstractVector, ::LazySet))
* [`project`](@ref project(::LazySet, ::AbstractVector{Int}))
* [`isdisjoint`](@ref isdisjoint(::LazySet, ::LazySet))

Expand Down
2 changes: 1 addition & 1 deletion docs/src/lib/sets/Hyperrectangle.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Inherited from [`AbstractHyperrectangle`](@ref):
* [`reflect`](@ref reflect(::AbstractHyperrectangle))
* [`vertices_list`](@ref vertices_list(::AbstractHyperrectangle))
* [`volume`](@ref volume(::AbstractHyperrectangle))
* [`distance`](@ref distance(::AbstractVector, ::AbstractHyperrectangle{N}; ::Real=N(2)) where {N})
* [`distance`](@ref distance(::AbstractVector, ::AbstractHyperrectangle))
* [``](@ref ∈(::AbstractVector, ::AbstractHyperrectangle))
* [`project`](@ref project(::AbstractHyperrectangle, ::AbstractVector{Int}))
* [`split`](@ref split(::AbstractHyperrectangle, ::AbstractVector{Int}))
Expand Down
2 changes: 1 addition & 1 deletion docs/src/lib/sets/Line.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ CurrentModule = LazySets.LineModule
```
```@docs
rand(::Type{Line})
distance(::AbstractVector, ::Line; ::Real=2.0)
```
```@meta
CurrentModule = LazySets.API
Expand Down Expand Up @@ -86,6 +85,7 @@ Undocumented implementations:
* [`isbounded`](@ref isbounded(::LazySet))
* [`isempty`](@ref isempty(::LazySet))
* [`isoperationtype`](@ref isoperationtype(::Type{LazySet}))
* [`distance`](@ref distance(::AbstractVector, ::LazySet))
* [`project`](@ref project(::LazySet, ::AbstractVector{Int}))
* [`ρ`](@ref ρ(::AbstractVector, ::LazySet))
* [`σ`](@ref σ(::AbstractVector, ::LazySet))
Expand Down
20 changes: 2 additions & 18 deletions src/Interfaces/AbstractHyperrectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -591,24 +591,7 @@ function project(H::AbstractHyperrectangle, block::AbstractVector{Int};
return Hyperrectangle(πc, πr; check_bounds=false)
end

"""
distance(x::AbstractVector, H::AbstractHyperrectangle{N};
[p]::Real=N(2)) where {N}
Compute the distance between a point `x` and a hyperrectangular set `H` with
respect to the given `p`-norm.
### Input
- `x` -- point/vector
- `H` -- hyperrectangular set
### Output
A scalar representing the distance between point `x` and hyperrectangle `H`.
"""
@commutative function distance(x::AbstractVector, H::AbstractHyperrectangle{N};
p::Real=N(2)) where {N}
@commutative function distance(x::AbstractVector, H::AbstractHyperrectangle; p::Real=2)
@assert length(x) == dim(H) "a vector of length $(length(x)) is " *
"incompatible with a set of dimension $(dim(H))"

Expand All @@ -631,6 +614,7 @@ A scalar representing the distance between point `x` and hyperrectangle `H`.

if !outside
# point is inside
N = promote_type(eltype(x), eltype(H))
return zero(N)
end

Expand Down
19 changes: 4 additions & 15 deletions src/Sets/HalfSpace/distance.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
"""
distance(x::AbstractVector, H::HalfSpace)
@commutative function distance(x::AbstractVector, H::HalfSpace; p::Real=2)
if p != 2
throw(ArgumentError("`distance` is only implemented for Euclidean norm"))
end

Compute the distance between point `x` and half-space `H` with respect to the
Euclidean norm.
### Input
- `x` -- vector
- `H` -- half-space
### Output
A scalar representing the distance between point `x` and half-space `H`.
"""
@commutative function distance(x::AbstractVector, H::HalfSpace)
N = promote_type(eltype(x), eltype(H))
a, b = _normalize_halfspace(H, N(2))
return max(dot(x, a) - b, zero(N))
Expand Down
19 changes: 4 additions & 15 deletions src/Sets/Hyperplane/distance.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
"""
distance(x::AbstractVector, H::Hyperplane)
@commutative function distance(x::AbstractVector, H::Hyperplane; p::Real=2)
if p != 2
throw(ArgumentError("`distance` is only implemented for Euclidean norm"))
end

Compute the distance between point `x` and hyperplane `H` with respect to the
Euclidean norm.
### Input
- `x` -- vector
- `H` -- hyperplane
### Output
A scalar representing the distance between point `x` and hyperplane `H`.
"""
@commutative function distance(x::AbstractVector, H::Hyperplane)
N = promote_type(eltype(x), eltype(H))
a, b = _normalize_halfspace(H, N(2))
return abs(dot(x, a) - b)
Expand Down
19 changes: 1 addition & 18 deletions src/Sets/Line/distance.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
"""
distance(x::AbstractVector, L::Line; [p]::Real=2.0)
Compute the distance between point `x` and the line with respect to the given
`p`-norm.
### Input
- `x` -- point/vector
- `L` -- line
- `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 `x` and the line `L`.
"""
@commutative function distance(x::AbstractVector, L::Line; p::Real=2.0)
@commutative function distance(x::AbstractVector, L::Line; p::Real=2)
d = L.d # direction of the line
t = dot(x - L.p, d) / dot(d, d)
return distance(x, L.p + t * d; p=p)
Expand Down

0 comments on commit 8b2a7b0

Please sign in to comment.