Skip to content

Commit

Permalink
implement sethrep for interval (JuliaPolyhedra#267)
Browse files Browse the repository at this point in the history
* implement sethrep for interval

* tests added and corrrection
  • Loading branch information
chachaleo committed Jul 28, 2021
1 parent ac9c2d8 commit b04c4f1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ surface(::Interval{T}) where {T} = zero(T)
volume(p::Interval) = p.length
Base.isempty(p::Interval) = isempty(p.vrep)

function Interval{T, AT, D}(haslb::Bool, lb::T, hasub::Bool, ub::T, isempty::Bool) where {T, AT, D}
function _interval(AT, haslb::Bool, lb::T, hasub::Bool, ub::T, isempty::Bool) where {T}
if haslb && hasub && _gt(lb, ub)
isempty = true
end
Expand Down Expand Up @@ -77,10 +77,14 @@ function Interval{T, AT, D}(haslb::Bool, lb::T, hasub::Bool, ub::T, isempty::Boo
h = hrep(hps, hss)
v = vrep(ps, ls, rs)
volume = isempty ? zero(T) : (haslb && hasub ? max(zero(T), ub - lb) : -one(T))
Interval{T, AT, D}(h, v, volume)
return h, v, volume
end

function _hinterval(rep::HRep{T}, ::Type{AT}, D) where {T, AT}
function Interval{T, AT, D}(haslb::Bool, lb::T, hasub::Bool, ub::T, isempty::Bool) where {T, AT, D}
return Interval{T, AT, D}(_interval(AT, haslb, lb, hasub, ub, isempty)...)
end

function _hinterval(rep::HRep{T}, ::Type{AT}) where {T, AT}
haslb = false
lb = zero(T)
hasub = false
Expand Down Expand Up @@ -125,7 +129,11 @@ function _hinterval(rep::HRep{T}, ::Type{AT}, D) where {T, AT}
_setub(hs.β / α)
end
end
Interval{T, AT, D}(haslb, lb, hasub, ub, empty)
return _interval(AT, haslb, lb, hasub, ub, empty)
end

function _hinterval(rep::HRep{T}, ::Type{AT}, D) where {T, AT}
return Interval{T, AT, D}(_hinterval(rep, AT)...)
end

function _vinterval(v::VRep{T}, ::Type{AT}, D) where {T, AT}
Expand Down Expand Up @@ -191,3 +199,11 @@ function detecthlinearity!(::Interval) end
function detectvlinearity!(::Interval) end
function removehredundancy!(::Interval) end
function removevredundancy!(::Interval) end

function sethrep!(p::Interval{T, AT}, h::HRep) where {T, AT}
hnew, v, volume = _hinterval(h, AT)
p.hrep = hnew
p.vrep = v
p.length = volume
return p
end
22 changes: 22 additions & 0 deletions test/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ function cartesian_product_test()
inequality_fulltest(p, expected)
end

function sethrep_test()
# x_1 ≤ 2 x_1 ≥ -1 <=> -x_1 ≤ 1
h1 = HalfSpace([1], 2) HalfSpace([-1], 1)
v1 = convexhull([-1], [2])
p = polyhedron(h1)
@test volume(p) == 3
inequality_fulltest(p, h1)
generator_fulltest(p, v1)
# x_1 ≤ 1 x_1 ≥ -1 <=> -x_1 ≤ 1
h2 = HalfSpace([1], 1) HalfSpace([-1], 1)
v2 = convexhull([-1], [1])
Polyhedra.sethrep!(p, h2)
# Test that the three fields have been modified
@test volume(p) == 2
inequality_fulltest(p, h2)
generator_fulltest(p, v2)
end

@testset "Interval tests" begin

# Closed interval
Expand Down Expand Up @@ -206,4 +224,8 @@ end
@testset "Cartesian product (#132)" begin
cartesian_product_test()
end

@testset "sethrep! (#267)" begin
sethrep_test()
end
end

0 comments on commit b04c4f1

Please sign in to comment.