Skip to content

Commit a5f9cc3

Browse files
authored
Merge pull request #100 from JuliaPolyhedra/bl/constructpolyhedron
Add constructpolyhedron method
2 parents 86e2467 + 7c35790 commit a5f9cc3

File tree

3 files changed

+27
-53
lines changed

3 files changed

+27
-53
lines changed

src/default.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,21 @@ promote_reptype(P::Type{<:Rep}) = P
9696
# whether Rep has the constructor Rep(::It...; solver=...)
9797
supportssolver(::Type{<:Rep}) = false
9898

99-
function default_similar(p::Tuple{Vararg{Rep}}, d::FullDim{N}, ::Type{T}, it::It{N, T}...) where {N, T}
100-
# Some types in p may not support `d` or `T` so we call `similar_type` after `promote_reptype`
101-
RepTout = similar_type(promote_reptype(typeof.(p)...), d, T)
102-
if supportssolver(RepTout)
99+
function constructpolyhedron(RepT::Type{<:Rep{N, T}}, p::Tuple{Vararg{Rep}}, it::It{N, T}...) where {N, T}
100+
if supportssolver(RepT)
103101
solver = default_solver(p...)
104102
if solver !== nothing && !(solver isa JuMP.UnsetSolver)
105-
return RepTout(it..., solver=solver)
103+
return RepT(it..., solver=solver)
106104
end
107105
end
108-
RepTout(it...)::RepTout # FIXME without this type annotation even convexhull(::PointsHull{2,Int64,Array{Int64,1}}, ::PointsHull{2,Int64,Array{Int64,1}}) is not type stable, why ?
106+
RepT(it...)::RepT # FIXME without this type annotation even convexhull(::PointsHull{2,Int64,Array{Int64,1}}, ::PointsHull{2,Int64,Array{Int64,1}}) is not type stable, why ?
107+
108+
end
109+
110+
function default_similar(p::Tuple{Vararg{Rep}}, d::FullDim{N}, ::Type{T}, it::It{N, T}...) where {N, T}
111+
# Some types in p may not support `d` or `T` so we call `similar_type` after `promote_reptype`
112+
RepT = similar_type(promote_reptype(typeof.(p)...), d, T)
113+
constructpolyhedron(RepT, p, it...)
109114
end
110115

