From 0864bf1ebe37c9a0d1c2a880ae9fc7612144f708 Mon Sep 17 00:00:00 2001 From: schillic Date: Mon, 3 Jun 2024 23:03:50 +0200 Subject: [PATCH 01/17] sort/add docs for Interval --- docs/src/lib/sets/Interval.md | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index 7686577da4..6fe7bdb8e0 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -31,8 +31,6 @@ high(::Interval) isflat(::Interval) linear_map(::AbstractMatrix, ::Interval) low(::Interval) -min(::Interval) -max(::Interval) ngens(::Interval) radius_hyperrectangle(::Interval) radius_hyperrectangle(::Interval{N}, ::Int) where {N} @@ -52,6 +50,8 @@ isdisjoint(::Interval, ::Interval, ::Bool=false) minkowski_difference(::Interval, ::Interval) minkowski_sum(::Interval, ::Interval) plot_recipe(::Interval{N}, ::Any=zero(N)) where {N} +min(::Interval) +max(::Interval) -(::Interval, ::Interval) *(::Interval, ::Interval) ``` @@ -61,6 +61,7 @@ CurrentModule = LazySets.API ``` Undocumented implementations: +* [`isoperationtype`](@ref isoperationtype(::Type{<:LazySet})) * [`radius`](@ref radius(::LazySet, ::Real)) ```@meta @@ -68,25 +69,49 @@ CurrentModule = LazySets ``` Inherited from [`LazySet`](@ref): +* [`complement`](@ref complement(::LazySet)) +* [`concretize`](@ref concretize(::LazySet)) +* [`constraints`](@ref constraints(::LazySet)) +* [`eltype`](@ref eltype(::Type{<:LazySet})) +* [`eltype`](@ref eltype(::LazySet)) +* [`isoperation`](@ref isoperation(::LazySet)) * [`singleton_list`](@ref singleton_list(::LazySet)) +* [`vertices`](@ref vertices(::LazySet)) +* [`affine_map`](@ref affine_map(::AbstractMatrix, ::LazySet, ::AbstractVector)) +* [`exponential_map`](@ref exponential_map(::AbstractMatrix, ::LazySet)) +* [`is_interior_point`](@ref is_interior_point(::AbstractVector, ::LazySet)) +* [`sample`](@ref sample(::LazySet, ::Int)) +* [`==`](@ref ==(::LazySet, ::LazySet)) +* [`isequivalent`](@ref isequivalent(::LazySet, ::LazySet)) +* [`⊂`](@ref ⊂(::LazySet, ::LazySet)) + +Inherited from [`AbstractPolyhedron`](@ref): +* [`is_polyhedral`](@ref is_polyhedral(::AbstractPolyhedron)) Inherited from [`AbstractPolytope`](@ref): * [`isbounded`](@ref isbounded(::AbstractPolytope)) -* [`isuniversal`](@ref isuniversal(::AbstractPolytope{N}, ::Bool=false) where {N}) +* [`isboundedtype`](@ref isboundedtype(::Type{<:AbstractPolytope})) Inherited from [`AbstractCentrallySymmetricPolytope`](@ref): * [`isempty`](@ref isempty(::AbstractCentrallySymmetricPolytope)) +* [`isuniversal`](@ref isuniversal(::AbstractCentrallySymmetricPolytope{N}, ::Bool=false) where {N}) Inherited from [`AbstractZonotope`](@ref): * [`order`](@ref order(::AbstractZonotope)) * [`togrep`](@ref togrep(::AbstractZonotope)) Inherited from [`AbstractHyperrectangle`](@ref): +* [`extrema`](@ref extrema(::AbstractHyperrectangle)) +* [`extrema`](@ref extrema(::AbstractHyperrectangle, ::Int)) * [`generators`](@ref generators(::AbstractHyperrectangle)) * [`genmat`](@ref genmat(::AbstractHyperrectangle)) * [`high`](@ref high(::AbstractHyperrectangle, ::Int)) +* [`isconvextype`](@ref isconvextype(::Type{<:AbstractHyperrectangle})) * [`low`](@ref low(::AbstractHyperrectangle, ::Int)) * [`norm`](@ref norm(::AbstractHyperrectangle, ::Real)) +* [`volume`](@ref volume(::AbstractHyperrectangle)) +* [`cartesian_product`](@ref cartesian_product(::AbstractHyperrectangle, ::AbstractHyperrectangle)) +* [`distance`](@ref distance(::AbstractHyperrectangle, ::AbstractHyperrectangle)) Some additional functionality is available for `IntervalArithmetic.Interval`s: From 0a2efc1a76952cad0528528db3eaf44a33193cf3 Mon Sep 17 00:00:00 2001 From: schillic Date: Sat, 1 Jun 2024 12:22:29 +0200 Subject: [PATCH 02/17] add permute for Interval --- docs/src/lib/sets/Interval.md | 1 + src/Sets/Interval/IntervalModule.jl | 5 +++-- src/Sets/Interval/permute.jl | 4 ++++ test/Sets/Interval.jl | 6 ++++++ 4 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 src/Sets/Interval/permute.jl diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index 6fe7bdb8e0..f85b459f9e 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -62,6 +62,7 @@ CurrentModule = LazySets.API Undocumented implementations: * [`isoperationtype`](@ref isoperationtype(::Type{<:LazySet})) +* [`permute`](@ref permute(::LazySet, ::AbstractVector{Int})) * [`radius`](@ref radius(::LazySet, ::Real)) ```@meta diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index b32ca3be44..826af2325d 100644 --- a/src/Sets/Interval/IntervalModule.jl +++ b/src/Sets/Interval/IntervalModule.jl @@ -13,8 +13,8 @@ using ReachabilityBase.Distribution: reseed! import IntervalArithmetic as IA @reexport import ..API: an_element, center, constraints_list, diameter, dim, high, ∈, - isoperationtype, linear_map, low, rand, rectify, reflect, scale, ρ, σ, - translate, vertices_list, + isoperationtype, linear_map, low, permute, rand, rectify, reflect, + scale, ρ, σ, translate, vertices_list, difference, intersection, isdisjoint, ⊆, minkowski_difference, minkowski_sum @reexport import ..LazySets: chebyshev_center_radius, isflat, ngens, plot_recipe, @@ -39,6 +39,7 @@ include("linear_map.jl") include("low.jl") include("isoperationtype.jl") include("ngens.jl") +include("permute.jl") include("radius.jl") include("radius_hyperrectangle.jl") include("rand.jl") diff --git a/src/Sets/Interval/permute.jl b/src/Sets/Interval/permute.jl new file mode 100644 index 0000000000..b6b989562f --- /dev/null +++ b/src/Sets/Interval/permute.jl @@ -0,0 +1,4 @@ +function permute(X::Interval, p::AbstractVector{Int}) + @assert length(p) == 1 && p[1] == 1 "invalid permutation vector $p" + return X +end diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index 3db9b7a9a4..d079a37ab5 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -288,4 +288,10 @@ for N in [Float64, Float32, Rational{Int}] end res, w = ⊆(I13, I13, true) @test ⊆(I13, I13) && res && w == N[] + + # permute + @test permute(x, [1]) == x + @test_throws AssertionError permute(x, Int[]) + @test_throws AssertionError permute(x, Int[2]) + @test_throws AssertionError permute(x, Int[1, 1]) end From e5e36127716c847f5f5c0a204aa8cff2849b1d0c Mon Sep 17 00:00:00 2001 From: schillic Date: Sat, 1 Jun 2024 13:00:05 +0200 Subject: [PATCH 03/17] add project for Interval --- docs/src/lib/sets/Interval.md | 1 + src/Sets/Interval/IntervalModule.jl | 5 +++-- src/Sets/Interval/project.jl | 4 ++++ test/Sets/Interval.jl | 6 ++++++ 4 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 src/Sets/Interval/project.jl diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index f85b459f9e..7c3510e668 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -63,6 +63,7 @@ CurrentModule = LazySets.API Undocumented implementations: * [`isoperationtype`](@ref isoperationtype(::Type{<:LazySet})) * [`permute`](@ref permute(::LazySet, ::AbstractVector{Int})) +* [`project`](@ref project(::LazySet, ::AbstractVector{Int})) * [`radius`](@ref radius(::LazySet, ::Real)) ```@meta diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index 826af2325d..dcb5d53209 100644 --- a/src/Sets/Interval/IntervalModule.jl +++ b/src/Sets/Interval/IntervalModule.jl @@ -13,8 +13,8 @@ using ReachabilityBase.Distribution: reseed! import IntervalArithmetic as IA @reexport import ..API: an_element, center, constraints_list, diameter, dim, high, ∈, - isoperationtype, linear_map, low, permute, rand, rectify, reflect, - scale, ρ, σ, translate, vertices_list, + isoperationtype, linear_map, low, permute, project, rand, rectify, + reflect, scale, ρ, σ, translate, vertices_list, difference, intersection, isdisjoint, ⊆, minkowski_difference, minkowski_sum @reexport import ..LazySets: chebyshev_center_radius, isflat, ngens, plot_recipe, @@ -40,6 +40,7 @@ include("low.jl") include("isoperationtype.jl") include("ngens.jl") include("permute.jl") +include("project.jl") include("radius.jl") include("radius_hyperrectangle.jl") include("rand.jl") diff --git a/src/Sets/Interval/project.jl b/src/Sets/Interval/project.jl new file mode 100644 index 0000000000..5073c518f7 --- /dev/null +++ b/src/Sets/Interval/project.jl @@ -0,0 +1,4 @@ +function project(X::Interval, block::AbstractVector{Int}; kwargs...) + @assert length(block) == 1 && block[1] == 1 "invalid permutation vector $block" + return X +end diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index d079a37ab5..749bf3715a 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -294,4 +294,10 @@ for N in [Float64, Float32, Rational{Int}] @test_throws AssertionError permute(x, Int[]) @test_throws AssertionError permute(x, Int[2]) @test_throws AssertionError permute(x, Int[1, 1]) + + # project + @test project(x, [1]) == x + @test_throws AssertionError project(x, Int[]) + @test_throws AssertionError project(x, Int[2]) + @test_throws AssertionError project(x, Int[1, 1]) end From 60fb487dcc19a79dcf5c0e1233c638ef3dae7813 Mon Sep 17 00:00:00 2001 From: schillic Date: Sat, 1 Jun 2024 19:58:22 +0200 Subject: [PATCH 04/17] add isapprox for Interval --- docs/src/lib/sets/Interval.md | 1 + src/Sets/Interval/IntervalModule.jl | 3 ++- src/Sets/Interval/isapprox.jl | 3 +++ test/Sets/Interval.jl | 4 ++++ 4 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 src/Sets/Interval/isapprox.jl diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index 7c3510e668..82b393628c 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -65,6 +65,7 @@ Undocumented implementations: * [`permute`](@ref permute(::LazySet, ::AbstractVector{Int})) * [`project`](@ref project(::LazySet, ::AbstractVector{Int})) * [`radius`](@ref radius(::LazySet, ::Real)) +* [`≈`](@ref ≈(::LazySet, ::LazySet)) ```@meta CurrentModule = LazySets diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index dcb5d53209..e9c886dc48 100644 --- a/src/Sets/Interval/IntervalModule.jl +++ b/src/Sets/Interval/IntervalModule.jl @@ -15,7 +15,7 @@ import IntervalArithmetic as IA @reexport import ..API: an_element, center, constraints_list, diameter, dim, high, ∈, isoperationtype, linear_map, low, permute, project, rand, rectify, reflect, scale, ρ, σ, translate, vertices_list, - difference, intersection, isdisjoint, ⊆, minkowski_difference, + difference, intersection, ≈, isdisjoint, ⊆, minkowski_difference, minkowski_sum @reexport import ..LazySets: chebyshev_center_radius, isflat, ngens, plot_recipe, radius_hyperrectangle, split @@ -55,6 +55,7 @@ include("vertices_list.jl") include("difference.jl") include("intersection.jl") +include("isapprox.jl") include("isdisjoint.jl") include("issubset.jl") include("minkowski_difference.jl") diff --git a/src/Sets/Interval/isapprox.jl b/src/Sets/Interval/isapprox.jl new file mode 100644 index 0000000000..199eaaf835 --- /dev/null +++ b/src/Sets/Interval/isapprox.jl @@ -0,0 +1,3 @@ +function ≈(I1::Interval, I2::Interval) + return _isapprox(min(I1), min(I2)) && _isapprox(max(I1), max(I2)) +end diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index 749bf3715a..532bec4737 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -300,4 +300,8 @@ for N in [Float64, Float32, Rational{Int}] @test_throws AssertionError project(x, Int[]) @test_throws AssertionError project(x, Int[2]) @test_throws AssertionError project(x, Int[1, 1]) + + # isapprox + @test x ≈ x ≈ translate(x, [1e-8]) + @test !(x ≈ translate(x, [1e-4])) end From 73134cbcf08316ca002f216cbbb413646b51b909 Mon Sep 17 00:00:00 2001 From: schillic Date: Sat, 1 Jun 2024 20:15:24 +0200 Subject: [PATCH 05/17] add linear_combination for convex sets --- docs/src/lib/sets/Interval.md | 12 ++++++++++++ src/API/Binary/linear_combination.jl | 2 ++ src/ConcreteOperations/linear_combination.jl | 4 ++++ test/Sets/Interval.jl | 5 +++++ 4 files changed, 23 insertions(+) diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index 82b393628c..0ae7bd8e02 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -91,6 +91,18 @@ Inherited from [`LazySet`](@ref): Inherited from [`AbstractPolyhedron`](@ref): * [`is_polyhedral`](@ref is_polyhedral(::AbstractPolyhedron)) +Inherited from [`ConvexSet`](@ref): + +```@meta +CurrentModule = LazySets.API +``` + +* [`linear_combination`](@ref linear_combination(::LazySet, ::LazySet)) + +```@meta +CurrentModule = LazySets +``` + Inherited from [`AbstractPolytope`](@ref): * [`isbounded`](@ref isbounded(::AbstractPolytope)) * [`isboundedtype`](@ref isboundedtype(::Type{<:AbstractPolytope})) diff --git a/src/API/Binary/linear_combination.jl b/src/API/Binary/linear_combination.jl index 7781f7a7ac..5aa683d7b1 100644 --- a/src/API/Binary/linear_combination.jl +++ b/src/API/Binary/linear_combination.jl @@ -19,5 +19,7 @@ The linear combination of two sets ``X`` and ``Y`` is defined as ```math \\left\\{\\frac{1}{2}(1+λ)x + \\frac{1}{2}(1-λ)y \\mid x ∈ X, y ∈ Y, λ ∈ [-1, 1]\\right\\}. ``` + +If ``X`` and ``Y`` are convex, their linear combination is identical with their convex hull. """ function linear_combination(::LazySet, ::LazySet) end diff --git a/src/ConcreteOperations/linear_combination.jl b/src/ConcreteOperations/linear_combination.jl index 762cb581cf..e22edc91e1 100644 --- a/src/ConcreteOperations/linear_combination.jl +++ b/src/ConcreteOperations/linear_combination.jl @@ -39,3 +39,7 @@ function linear_combination(P1::SimpleSparsePolynomialZonotope, return SimpleSparsePolynomialZonotope(c, G, E) end + +function linear_combination(X::ConvexSet, Y::ConvexSet) + return convex_hull(X, Y) +end diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index 532bec4737..7b9b9054e4 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -304,4 +304,9 @@ for N in [Float64, Float32, Rational{Int}] # isapprox @test x ≈ x ≈ translate(x, [1e-8]) @test !(x ≈ translate(x, [1e-4])) + + # convex_hull & linear_combination + I1 = Interval(N(0), N(1)) + I2 = Interval(N(2), N(3)) + @test convex_hull(I1, I2) == linear_combination(I1, I2) == Interval(N(0), N(3)) end From d82fb2a7e85e0f9894177c4a5630893e113527be Mon Sep 17 00:00:00 2001 From: schillic Date: Sat, 1 Jun 2024 20:18:54 +0200 Subject: [PATCH 06/17] better convex_hull for Interval --- docs/src/lib/sets/Interval.md | 1 + src/Sets/Interval/IntervalModule.jl | 7 ++++--- src/Sets/Interval/convex_hull.jl | 3 +++ 3 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 src/Sets/Interval/convex_hull.jl diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index 0ae7bd8e02..61a1250387 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -65,6 +65,7 @@ Undocumented implementations: * [`permute`](@ref permute(::LazySet, ::AbstractVector{Int})) * [`project`](@ref project(::LazySet, ::AbstractVector{Int})) * [`radius`](@ref radius(::LazySet, ::Real)) +* [`convex_hull`](@ref convex_hull(::LazySet, ::LazySet)) * [`≈`](@ref ≈(::LazySet, ::LazySet)) ```@meta diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index e9c886dc48..4878f812b4 100644 --- a/src/Sets/Interval/IntervalModule.jl +++ b/src/Sets/Interval/IntervalModule.jl @@ -12,9 +12,9 @@ using ReachabilityBase.Comparison using ReachabilityBase.Distribution: reseed! import IntervalArithmetic as IA -@reexport import ..API: an_element, center, constraints_list, diameter, dim, high, ∈, - isoperationtype, linear_map, low, permute, project, rand, rectify, - reflect, scale, ρ, σ, translate, vertices_list, +@reexport import ..API: an_element, center, constraints_list, convex_hull, diameter, dim, + high, ∈, isoperationtype, linear_map, low, permute, project, + rand, rectify, reflect, scale, ρ, σ, translate, vertices_list, difference, intersection, ≈, isdisjoint, ⊆, minkowski_difference, minkowski_sum @reexport import ..LazySets: chebyshev_center_radius, isflat, ngens, plot_recipe, @@ -30,6 +30,7 @@ include("center.jl") include("chebyshev_center_radius.jl") include("constraints_list.jl") include("convert.jl") +include("convex_hull.jl") include("diameter.jl") include("dim.jl") include("high.jl") diff --git a/src/Sets/Interval/convex_hull.jl b/src/Sets/Interval/convex_hull.jl new file mode 100644 index 0000000000..46b39e5076 --- /dev/null +++ b/src/Sets/Interval/convex_hull.jl @@ -0,0 +1,3 @@ +function convex_hull(I1::Interval, I2::Interval) + return Interval(min(min(I1), min(I2)), max(max(I1), max(I2))) +end From 3017cda67e0f5137b38e8a6bbae87e0f7218f4ce Mon Sep 17 00:00:00 2001 From: schillic Date: Thu, 6 Jun 2024 05:47:09 +0200 Subject: [PATCH 07/17] better complement for Interval --- docs/src/lib/sets/Interval.md | 2 +- src/Sets/Interval/IntervalModule.jl | 8 +++++--- src/Sets/Interval/complement.jl | 6 ++++++ test/Sets/Interval.jl | 6 ++++++ 4 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 src/Sets/Interval/complement.jl diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index 61a1250387..50741f3e92 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -61,6 +61,7 @@ CurrentModule = LazySets.API ``` Undocumented implementations: +* [`complement`](@ref complement(::LazySet)) * [`isoperationtype`](@ref isoperationtype(::Type{<:LazySet})) * [`permute`](@ref permute(::LazySet, ::AbstractVector{Int})) * [`project`](@ref project(::LazySet, ::AbstractVector{Int})) @@ -73,7 +74,6 @@ CurrentModule = LazySets ``` Inherited from [`LazySet`](@ref): -* [`complement`](@ref complement(::LazySet)) * [`concretize`](@ref concretize(::LazySet)) * [`constraints`](@ref constraints(::LazySet)) * [`eltype`](@ref eltype(::Type{<:LazySet})) diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index 4878f812b4..c2ad63a720 100644 --- a/src/Sets/Interval/IntervalModule.jl +++ b/src/Sets/Interval/IntervalModule.jl @@ -12,9 +12,10 @@ using ReachabilityBase.Comparison using ReachabilityBase.Distribution: reseed! import IntervalArithmetic as IA -@reexport import ..API: an_element, center, constraints_list, convex_hull, diameter, dim, - high, ∈, isoperationtype, linear_map, low, permute, project, - rand, rectify, reflect, scale, ρ, σ, translate, vertices_list, +@reexport import ..API: an_element, center, complement, constraints_list, convex_hull, + diameter, dim, high, ∈, isoperationtype, linear_map, low, + permute, project, rand, rectify, reflect, scale, ρ, σ, translate, + vertices_list, difference, intersection, ≈, isdisjoint, ⊆, minkowski_difference, minkowski_sum @reexport import ..LazySets: chebyshev_center_radius, isflat, ngens, plot_recipe, @@ -28,6 +29,7 @@ include("Interval.jl") include("an_element.jl") include("center.jl") include("chebyshev_center_radius.jl") +include("complement.jl") include("constraints_list.jl") include("convert.jl") include("convex_hull.jl") diff --git a/src/Sets/Interval/complement.jl b/src/Sets/Interval/complement.jl new file mode 100644 index 0000000000..9d4f919b1f --- /dev/null +++ b/src/Sets/Interval/complement.jl @@ -0,0 +1,6 @@ +function complement(X::Interval) + N = eltype(X) + L = HalfSpace(SingleEntryVector(1, 1, one(N)), min(X)) + H = HalfSpace(SingleEntryVector(1, 1, -one(N)), -max(X)) + return UnionSet(L, H) +end diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index 7b9b9054e4..241a6bc0cb 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -309,4 +309,10 @@ for N in [Float64, Float32, Rational{Int}] I1 = Interval(N(0), N(1)) I2 = Interval(N(2), N(3)) @test convex_hull(I1, I2) == linear_combination(I1, I2) == Interval(N(0), N(3)) + + # complement + C = complement(I2) + L = HalfSpace(N[1], N(2)) + H = HalfSpace(N[-1], N(-3)) + @test length(C) == 2 && ispermutation([C[1], C[2]], [L, H]) end From 04b0d5a5439b1ebd35de235cedcb0e767170a049 Mon Sep 17 00:00:00 2001 From: schillic Date: Sun, 9 Jun 2024 17:48:32 +0200 Subject: [PATCH 08/17] better extrema for Interval --- docs/src/lib/sets/Interval.md | 4 ++-- src/Sets/Interval/IntervalModule.jl | 7 ++++--- src/Sets/Interval/extrema.jl | 12 ++++++++++++ test/Sets/Interval.jl | 7 +++++++ 4 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 src/Sets/Interval/extrema.jl diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index 50741f3e92..47f9ba7d56 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -62,6 +62,8 @@ CurrentModule = LazySets.API Undocumented implementations: * [`complement`](@ref complement(::LazySet)) +* [`extrema`](@ref extrema(::LazySet)) +* [`extrema`](@ref extrema(::LazySet, ::Int)) * [`isoperationtype`](@ref isoperationtype(::Type{<:LazySet})) * [`permute`](@ref permute(::LazySet, ::AbstractVector{Int})) * [`project`](@ref project(::LazySet, ::AbstractVector{Int})) @@ -117,8 +119,6 @@ Inherited from [`AbstractZonotope`](@ref): * [`togrep`](@ref togrep(::AbstractZonotope)) Inherited from [`AbstractHyperrectangle`](@ref): -* [`extrema`](@ref extrema(::AbstractHyperrectangle)) -* [`extrema`](@ref extrema(::AbstractHyperrectangle, ::Int)) * [`generators`](@ref generators(::AbstractHyperrectangle)) * [`genmat`](@ref genmat(::AbstractHyperrectangle)) * [`high`](@ref high(::AbstractHyperrectangle, ::Int)) diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index c2ad63a720..ce60a5fa81 100644 --- a/src/Sets/Interval/IntervalModule.jl +++ b/src/Sets/Interval/IntervalModule.jl @@ -13,9 +13,9 @@ using ReachabilityBase.Distribution: reseed! import IntervalArithmetic as IA @reexport import ..API: an_element, center, complement, constraints_list, convex_hull, - diameter, dim, high, ∈, isoperationtype, linear_map, low, - permute, project, rand, rectify, reflect, scale, ρ, σ, translate, - vertices_list, + diameter, dim, extrema, high, ∈, isoperationtype, linear_map, + low, permute, project, rand, rectify, reflect, scale, ρ, σ, + translate, vertices_list, difference, intersection, ≈, isdisjoint, ⊆, minkowski_difference, minkowski_sum @reexport import ..LazySets: chebyshev_center_radius, isflat, ngens, plot_recipe, @@ -35,6 +35,7 @@ include("convert.jl") include("convex_hull.jl") include("diameter.jl") include("dim.jl") +include("extrema.jl") include("high.jl") include("in.jl") include("isflat.jl") diff --git a/src/Sets/Interval/extrema.jl b/src/Sets/Interval/extrema.jl new file mode 100644 index 0000000000..676819291d --- /dev/null +++ b/src/Sets/Interval/extrema.jl @@ -0,0 +1,12 @@ +# the implementations here are equivalent to the default implementation for +# `LazySet`, but more efficient than the more specific implementations for +# subtypes + +function extrema(X::Interval, i::Int) + @assert i == 1 "an interval has dimension 1, but the index is $i" + return (min(X), max(X)) +end + +function extrema(X::Interval) + return ([min(X)], [max(X)]) +end diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index 241a6bc0cb..f8f0b2d4c3 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -315,4 +315,11 @@ for N in [Float64, Float32, Rational{Int}] L = HalfSpace(N[1], N(2)) H = HalfSpace(N[-1], N(-3)) @test length(C) == 2 && ispermutation([C[1], C[2]], [L, H]) + + # low, high, extrema + @test extrema(I1) == (low(I1), high(I1)) == (N[0], N[1]) + @test extrema(I1, 1) == (low(I1, 1), high(I1, 1)) == (N(0), N(1)) + @test_throws AssertionError low(I1, 2) + @test_throws AssertionError high(I1, 2) + @test_throws AssertionError extrema(I1, 2) end From 97d138ae8ec774a86f8c271fb8e9b2456b14a41c Mon Sep 17 00:00:00 2001 From: schillic Date: Sun, 9 Jun 2024 17:59:30 +0200 Subject: [PATCH 09/17] better low/high for Interval --- docs/src/lib/sets/Interval.md | 8 ++++---- src/Sets/Interval/high.jl | 20 ++++++-------------- src/Sets/Interval/low.jl | 20 ++++++-------------- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index 47f9ba7d56..ba62deb64a 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -26,11 +26,9 @@ chebyshev_center_radius(::Interval) constraints_list(::Interval) diameter(::Interval, ::Real=Inf) dim(::Interval) -high(::Interval) ∈(::AbstractVector, ::Interval) isflat(::Interval) linear_map(::AbstractMatrix, ::Interval) -low(::Interval) ngens(::Interval) radius_hyperrectangle(::Interval) radius_hyperrectangle(::Interval{N}, ::Int) where {N} @@ -64,7 +62,11 @@ Undocumented implementations: * [`complement`](@ref complement(::LazySet)) * [`extrema`](@ref extrema(::LazySet)) * [`extrema`](@ref extrema(::LazySet, ::Int)) +* [`high`](@ref high(::LazySet, ::Int)) +* [`high`](@ref high(::LazySet)) * [`isoperationtype`](@ref isoperationtype(::Type{<:LazySet})) +* [`low`](@ref low(::LazySet, ::Int)) +* [`low`](@ref low(::LazySet)) * [`permute`](@ref permute(::LazySet, ::AbstractVector{Int})) * [`project`](@ref project(::LazySet, ::AbstractVector{Int})) * [`radius`](@ref radius(::LazySet, ::Real)) @@ -121,9 +123,7 @@ Inherited from [`AbstractZonotope`](@ref): Inherited from [`AbstractHyperrectangle`](@ref): * [`generators`](@ref generators(::AbstractHyperrectangle)) * [`genmat`](@ref genmat(::AbstractHyperrectangle)) -* [`high`](@ref high(::AbstractHyperrectangle, ::Int)) * [`isconvextype`](@ref isconvextype(::Type{<:AbstractHyperrectangle})) -* [`low`](@ref low(::AbstractHyperrectangle, ::Int)) * [`norm`](@ref norm(::AbstractHyperrectangle, ::Real)) * [`volume`](@ref volume(::AbstractHyperrectangle)) * [`cartesian_product`](@ref cartesian_product(::AbstractHyperrectangle, ::AbstractHyperrectangle)) diff --git a/src/Sets/Interval/high.jl b/src/Sets/Interval/high.jl index 7b3dd7e382..ed40748d31 100644 --- a/src/Sets/Interval/high.jl +++ b/src/Sets/Interval/high.jl @@ -1,16 +1,8 @@ -""" - high(x::Interval) - -Return the higher coordinate of an interval set. - -### Input - -- `x` -- interval - -### Output +function high(X::Interval, i::Int) + @assert i == 1 "an interval has dimension 1, but the index is $i" + return max(X) +end -A vector with the higher coordinate of the interval. -""" -function high(x::Interval) - return [x.dat.hi] +function high(X::Interval) + return [max(X)] end diff --git a/src/Sets/Interval/low.jl b/src/Sets/Interval/low.jl index 6f18a067d3..7d2012a92b 100644 --- a/src/Sets/Interval/low.jl +++ b/src/Sets/Interval/low.jl @@ -1,16 +1,8 @@ -""" - low(x::Interval) - -Return the lower coordinate of an interval set. - -### Input - -- `x` -- interval - -### Output +function low(X::Interval, i::Int) + @assert i == 1 "an interval has dimension 1, but the index is $i" + return min(X) +end -A vector with the lower coordinate of the interval. -""" -function low(x::Interval) - return [x.dat.lo] +function low(X::Interval) + return [min(X)] end From 7e5e5dbdbf6c7e6d8c3b5b89d81075b2e04d8df6 Mon Sep 17 00:00:00 2001 From: schillic Date: Sun, 9 Jun 2024 18:06:42 +0200 Subject: [PATCH 10/17] better norm for Interval --- docs/src/lib/sets/Interval.md | 2 +- src/Sets/Interval/IntervalModule.jl | 3 ++- src/Sets/Interval/norm.jl | 3 +++ test/Sets/Interval.jl | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 src/Sets/Interval/norm.jl diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index ba62deb64a..7cc54cf3ec 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -67,6 +67,7 @@ Undocumented implementations: * [`isoperationtype`](@ref isoperationtype(::Type{<:LazySet})) * [`low`](@ref low(::LazySet, ::Int)) * [`low`](@ref low(::LazySet)) +* [`norm`](@ref norm(::LazySet, ::Real)) * [`permute`](@ref permute(::LazySet, ::AbstractVector{Int})) * [`project`](@ref project(::LazySet, ::AbstractVector{Int})) * [`radius`](@ref radius(::LazySet, ::Real)) @@ -124,7 +125,6 @@ Inherited from [`AbstractHyperrectangle`](@ref): * [`generators`](@ref generators(::AbstractHyperrectangle)) * [`genmat`](@ref genmat(::AbstractHyperrectangle)) * [`isconvextype`](@ref isconvextype(::Type{<:AbstractHyperrectangle})) -* [`norm`](@ref norm(::AbstractHyperrectangle, ::Real)) * [`volume`](@ref volume(::AbstractHyperrectangle)) * [`cartesian_product`](@ref cartesian_product(::AbstractHyperrectangle, ::AbstractHyperrectangle)) * [`distance`](@ref distance(::AbstractHyperrectangle, ::AbstractHyperrectangle)) diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index ce60a5fa81..b8b86ab8e2 100644 --- a/src/Sets/Interval/IntervalModule.jl +++ b/src/Sets/Interval/IntervalModule.jl @@ -14,7 +14,7 @@ import IntervalArithmetic as IA @reexport import ..API: an_element, center, complement, constraints_list, convex_hull, diameter, dim, extrema, high, ∈, isoperationtype, linear_map, - low, permute, project, rand, rectify, reflect, scale, ρ, σ, + low, norm, permute, project, rand, rectify, reflect, scale, ρ, σ, translate, vertices_list, difference, intersection, ≈, isdisjoint, ⊆, minkowski_difference, minkowski_sum @@ -43,6 +43,7 @@ include("linear_map.jl") include("low.jl") include("isoperationtype.jl") include("ngens.jl") +include("norm.jl") include("permute.jl") include("project.jl") include("radius.jl") diff --git a/src/Sets/Interval/norm.jl b/src/Sets/Interval/norm.jl new file mode 100644 index 0000000000..dc213f6edc --- /dev/null +++ b/src/Sets/Interval/norm.jl @@ -0,0 +1,3 @@ +function norm(X::Interval, p::Real=Inf) + return max(abs(min(X)), abs(max(X))) +end diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index f8f0b2d4c3..ccb3f09125 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -322,4 +322,8 @@ for N in [Float64, Float32, Rational{Int}] @test_throws AssertionError low(I1, 2) @test_throws AssertionError high(I1, 2) @test_throws AssertionError extrema(I1, 2) + + # norm + @test norm(I1) == N(1) + @test norm(Interval(N(-2), N(1))) == N(2) end From a7a57f1138201fed2e7af266ee0f737f97132893 Mon Sep 17 00:00:00 2001 From: schillic Date: Sun, 9 Jun 2024 18:09:59 +0200 Subject: [PATCH 11/17] better volume for Interval --- docs/src/lib/sets/Interval.md | 2 +- src/Sets/Interval/IntervalModule.jl | 3 ++- src/Sets/Interval/volume.jl | 3 +++ test/Sets/Interval.jl | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 src/Sets/Interval/volume.jl diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index 7cc54cf3ec..dd6afda6f8 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -71,6 +71,7 @@ Undocumented implementations: * [`permute`](@ref permute(::LazySet, ::AbstractVector{Int})) * [`project`](@ref project(::LazySet, ::AbstractVector{Int})) * [`radius`](@ref radius(::LazySet, ::Real)) +* [`volume`](@ref volume(::LazySet)) * [`convex_hull`](@ref convex_hull(::LazySet, ::LazySet)) * [`≈`](@ref ≈(::LazySet, ::LazySet)) @@ -125,7 +126,6 @@ Inherited from [`AbstractHyperrectangle`](@ref): * [`generators`](@ref generators(::AbstractHyperrectangle)) * [`genmat`](@ref genmat(::AbstractHyperrectangle)) * [`isconvextype`](@ref isconvextype(::Type{<:AbstractHyperrectangle})) -* [`volume`](@ref volume(::AbstractHyperrectangle)) * [`cartesian_product`](@ref cartesian_product(::AbstractHyperrectangle, ::AbstractHyperrectangle)) * [`distance`](@ref distance(::AbstractHyperrectangle, ::AbstractHyperrectangle)) diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index b8b86ab8e2..6376e9dc7d 100644 --- a/src/Sets/Interval/IntervalModule.jl +++ b/src/Sets/Interval/IntervalModule.jl @@ -15,7 +15,7 @@ import IntervalArithmetic as IA @reexport import ..API: an_element, center, complement, constraints_list, convex_hull, diameter, dim, extrema, high, ∈, isoperationtype, linear_map, low, norm, permute, project, rand, rectify, reflect, scale, ρ, σ, - translate, vertices_list, + translate, vertices_list, volume, difference, intersection, ≈, isdisjoint, ⊆, minkowski_difference, minkowski_sum @reexport import ..LazySets: chebyshev_center_radius, isflat, ngens, plot_recipe, @@ -57,6 +57,7 @@ include("support_vector.jl") include("support_function.jl") include("translate.jl") include("vertices_list.jl") +include("volume.jl") include("difference.jl") include("intersection.jl") diff --git a/src/Sets/Interval/volume.jl b/src/Sets/Interval/volume.jl new file mode 100644 index 0000000000..393a7475db --- /dev/null +++ b/src/Sets/Interval/volume.jl @@ -0,0 +1,3 @@ +function volume(X::Interval) + return max(X) - min(X) +end diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index ccb3f09125..d0bd302b63 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -326,4 +326,8 @@ for N in [Float64, Float32, Rational{Int}] # norm @test norm(I1) == N(1) @test norm(Interval(N(-2), N(1))) == N(2) + + # volume + @test volume(I1) == 1 + @test volume(Interval(N(-2), N(1))) == N(3) end From f390ec1af6e906ff880b600c53b6b33adfb144ba Mon Sep 17 00:00:00 2001 From: schillic Date: Mon, 10 Jun 2024 18:48:52 +0200 Subject: [PATCH 12/17] better affine_map for Interval --- docs/src/lib/sets/Interval.md | 2 +- src/Sets/Interval/IntervalModule.jl | 9 +++++---- src/Sets/Interval/affine_map.jl | 10 ++++++++++ test/Sets/Interval.jl | 5 +++++ 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 src/Sets/Interval/affine_map.jl diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index dd6afda6f8..0c0bfc89de 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -59,6 +59,7 @@ CurrentModule = LazySets.API ``` Undocumented implementations: +* [`affine_map`](@ref affine_map(::AbstractMatrix, ::LazySet, ::AbstractVector)) * [`complement`](@ref complement(::LazySet)) * [`extrema`](@ref extrema(::LazySet)) * [`extrema`](@ref extrema(::LazySet, ::Int)) @@ -87,7 +88,6 @@ Inherited from [`LazySet`](@ref): * [`isoperation`](@ref isoperation(::LazySet)) * [`singleton_list`](@ref singleton_list(::LazySet)) * [`vertices`](@ref vertices(::LazySet)) -* [`affine_map`](@ref affine_map(::AbstractMatrix, ::LazySet, ::AbstractVector)) * [`exponential_map`](@ref exponential_map(::AbstractMatrix, ::LazySet)) * [`is_interior_point`](@ref is_interior_point(::AbstractVector, ::LazySet)) * [`sample`](@ref sample(::LazySet, ::Int)) diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index 6376e9dc7d..c5b7b3797c 100644 --- a/src/Sets/Interval/IntervalModule.jl +++ b/src/Sets/Interval/IntervalModule.jl @@ -12,10 +12,10 @@ using ReachabilityBase.Comparison using ReachabilityBase.Distribution: reseed! import IntervalArithmetic as IA -@reexport import ..API: an_element, center, complement, constraints_list, convex_hull, - diameter, dim, extrema, high, ∈, isoperationtype, linear_map, - low, norm, permute, project, rand, rectify, reflect, scale, ρ, σ, - translate, vertices_list, volume, +@reexport import ..API: affine_map, an_element, center, complement, constraints_list, + convex_hull, diameter, dim, extrema, high, ∈, isoperationtype, + linear_map, low, norm, permute, project, rand, rectify, reflect, + scale, ρ, σ, translate, vertices_list, volume, difference, intersection, ≈, isdisjoint, ⊆, minkowski_difference, minkowski_sum @reexport import ..LazySets: chebyshev_center_radius, isflat, ngens, plot_recipe, @@ -26,6 +26,7 @@ export Interval include("Interval.jl") +include("affine_map.jl") include("an_element.jl") include("center.jl") include("chebyshev_center_radius.jl") diff --git a/src/Sets/Interval/affine_map.jl b/src/Sets/Interval/affine_map.jl new file mode 100644 index 0000000000..df7cfa63c2 --- /dev/null +++ b/src/Sets/Interval/affine_map.jl @@ -0,0 +1,10 @@ +function affine_map(M, X::Interval, v::AbstractVector; kwargs...) + @assert size(M, 2) == 1 "cannot apply an affine map of dimension $(size(M, 2)) " * + "to an interval" + @assert size(M, 1) == length(v) "cannot apply an affine map of matrix dimension " * + "$(size(M, 1)) and translation dimension $(length(v))" + @inbounds if length(v) == 1 + return Interval(M[1, 1] * X.dat + v[1]) + end + return translate(linear_map(M, X; kwargs...), v) +end diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index d0bd302b63..cbff1d5eb9 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -330,4 +330,9 @@ for N in [Float64, Float32, Rational{Int}] # volume @test volume(I1) == 1 @test volume(Interval(N(-2), N(1))) == N(3) + + # affine_map + M = hcat(N[2]) + v = N[-3] + @test affine_map(M, I1, v) == Interval(N(-3), N(-1)) end From 90f2440cc6a35ee2973116b823ecf405d3c1c580 Mon Sep 17 00:00:00 2001 From: schillic Date: Mon, 10 Jun 2024 19:05:48 +0200 Subject: [PATCH 13/17] better exponential_map for Interval --- docs/src/lib/sets/Interval.md | 2 +- src/Interfaces/LazySet.jl | 3 +++ src/Sets/Interval/IntervalModule.jl | 7 ++++--- src/Sets/Interval/exponential_map.jl | 6 ++++++ test/Sets/Interval.jl | 3 +++ 5 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 src/Sets/Interval/exponential_map.jl diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index 0c0bfc89de..1c95ee8f64 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -61,6 +61,7 @@ CurrentModule = LazySets.API Undocumented implementations: * [`affine_map`](@ref affine_map(::AbstractMatrix, ::LazySet, ::AbstractVector)) * [`complement`](@ref complement(::LazySet)) +* [`exponential_map`](@ref exponential_map(::AbstractMatrix, ::LazySet)) * [`extrema`](@ref extrema(::LazySet)) * [`extrema`](@ref extrema(::LazySet, ::Int)) * [`high`](@ref high(::LazySet, ::Int)) @@ -88,7 +89,6 @@ Inherited from [`LazySet`](@ref): * [`isoperation`](@ref isoperation(::LazySet)) * [`singleton_list`](@ref singleton_list(::LazySet)) * [`vertices`](@ref vertices(::LazySet)) -* [`exponential_map`](@ref exponential_map(::AbstractMatrix, ::LazySet)) * [`is_interior_point`](@ref is_interior_point(::AbstractVector, ::LazySet)) * [`sample`](@ref sample(::LazySet, ::Int)) * [`==`](@ref ==(::LazySet, ::LazySet)) diff --git a/src/Interfaces/LazySet.jl b/src/Interfaces/LazySet.jl index 640be7fb1f..c6df0a83cd 100644 --- a/src/Interfaces/LazySet.jl +++ b/src/Interfaces/LazySet.jl @@ -487,6 +487,9 @@ end The default implementation applies the functions `exp` and `linear_map`. """ function exponential_map(M::AbstractMatrix, X::LazySet) + n = dim(X) + @assert size(M) == (n, n) "cannot apply an exponential map of dimension " * + "$(size(M)) to an $n-dimensional set" return linear_map(exp(M), X) end diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index c5b7b3797c..e2f54f8b45 100644 --- a/src/Sets/Interval/IntervalModule.jl +++ b/src/Sets/Interval/IntervalModule.jl @@ -13,9 +13,9 @@ using ReachabilityBase.Distribution: reseed! import IntervalArithmetic as IA @reexport import ..API: affine_map, an_element, center, complement, constraints_list, - convex_hull, diameter, dim, extrema, high, ∈, isoperationtype, - linear_map, low, norm, permute, project, rand, rectify, reflect, - scale, ρ, σ, translate, vertices_list, volume, + 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, ⊆, minkowski_difference, minkowski_sum @reexport import ..LazySets: chebyshev_center_radius, isflat, ngens, plot_recipe, @@ -36,6 +36,7 @@ include("convert.jl") include("convex_hull.jl") include("diameter.jl") include("dim.jl") +include("exponential_map.jl") include("extrema.jl") include("high.jl") include("in.jl") diff --git a/src/Sets/Interval/exponential_map.jl b/src/Sets/Interval/exponential_map.jl new file mode 100644 index 0000000000..368cc68ad5 --- /dev/null +++ b/src/Sets/Interval/exponential_map.jl @@ -0,0 +1,6 @@ +function exponential_map(M::AbstractMatrix, X::Interval) + @assert size(M) == (1, 1) "cannot apply an exponential map of dimension " * + "$(size(M)) to an interval" + @inbounds e = exp(M[1, 1]) + return Interval(e * X.dat) +end diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index cbff1d5eb9..1231405e88 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -335,4 +335,7 @@ for N in [Float64, Float32, Rational{Int}] M = hcat(N[2]) v = N[-3] @test affine_map(M, I1, v) == Interval(N(-3), N(-1)) + + # exponential_map + @test exponential_map(M, I1) == Interval(N(0), N(exp(N(2)))) end From 6866198cc2d1038de09db542a607e4eb575f3312 Mon Sep 17 00:00:00 2001 From: schillic Date: Mon, 10 Jun 2024 20:40:42 +0200 Subject: [PATCH 14/17] fix alphabetic order --- src/Sets/Interval/IntervalModule.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index e2f54f8b45..1208832619 100644 --- a/src/Sets/Interval/IntervalModule.jl +++ b/src/Sets/Interval/IntervalModule.jl @@ -41,9 +41,9 @@ include("extrema.jl") include("high.jl") include("in.jl") include("isflat.jl") +include("isoperationtype.jl") include("linear_map.jl") include("low.jl") -include("isoperationtype.jl") include("ngens.jl") include("norm.jl") include("permute.jl") From ce5801fe86dae07e9daa91a84dae978ca058a464 Mon Sep 17 00:00:00 2001 From: schillic Date: Mon, 10 Jun 2024 21:00:23 +0200 Subject: [PATCH 15/17] better isequivalent for Interval --- docs/src/lib/sets/Interval.md | 2 +- src/Sets/Interval/IntervalModule.jl | 5 +++-- src/Sets/Interval/isequivalent.jl | 3 +++ test/Sets/Interval.jl | 3 +++ 4 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 src/Sets/Interval/isequivalent.jl diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index 1c95ee8f64..995d56a059 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -76,6 +76,7 @@ Undocumented implementations: * [`volume`](@ref volume(::LazySet)) * [`convex_hull`](@ref convex_hull(::LazySet, ::LazySet)) * [`≈`](@ref ≈(::LazySet, ::LazySet)) +* [`isequivalent`](@ref isequivalent(::LazySet, ::LazySet)) ```@meta CurrentModule = LazySets @@ -92,7 +93,6 @@ Inherited from [`LazySet`](@ref): * [`is_interior_point`](@ref is_interior_point(::AbstractVector, ::LazySet)) * [`sample`](@ref sample(::LazySet, ::Int)) * [`==`](@ref ==(::LazySet, ::LazySet)) -* [`isequivalent`](@ref isequivalent(::LazySet, ::LazySet)) * [`⊂`](@ref ⊂(::LazySet, ::LazySet)) Inherited from [`AbstractPolyhedron`](@ref): diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index 1208832619..79531f9e80 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, ⊆, minkowski_difference, - minkowski_sum + difference, 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 @@ -65,6 +65,7 @@ include("difference.jl") include("intersection.jl") include("isapprox.jl") include("isdisjoint.jl") +include("isequivalent.jl") include("issubset.jl") include("minkowski_difference.jl") include("minkowski_sum.jl") diff --git a/src/Sets/Interval/isequivalent.jl b/src/Sets/Interval/isequivalent.jl new file mode 100644 index 0000000000..c0e2c7ad27 --- /dev/null +++ b/src/Sets/Interval/isequivalent.jl @@ -0,0 +1,3 @@ +function isequivalent(I1::Interval, I2::Interval) + return I1 ≈ I2 +end diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index 1231405e88..450e573ff5 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -338,4 +338,7 @@ for N in [Float64, Float32, Rational{Int}] # exponential_map @test exponential_map(M, I1) == Interval(N(0), N(exp(N(2)))) + + # isequivalent + @test isequivalent(I1, I1) && !isequivalent(I1, I2) end From 9e720b4e88f30fbd12146b3ad5e31635f7960c17 Mon Sep 17 00:00:00 2001 From: schillic Date: Mon, 10 Jun 2024 22:47:16 +0200 Subject: [PATCH 16/17] better isstrictsubset for Interval --- docs/src/lib/sets/Interval.md | 2 +- src/Sets/Interval/IntervalModule.jl | 3 ++- src/Sets/Interval/isstrictsubset.jl | 11 +++++++++++ test/Sets/Interval.jl | 11 +++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/Sets/Interval/isstrictsubset.jl diff --git a/docs/src/lib/sets/Interval.md b/docs/src/lib/sets/Interval.md index 995d56a059..9fd329c8d3 100644 --- a/docs/src/lib/sets/Interval.md +++ b/docs/src/lib/sets/Interval.md @@ -77,6 +77,7 @@ Undocumented implementations: * [`convex_hull`](@ref convex_hull(::LazySet, ::LazySet)) * [`≈`](@ref ≈(::LazySet, ::LazySet)) * [`isequivalent`](@ref isequivalent(::LazySet, ::LazySet)) +* [`⊂`](@ref ⊂(::LazySet, ::LazySet)) ```@meta CurrentModule = LazySets @@ -93,7 +94,6 @@ Inherited from [`LazySet`](@ref): * [`is_interior_point`](@ref is_interior_point(::AbstractVector, ::LazySet)) * [`sample`](@ref sample(::LazySet, ::Int)) * [`==`](@ref ==(::LazySet, ::LazySet)) -* [`⊂`](@ref ⊂(::LazySet, ::LazySet)) Inherited from [`AbstractPolyhedron`](@ref): * [`is_polyhedral`](@ref is_polyhedral(::AbstractPolyhedron)) diff --git a/src/Sets/Interval/IntervalModule.jl b/src/Sets/Interval/IntervalModule.jl index 79531f9e80..0fe1bcc6de 100644 --- a/src/Sets/Interval/IntervalModule.jl +++ b/src/Sets/Interval/IntervalModule.jl @@ -16,7 +16,7 @@ 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, ⊆, + difference, intersection, ≈, isdisjoint, isequivalent, ⊂, ⊆, minkowski_difference, minkowski_sum @reexport import ..LazySets: chebyshev_center_radius, isflat, ngens, plot_recipe, radius_hyperrectangle, split @@ -66,6 +66,7 @@ include("intersection.jl") include("isapprox.jl") include("isdisjoint.jl") include("isequivalent.jl") +include("isstrictsubset.jl") include("issubset.jl") include("minkowski_difference.jl") include("minkowski_sum.jl") diff --git a/src/Sets/Interval/isstrictsubset.jl b/src/Sets/Interval/isstrictsubset.jl new file mode 100644 index 0000000000..ea4bcbe02a --- /dev/null +++ b/src/Sets/Interval/isstrictsubset.jl @@ -0,0 +1,11 @@ +function ⊂(X::Interval, Y::Interval, witness::Bool=false) + if min(X) < min(Y) || max(X) > max(Y) + return _witness_result_empty(witness, false, X, Y) + end + if min(X) > min(Y) + return witness ? (true, low(Y)) : true + elseif max(Y) > max(X) + return witness ? (true, high(Y)) : true + end + return _witness_result_empty(witness, false, X, Y) +end diff --git a/test/Sets/Interval.jl b/test/Sets/Interval.jl index 450e573ff5..fe1183a7ba 100644 --- a/test/Sets/Interval.jl +++ b/test/Sets/Interval.jl @@ -282,12 +282,23 @@ for N in [Float64, Float32, Rational{Int}] I13 = Interval(N(1), N(3)) I02 = Interval(N(0), N(2)) I24 = Interval(N(2), N(4)) + I03 = Interval(N(0), N(3)) + I14 = Interval(N(1), N(4)) for I in (I02, I24) res, w = ⊆(I13, I, true) @test !(⊆(I13, I)) && !res && w ∈ I13 && w ∉ I end res, w = ⊆(I13, I13, true) @test ⊆(I13, I13) && res && w == N[] + # isstrictsubset + for I in (I02, I24, I13) + res, w = ⊂(I13, I, true) + @test !(⊂(I13, I)) && !res && w == N[] + end + for I in (I03, I14) + res, w = ⊂(I13, I, true) + @test ⊂(I13, I) && res && w ∈ I && w ∉ I13 + end # permute @test permute(x, [1]) == x From 36c3dba1b41967fc5dfe507c107271c1f5fa8158 Mon Sep 17 00:00:00 2001 From: schillic Date: Tue, 11 Jun 2024 18:55:31 +0200 Subject: [PATCH 17/17] better distance for Interval --- docs/src/lib/sets/Interval.md | 2 +- src/Sets/Interval/IntervalModule.jl | 5 +++-- src/Sets/Interval/distance.jl | 7 +++++++ test/Sets/Interval.jl | 5 +++++ 4 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 src/Sets/Interval/distance.jl 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