diff --git a/src/HPolyhedron.jl b/src/HPolyhedron.jl index 6e711a575a..91e98db08b 100644 --- a/src/HPolyhedron.jl +++ b/src/HPolyhedron.jl @@ -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 @@ -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} diff --git a/src/HalfSpace.jl b/src/HalfSpace.jl index 01ad3e23ac..0d4d889557 100644 --- a/src/HalfSpace.jl +++ b/src/HalfSpace.jl @@ -1,6 +1,7 @@ import Base: rand, ∈, - isempty + isempty, + convert export HalfSpace, LinearConstraint, an_element, @@ -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 diff --git a/src/VPolytope.jl b/src/VPolytope.jl index 2f5e295179..3b630ec0b4 100644 --- a/src/VPolytope.jl +++ b/src/VPolytope.jl @@ -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} diff --git a/test/unit_HalfSpace.jl b/test/unit_HalfSpace.jl index 71b74cc3d4..7f3eeb0abe 100644 --- a/test/unit_HalfSpace.jl +++ b/test/unit_HalfSpace.jl @@ -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 diff --git a/test/unit_Polytope.jl b/test/unit_Polytope.jl index 79e1edafc8..25debd5499 100644 --- a/test/unit_Polytope.jl +++ b/test/unit_Polytope.jl @@ -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] @@ -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) @@ -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