From 7b17d2e857955f27b1cc8516e9f601239b6b5f2d Mon Sep 17 00:00:00 2001 From: schillic Date: Sun, 29 Dec 2024 22:45:11 +0100 Subject: [PATCH] merge API's 'distance' with existing methods --- docs/src/lib/sets/HalfSpace.md | 2 +- docs/src/lib/sets/Hyperplane.md | 2 +- docs/src/lib/sets/Line.md | 2 +- src/Sets/HalfSpace/distance.jl | 19 ++++--------------- src/Sets/Hyperplane/distance.jl | 19 ++++--------------- src/Sets/Line/distance.jl | 19 +------------------ 6 files changed, 12 insertions(+), 51 deletions(-) diff --git a/docs/src/lib/sets/HalfSpace.md b/docs/src/lib/sets/HalfSpace.md index f09517c110..d92deabc2c 100644 --- a/docs/src/lib/sets/HalfSpace.md +++ b/docs/src/lib/sets/HalfSpace.md @@ -72,7 +72,6 @@ CurrentModule = LazySets.HalfSpaceModule ``` ```@docs rand(::Type{HalfSpace}) -distance(::AbstractVector, ::HalfSpace) ``` ```@meta CurrentModule = LazySets.API @@ -165,6 +164,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 diff --git a/docs/src/lib/sets/Hyperplane.md b/docs/src/lib/sets/Hyperplane.md index b45ebba539..a0794254c1 100644 --- a/docs/src/lib/sets/Hyperplane.md +++ b/docs/src/lib/sets/Hyperplane.md @@ -67,7 +67,6 @@ CurrentModule = LazySets.HyperplaneModule ``` ```@docs rand(::Type{Hyperplane}) -distance(::AbstractVector, ::Hyperplane) ``` ```@meta CurrentModule = LazySets.API @@ -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)) diff --git a/docs/src/lib/sets/Line.md b/docs/src/lib/sets/Line.md index cfd71998b3..040bdb61b3 100644 --- a/docs/src/lib/sets/Line.md +++ b/docs/src/lib/sets/Line.md @@ -48,7 +48,6 @@ CurrentModule = LazySets.LineModule ``` ```@docs rand(::Type{Line}) -distance(::AbstractVector, ::Line; ::Real=2.0) ``` ```@meta CurrentModule = LazySets.API @@ -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)) diff --git a/src/Sets/HalfSpace/distance.jl b/src/Sets/HalfSpace/distance.jl index 6bce85fe98..642d6e1726 100644 --- a/src/Sets/HalfSpace/distance.jl +++ b/src/Sets/HalfSpace/distance.jl @@ -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)) diff --git a/src/Sets/Hyperplane/distance.jl b/src/Sets/Hyperplane/distance.jl index 12682571b0..72c6bb1b1e 100644 --- a/src/Sets/Hyperplane/distance.jl +++ b/src/Sets/Hyperplane/distance.jl @@ -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) diff --git a/src/Sets/Line/distance.jl b/src/Sets/Line/distance.jl index 480a5e8384..a513e46e66 100644 --- a/src/Sets/Line/distance.jl +++ b/src/Sets/Line/distance.jl @@ -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)