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

#974 - tohrep and tovrep do not preserve numeric type #985

Merged
merged 5 commits into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
6 changes: 5 additions & 1 deletion src/HPolyhedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,11 @@ function tovrep(P::HPoly{N};
@assert isdefined(@__MODULE__, :Polyhedra) "the function `tovrep` needs " *
"the package 'Polyhedra' to be loaded"
P = polyhedron(P; backend=backend)
return VPolytope(P)
Q = VPolytope(P)
if Q isa VPolytope{N}
return Q
end
return VPolytope{N}(vertices_list(Q))
schillic marked this conversation as resolved.
Show resolved Hide resolved
end

"""
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
6 changes: 5 additions & 1 deletion src/VPolytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,11 @@ function tohrep(P::VPolytope{N};
backend=default_polyhedra_backend(P, N)) where {N<:Real}
@assert isdefined(@__MODULE__, :Polyhedra) "the function `tohrep` needs the " *
"package 'Polyhedra' to be loaded"
return HPolytope(polyhedron(P; backend=backend))
Q = HPolytope(polyhedron(P; backend=backend))
if Q isa HPolytope{N}
return Q
end
return HPolytope{N}(constraints_list(Q))
end

"""
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
8 changes: 4 additions & 4 deletions test/unit_Polytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ if test_suite_polyhedra
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
@test tovrep(P) isa VPolytope{N}
@test tohrep(P) isa HPolytope{N} # no-op

# checking for emptiness
P = HPolytope([LinearConstraint(N[1, 0], N(0))]) # x <= 0
Expand Down Expand Up @@ -246,7 +246,7 @@ if test_suite_polyhedra

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