Skip to content

Commit

Permalink
better distance for Interval
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Jun 28, 2024
1 parent 9e720b4 commit 36c3dba
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/src/lib/sets/Interval.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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:

Expand Down
5 changes: 3 additions & 2 deletions src/Sets/Interval/IntervalModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions src/Sets/Interval/distance.jl
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions test/Sets/Interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 36c3dba

Please sign in to comment.