From b4189709760acd3c0aeb3126df0d3d4b4a809d43 Mon Sep 17 00:00:00 2001 From: mforets Date: Fri, 28 Feb 2020 10:28:04 -0300 Subject: [PATCH 1/2] add alternative constructor --- src/Sets/Hyperrectangle.jl | 20 +++++++++++++------- test/unit_Hyperrectangle.jl | 5 +++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/Sets/Hyperrectangle.jl b/src/Sets/Hyperrectangle.jl index 36ab07a0c9..2f9aab6810 100644 --- a/src/Sets/Hyperrectangle.jl +++ b/src/Sets/Hyperrectangle.jl @@ -71,6 +71,11 @@ julia> h = [1.0, 2.0]; julia> Hyperrectangle(low=l, high=h) Hyperrectangle{Float64,Array{Float64,1},Array{Float64,1}}([-1.0, 1.0], [2.0, 1.0]) ``` + +By default, the constructor checks that that radius of the hyperrecatangle +is nonnegative. To supress this check, use the `check_bounds` optional flag +in the constructor. Note that if `check_bounds` is set to `false`, the behavior +the set may be wrong/undefined. """ struct Hyperrectangle{N<:Real, VNC<:AbstractVector{N}, VNR<:AbstractVector{N} } <: AbstractHyperrectangle{N} @@ -78,11 +83,13 @@ struct Hyperrectangle{N<:Real, VNC<:AbstractVector{N}, VNR<:AbstractVector{N} radius::VNR # default constructor with length comparison & domain constraint for radius - function Hyperrectangle(center::VNC, radius::VNR) where + function Hyperrectangle(center::VNC, radius::VNR; check_bounds::Bool=true) where {N<:Real, VNC<:AbstractVector{N}, VNR<:AbstractVector{N}} @assert length(center) == length(radius) "length of center and " * "radius must be equal" - @assert all(v -> v >= zero(N), radius) "radius must not be negative" + if check_bounds + @assert all(v -> v >= zero(N), radius) "radius must not be negative" + end return new{N, VNC, VNR}(center, radius) end end @@ -91,14 +98,13 @@ isoperationtype(::Type{<:Hyperrectangle}) = false isconvextype(::Type{<:Hyperrectangle}) = true # constructor from keyword arguments (lower and upper bounds) -function Hyperrectangle(; - high::AbstractVector{N}, - low::AbstractVector{N}) where {N<:Real} - @assert all(i -> low[i] <= high[i], eachindex(low)) "lower bound must be lower than upper bound" +function Hyperrectangle(; high::AbstractVector{N}, + low::AbstractVector{N}, + check_bounds::Bool=true) where {N<:Real} # compute center and radius from high and low vectors center = (high .+ low) ./ 2 radius = high .- center - return Hyperrectangle(center, radius) + return Hyperrectangle(center, radius, check_bounds=check_bounds) end diff --git a/test/unit_Hyperrectangle.jl b/test/unit_Hyperrectangle.jl index 129beb68a6..94152b86a5 100644 --- a/test/unit_Hyperrectangle.jl +++ b/test/unit_Hyperrectangle.jl @@ -106,6 +106,11 @@ for N in [Float64, Rational{Int}, Float32] @test H1.radius == H2.radius @test_throws AssertionError Hyperrectangle(low=h, high=l) + # constructor without bounds check + @test_throws AssertionError Hyperrectangle(low=N[1], high=N[0]) # default: true + @test_throws AssertionError Hyperrectangle(low=N[1], high=N[0], check_bounds=true) + @test Hyperrectangle(low=N[1], high=N[0], check_bounds=false) isa Hyperrectangle + # Test low and high methods for a hyperrectangle H = Hyperrectangle(low=l, high=h) @test low(H) == l From 1b5ac15729a096cd6c3043d84f00d8bf334eec16 Mon Sep 17 00:00:00 2001 From: Marcelo Forets Date: Fri, 28 Feb 2020 16:28:02 -0300 Subject: [PATCH 2/2] Update src/Sets/Hyperrectangle.jl Co-Authored-By: Christian Schilling --- src/Sets/Hyperrectangle.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sets/Hyperrectangle.jl b/src/Sets/Hyperrectangle.jl index 2f9aab6810..cc9a6fe541 100644 --- a/src/Sets/Hyperrectangle.jl +++ b/src/Sets/Hyperrectangle.jl @@ -75,7 +75,7 @@ Hyperrectangle{Float64,Array{Float64,1},Array{Float64,1}}([-1.0, 1.0], [2.0, 1.0 By default, the constructor checks that that radius of the hyperrecatangle is nonnegative. To supress this check, use the `check_bounds` optional flag in the constructor. Note that if `check_bounds` is set to `false`, the behavior -the set may be wrong/undefined. +of a set with contradictory bounds is undefined. """ struct Hyperrectangle{N<:Real, VNC<:AbstractVector{N}, VNR<:AbstractVector{N} } <: AbstractHyperrectangle{N}