Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make bounds check in hyperrectangle constructor optional #2018

Merged
merged 2 commits into from
Feb 28, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add alternative constructor
mforets committed Feb 28, 2020
commit b4189709760acd3c0aeb3126df0d3d4b4a809d43
20 changes: 13 additions & 7 deletions src/Sets/Hyperrectangle.jl
Original file line number Diff line number Diff line change
@@ -71,18 +71,25 @@ 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
mforets marked this conversation as resolved.
Show resolved Hide resolved
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.
mforets marked this conversation as resolved.
Show resolved Hide resolved
"""
struct Hyperrectangle{N<:Real, VNC<:AbstractVector{N}, VNR<:AbstractVector{N}
} <: AbstractHyperrectangle{N}
center::VNC
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


5 changes: 5 additions & 0 deletions test/unit_Hyperrectangle.jl
Original file line number Diff line number Diff line change
@@ -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