111116
"""

src/iterators.jl

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,8 @@ hreps(p::HAffineSpace{N, T}...) where {N, T} = tuple(hyperplanes(p...))
303303
hmap(f, d::FullDim, ::Type{T}, p::HRep...) where T = maphyperplanes(f, d, T, p...), maphalfspaces(f, d, T, p...)
304304
hmap(f, d::FullDim, ::Type{T}, p::HAffineSpace...) where T = tuple(maphyperplanes(f, d, T, p...))
305305

306-
function hconvert(::Type{RepTout}, p::HRep{N, T}) where {N, T, RepTout<:HRep{N, T}}
307-
RepTout(hreps(p)...)
308-
end
309-
function hconvert(::Type{RepTout}, p::HRep{N}) where {N, T, RepTout<:HRep{N, T}}
310-
RepTout(RepIterator{N, T}.(hreps(p))...)
311-
end
306+
hconvert(RepT::Type{<:HRep{N, T}}, p::HRep{N, T}) where {N, T} = constructpolyhedron(RepT, (p,), hreps(p)...)
307+
hconvert(RepT::Type{<:HRep{N, T}}, p::HRep{N}) where {N, T} = constructpolyhedron(RepT, (p,), RepIterator{N, T}.(hreps(p))...)
312308

313309
vreps(p...) = preps(p...)..., rreps(p...)...
314310
preps(p::VRep...) = tuple(points(p...))
@@ -324,9 +320,5 @@ rmap(f, d::FullDim, ::Type{T}, p::VRep...) where T = maplines(f, d, T, p...), ma
324320
rmap(f, d::FullDim, ::Type{T}, p::VLinearSpace...) where T = tuple(maplines(f, d, T, p...))
325321
rmap(f, d::FullDim, ::Type, p::VPolytope...) = tuple()
326322

327-
function vconvert(::Type{RepTout}, p::VRep{N, T}) where {N, T, RepTout<:VRep{N, T}}
328-
RepTout(vreps(p)...)
329-
end
330-
function vconvert(::Type{RepTout}, p::VRep{N}) where {N, T, RepTout<:VRep{N, T}}
331-
RepTout(RepIterator{N, T}.(vreps(p))...)
332-
end
323+
vconvert(RepT::Type{<:VRep{N, T}}, p::VRep{N, T}) where {N, T} = constructpolyhedron(RepT, (p,), vreps(p)...)
324+
vconvert(RepT::Type{<:VRep{N, T}}, p::VRep{N}) where {N, T} = constructpolyhedron(RepT, (p,), RepIterator{N, T}.(vreps(p))...)

src/representation.jl

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -57,59 +57,36 @@ checkvconsistency(p::Polyhedron) = vrepiscomputed(p) && checkvconsistency(vrep(p
5757
Base.convert(::Type{T}, p::T) where {T<:HRepresentation} = p
5858
Base.convert(::Type{T}, p::T) where {T<:VRepresentation} = p
5959

60-
Base.convert(::Type{RepTout}, p::HRepresentation) where RepTout<:HRep = hconvert(RepTout, p)
61-
Base.convert(::Type{RepTout}, p::HRep) where {RepTout<:HRepresentation} = hconvert(RepTout, p)
60+
Base.convert(RepT::Type{<:HRep}, p::HRepresentation) = hconvert(RepT, p)
61+
Base.convert(RepT::Type{<:HRepresentation}, p::HRep) = hconvert(RepT, p)
6262
# avoid ambiguity
63-
Base.convert(::Type{RepTout}, p::HRepresentation) where {RepTout<:HRepresentation} = hconvert(RepTout, p)
63+
Base.convert(RepT::Type{<:HRepresentation}, p::HRepresentation) = hconvert(RepT, p)
6464

6565
Base.copy(rep::HRepresentation) = typeof(rep)(hreps(rep)...)
6666
Base.copy(rep::VRepresentation) = typeof(rep)(vreps(rep)...)
6767

6868
function Polyhedron{N, S}(p::Polyhedron{N, T}) where {N, S, T}
69-
RepTout = similar_type(typeof(p), S)
69+
RepT = similar_type(typeof(p), S)
7070
if !hrepiscomputed(p) && vrepiscomputed(p)
71-
vconvert(RepTout, p)
71+
vconvert(RepT, p)
7272
else
73-
hconvert(RepTout, p)
73+
hconvert(RepT, p)
7474
end
7575
end
7676

77-
#function vconvert{RepTout<:VRep, RepTin<:VRep}(::Type{RepTout}, p::RepTin)
78-
# Nin = fulldim(RepTin)
79-
# Nout = fulldim(RepTout)
80-
# if Nin != Nout
81-
# error("Different dimension")
82-
# end
83-
# Tin = eltype(RepTin)
84-
# Tout = eltype(RepTout)
85-
# if Tin == Tout
86-
# f = nothing
87-
# else
88-
# f = (i,x) -> similar_type(typeof(x), Tout)(x)
89-
# end
90-
# if decomposedvfast(p)
91-
# RepTout(PointIterator{Nout,Tout,Nin,Tin}([p], f), RayIterator{Nout,Tout,Nin,Tin}([p], f))
92-
# else
93-
# RepTout(VRepIterator{Nout,Tout,Nin,Tin}([p], f))
94-
# end
95-
#end
96-
97-
Base.convert(::Type{RepTout}, p::VRepresentation) where {RepTout<:VRep} = vconvert(RepTout, p)
98-
Base.convert(::Type{RepTout}, p::VRep) where {RepTout<:VRepresentation} = vconvert(RepTout, p)
77+
Base.convert(RepT::Type{<:VRep}, p::VRepresentation) = vconvert(RepT, p)
78+
Base.convert(RepT::Type{<:VRepresentation}, p::VRep) = vconvert(RepT, p)
9979
# avoid ambiguity
100-
Base.convert(::Type{RepTout}, p::VRepresentation) where {RepTout<:VRepresentation} = vconvert(RepTout, p)
80+
Base.convert(RepT::Type{<:VRepresentation}, p::VRepresentation) = vconvert(RepT, p)
10181

10282
# Used by SimpleVRepPolyhedraModel
10383
Base.convert(::Type{VRep}, p::VRepresentation) = p
10484

10585
MultivariatePolynomials.changecoefficienttype(p::Rep{N,T}, ::Type{T}) where {N,T} = p
106-
function MultivariatePolynomials.changecoefficienttype(p::RepTin, ::Type{Tout}) where {RepTin<:Rep, Tout}
107-
RepTout = similar_type(RepTin, Tout)
108-
RepTout(p)
109-
end
86+
MultivariatePolynomials.changecoefficienttype(p::Rep, T::Type) = similar_type(typeof(p), T)(p)
11087

111-
VRepresentation{N, T}(v::RepTin) where {N, T, RepTin} = similar_type(RepTin, FullDim{N}(), T)(v)
112-
HRepresentation{N, T}(h::RepTin) where {N, T, RepTin} = similar_type(RepTin, FullDim{N}(), T)(h)
88+
VRepresentation{N, T}(v::VRepresentation) where {N, T} = similar_type(typeof(v), FullDim{N}(), T)(v)
89+
HRepresentation{N, T}(h::HRepresentation) where {N, T} = similar_type(typeof(h), FullDim{N}(), T)(h)
11390

11491
VRep{N, T}(v::VRepresentation) where {N, T} = VRepresentation{N, T}(v)
11592
VRep{N, T}(p::Polyhedron) where {N, T} = Polyhedron{N, T}(p)

0 commit comments

Comments
 (0)