diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index 9fd329c8d3..51d4009e02 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -75,6 +75,7 @@ Undocumented implementations: * [`radius`](@ref radius(::LazySet, ::Real)) * [`volume`](@ref volume(::LazySet)) * [`convex_hull`](@ref convex_hull(::LazySet, ::LazySet)) +* [`distance`](@ref distance(::LazySet, ::LazySet)) * [`≈`](@ref ≈(::LazySet, ::LazySet)) * [`isequivalent`](@ref isequivalent(::LazySet, ::LazySet)) * [`⊂`](@ref ⊂(::LazySet, ::LazySet)) @@ -127,7 +128,6 @@ Inherited from [`AbstractHyperrectangle`](@ref): * [`genmat`](@ref genmat(::AbstractHyperrectangle)) * [`isconvextype`](@ref isconvextype(::Type{<:AbstractHyperrectangle})) * [`cartesian_product`](@ref cartesian_product(::AbstractHyperrectangle, ::AbstractHyperrectangle)) -* [`distance`](@ref distance(::AbstractHyperrectangle, ::AbstractHyperrectangle)) Some additional functionality is available for `IntervalArithmetic.Interval`s: diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index 0fe1bcc6de..1ebc741a40 100644 --- a/src/Sets/Interval/IntervalModule.jl +++ b/src/Sets/Interval/IntervalModule.jl @@ -16,8 +16,8 @@ import IntervalArithmetic as IA convex_hull, diameter, dim, exponential_map, extrema, high, ∈, isoperationtype, linear_map, low, norm, permute, project, rand, rectify, reflect, scale, ρ, σ, translate, vertices_list, volume, - difference, intersection, ≈, isdisjoint, isequivalent, ⊂, ⊆, - minkowski_difference, minkowski_sum + difference, distance, intersection, ≈, isdisjoint, isequivalent, + ⊂, ⊆, minkowski_difference, minkowski_sum @reexport import ..LazySets: chebyshev_center_radius, isflat, ngens, plot_recipe, radius_hyperrectangle, split import Base: convert, -, *, min, max @@ -62,6 +62,7 @@ include("vertices_list.jl") include("volume.jl") include("difference.jl") +include("distance.jl") include("intersection.jl") include("isapprox.jl") include("isdisjoint.jl") diff --git a/src/Sets/Interval/distance.jl b/src/Sets/Interval/distance.jl new file mode 100644 index 0000000000..bcd3392116 --- /dev/null +++ b/src/Sets/Interval/distance.jl @@ -0,0 +1,7 @@ +function distance(X::Interval, Y::Interval; p::Real=2) + d = max(min(X) - max(Y), min(Y) - max(X)) + if d < 0 + return zero(d) + end + return d +end diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index fe1183a7ba..224acd85b6 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -352,4 +352,9 @@ for N in [Float64, Float32, Rational{Int}] # isequivalent @test isequivalent(I1, I1) && !isequivalent(I1, I2) + + # distance + @test distance(I1, I2) == distance(I2, I1) == N(1) + I3 = Interval(N(1//2), N(2)) + @test distance(I1, I3) == distance(I2, I3) == distance(I1, I1) == N(0) end