Skip to content

Commit

Permalink
Merge pull request #985 from JuliaReach/schillic/974
Browse files Browse the repository at this point in the history
#974 - tohrep and tovrep do not preserve numeric type
  • Loading branch information
schillic authored Jan 13, 2019
2 parents 94a7cb1 + 646daf3 commit 9bc8dcb
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
10 changes: 6 additions & 4 deletions src/HPolyhedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,9 @@ Transform a polyhedron in H-representation to a polytope in V-representation.
### Input
- `P` -- polyhedron in constraint representation
- `backend` -- (optional, default: `default_polyhedra_backend(P, N)`)
the polyhedral computations backend
- `P` -- polyhedron in constraint representation
- `backend` -- (optional, default: `default_polyhedra_backend(P, N)`) the
backend for polyhedral computations
### Output
Expand All @@ -593,8 +593,10 @@ in constraint representation.
### Notes
The conversion may not preserve the numeric type (e.g., with `N == Float32`)
depending on the backend.
For further information on the supported backends see
[Polyhedra's documentation](https://juliapolyhedra.github.io/Polyhedra.jl/).
[Polyhedra's documentation](https://juliapolyhedra.github.io/Polyhedra.jl/latest/installation.html#Getting-Libraries-1).
"""
function tovrep(P::HPoly{N};
backend=default_polyhedra_backend(P, N)) where {N<:Real}
Expand Down
7 changes: 6 additions & 1 deletion src/HalfSpace.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Base: rand,
,
isempty
isempty,
convert

export HalfSpace, LinearConstraint,
an_element,
Expand Down Expand Up @@ -31,6 +32,10 @@ struct HalfSpace{N<:Real} <: LazySet{N}
b::N
end

function convert(::Type{HalfSpace{N}}, hs::HalfSpace) where {N<:Real}
return HalfSpace{N}(hs.a, hs.b)
end

"""
LinearConstraint
Expand Down
15 changes: 10 additions & 5 deletions src/VPolytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,21 @@ Transform a polytope in V-representation to a polytope in H-representation.
### Input
- `P` -- polytope in vertex representation
- `backend` -- (optional, default: `default_polyhedra_backend(P, N)`) the polyhedral
computations backend,
see [Polyhedra's documentation](https://juliapolyhedra.github.io/Polyhedra.jl/latest/installation.html#Getting-Libraries-1)
for further information
- `P` -- polytope in vertex representation
- `backend` -- (optional, default: `default_polyhedra_backend(P, N)`) the
backend for polyhedral computations
### Output
The `HPolytope` which is the constraint representation of the given polytope
in vertex representation.
### Notes
The conversion may not preserve the numeric type (e.g., with `N == Float32`)
depending on the backend.
For further information on the supported backends see
[Polyhedra's documentation](https://juliapolyhedra.github.io/Polyhedra.jl/latest/installation.html#Getting-Libraries-1).
"""
function tohrep(P::VPolytope{N};
backend=default_polyhedra_backend(P, N)) where {N<:Real}
Expand Down
5 changes: 5 additions & 0 deletions test/unit_HalfSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ for N in [Float64, Rational{Int}, Float32]
normal = ones(N, 3)
hs = HalfSpace(normal, N(5))

# numeric-type conversion preserves vector base type
hs1 = HalfSpace(spzeros(4), 1.)
hs2 = convert(HalfSpace{N}, hs1)
@test hs2.a isa SparseVector{N}

# dimension
@test dim(hs) == 3

Expand Down
37 changes: 25 additions & 12 deletions test/unit_Polytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,31 @@ end
@test HPolytope() isa HPolytope{Float64}
@test VPolytope() isa VPolytope{Float64}

# Polyhedra tests that do not work with Float32
if test_suite_polyhedra
for N in [Float64, Rational{Int}]
# CDDLib does not preserve the Rational{Int} type
if VERSION >= v"0.7" || N == Float64
# tovrep from HPolytope
A = N[0 1; 1 0; -1 -1]
b = N[0.25, 0.25, -0]
P = HPolytope(A, b)
@test tovrep(P) isa VPolytope{N}
@test tohrep(P) isa HPolytope{N} # no-op

# tohrep from VPolytope
v1 = N[1, 0]
v2 = N[0, 1]
v3 = N[-1, 0]
v4 = N[0, -1]
v5 = N[0, 0]
P = VPolytope([v1, v2, v3, v4, v5])
@test tohrep(P) isa HPolytope{N}
@test tovrep(P) isa VPolytope{N} # no-op
end
end
end

# Polyhedra tests that only work with Float64
if test_suite_polyhedra
for N in [Float64]
Expand Down Expand Up @@ -186,13 +211,6 @@ if test_suite_polyhedra
vl = vertices_list(p)
@test ispermutation(vl, [N[0], N[1]])

# tovrep from HPolytope
A = [N(0) N(-1); N(-1) N(0); N(1) N(1)]
b = N[-0.25, -0.25, -0]
P = HPolytope(A, b)
@test tovrep(P) isa VPolytope
@test tohrep(P) isa HPolytope # test no-op

# checking for emptiness
P = HPolytope([LinearConstraint(N[1, 0], N(0))]) # x <= 0
@test !isempty(P)
Expand Down Expand Up @@ -243,10 +261,5 @@ if test_suite_polyhedra
cp = cartesian_product(p1, p2)
vl = vertices_list(cp)
@test ispermutation(vl, [N[0, 0, 2], N[1, 1, 2]])

# tohrep from VPolytope
P = VPolytope([v1, v2, v3, v4, v5])
@test tohrep(P) isa HPolytope
@test tovrep(P) isa VPolytope # no-op
end
end

0 comments on commit 9bc8dcb

Please sign in to comment.