Skip to content
This repository has been archived by the owner on Feb 12, 2021. It is now read-only.

Commit

Permalink
julia formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
kalmarek committed Jan 15, 2020
1 parent 0b8a0de commit d20bea5
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 179 deletions.
34 changes: 20 additions & 14 deletions src/HomogeneousPolyhedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@
###############################################################################
###############################################################################
function Base.show(io::IO, H::HomogeneousPolyhedron)
if iscomputed(H, :INEQUALITIES)
print(io, "Homogeneous polyhedron given by { x | A x ≥ 0 } where \n")
print(io, "\nA = \n")
Base.print_array(io, H.polymakePolytope.INEQUALITIES)
else
println(io, "Homogeneous polyhedron defined as convex hull of vertices and rays.")
println(io, "The hyperplane description has not been computed yet.")
end
if iscomputed(H, :INEQUALITIES)
print(io, "Homogeneous polyhedron given by { x | A x ≥ 0 } where \n")
print(io, "\nA = \n")
Base.print_array(io, H.polymakePolytope.INEQUALITIES)
else
println(
io,
"Homogeneous polyhedron defined as convex hull of vertices and rays.",
)
println(io, "The hyperplane description has not been computed yet.")
end
end

function ==(H0::HomogeneousPolyhedron, H1::HomogeneousPolyhedron)
return polytope.included_polyhedra(H0.polymakePolytope, H1.polymakePolytope) &&
polytope.included_polyhedra(H1.polymakePolytope, H0.polymakePolytope)
return polytope.included_polyhedra(
H0.polymakePolytope,
H1.polymakePolytope,
) && polytope.included_polyhedra(H1.polymakePolytope, H0.polymakePolytope)
end

###############################################################################
Expand All @@ -36,7 +41,8 @@ polytope.dim(H::HomogeneousPolyhedron) = polytope.dim(H.polymakePolytope)
Returns the ambient dimension of a polyhedron.
"""
polytope.ambient_dim(H::HomogeneousPolyhedron) = polytope.ambient_dim(H.polymakePolytope)
polytope.ambient_dim(H::HomogeneousPolyhedron) =
polytope.ambient_dim(H.polymakePolytope)

"""
vertices(H::HomogeneousPolyhedron)
Expand All @@ -50,7 +56,8 @@ vertices(H::HomogeneousPolyhedron) = transpose(H.polymakePolytope.VERTICES)
Returns a basis of the lineality space of a polyhedron in column-major format.
"""
lineality_space(H::HomogeneousPolyhedron) = transpose(H.polymakePolytope.LINEALITY_SPACE)
lineality_space(H::HomogeneousPolyhedron) =
transpose(H.polymakePolytope.LINEALITY_SPACE)

"""
facets(H::HomogeneousPolyhedron)
Expand All @@ -68,6 +75,5 @@ facets(H::HomogeneousPolyhedron) = transpose(H.polymakePolytope.FACETS)
homogeneous_cube(d [, u, l])
Construct the $[-1,1]$-cube in dimension $d$. If $u$ and $l$ are given, the $[l,u]$-cube in dimension $d$ is returned.
"""
homogeneous_cube(d) = HomogeneousPolyhedron(polytope.cube(d))
""" homogeneous_cube(d) = HomogeneousPolyhedron(polytope.cube(d))
homogeneous_cube(d, u, l) = HomogeneousPolyhedron(polytope.cube(d, u, l))
4 changes: 2 additions & 2 deletions src/LinearProgram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Constructs the primal linear program max{cx | Ax<= b}.
see Def. 4.10
"""
primal_program(c, A, b) = LinearProgram(Polyhedron(A,b), c)
primal_program(c, A, b) = LinearProgram(Polyhedron(A, b), c)

"""
dual_program(c, A, b)
Expand All @@ -14,7 +14,7 @@ Constructs the dual linear program min{yb | yA=c, y>=0}.
see Def. 4.10
"""
dual_program(c, A, b) = LinearProgram(dual_polyhedron(A,c), b)
dual_program(c, A, b) = LinearProgram(dual_polyhedron(A, c), b)

minimal_vertex(lp::LinearProgram) = dehomogenize(lp.polymake_lp.MINIMAL_VERTEX)
maximal_vertex(lp::LinearProgram) = dehomogenize(lp.polymake_lp.MAXIMAL_VERTEX)
Expand Down
110 changes: 20 additions & 90 deletions src/OscarPolytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,101 +7,31 @@ const polytope = Polymake.polytope

import Polymake.polytope: ambient_dim, dim

export Polyhedron, HomogeneousPolyhedron, LinearProgram,
ambient_dim, dim, facets, lineality_space, rays, vertices, convex_hull,
primal_program, dual_program, dual_polyhedron,
minimal_vertex, minimal_value, maximal_vertex, maximal_value,
augment, homogenize, dehomogenize
export Polyhedron,
HomogeneousPolyhedron,
LinearProgram,
ambient_dim,
augment,
convex_hull,
dehomogenize,
dim,
dual_program,
dual_polyhedron,
facets,
homogenize,
lineality_space,
maximal_vertex,
maximal_value,
minimal_vertex,
minimal_value,
primal_program,
rays,
vertices

include("types.jl")
include("HomogeneousPolyhedron.jl")
include("Polyhedron.jl")
include("helpers.jl")
include("LinearProgram.jl")

#here BigInt, Integer, (fmpz, fmpq) -> Rational
# nf_elem quad real field: -> QuadraticExtension
# float -> Float
# mpfr, BigFloat -> AccurateFloat




#
# #we don't have points yet, so I can only return the matrix.
# # the polyhedron is not homogeneous, so I strip the 1st entry
# #TODO: since P is given by Ax <= b, should the "points" be rows or columns?
#
# @doc Markdown.doc"""
# boundary_points_matrix(P::Polyhedron) -> fmpz_mat
# > Given a bounded polyhedron, return the coordinates of all integer points
# > its boundary as rows in a matrix.
# """
# function boundary_points_matrix(P::Polyhedron)
# p = _polytope(P)
# out = Polymake.give(p, "BOUNDARY_LATTICE_POINTS")
# res = zero_matrix(FlintZZ, rows(out), cols(out)-1)
# for i=1:rows(out)
# @assert out[i,1] == 1
# for j=1:cols(out)-1
# res[i,j] = out[i, j+1]
# end
# end
# return res
# end
#
# @doc Markdown.doc"""
# inner_points_matrix(P::Polyhedron) -> fmpz_mat
# > Given a bounded polyhedron, return the coordinates of all integer points
# > in its interior as rows in a matrix.
# """
# function inner_points_matrix(P::Polyhedron)
# p = _polytope(P)
# out = Polymake.give(p, "INTERIOR_LATTICE_POINTS")
# res = zero_matrix(FlintZZ, rows(out), cols(out)-1)
# for i=1:rows(out)
# @assert out[i,1] == 1
# for j=1:cols(out)-1
# res[i,j] = out[i, j+1]
# end
# end
# return res
# end

#=
function polyhedron(V::Array{Point, 1}, C::Array{Point, 1}=Array{Point, 1}(UndefInitializer(), 0), L::Array{Point, 1} = Array{Point, 1}(UndefInitializer(), 0))# only for Homogeneous version; Other::Dict{String, Any} = Dict{String, Any}())
d = Dict{String, Any}("INEQUALITIES" => bA)
# for (k,v) = Other
# d[k] = v
# end
# POINTS = [ 1 V; 0 C], INPUT_LINEALITIES = L
p = Polymake.perlobj("Polytope<Rational>", d)
P = Polyhedron(t)
P.P = p
return P
end
function polyhedron(V::MatElem, C::MatElem, L::MatElem)
end
function convex_hull(P::polyhedron, Q::Polyhedron)
end
function convex_hull(P::polyhedron, Q::Point)
end
+(P, Q)
intersect(P, Q)
product
join
free_sum
lattice_points
isbounded
isunbounded
lattice_points_generators
volume
isempty
=#
end # module
73 changes: 48 additions & 25 deletions src/Polyhedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@ The polytope given as the convex hull of the columns of V. Optionally, rays (R)
and generators of the lineality space (L) can be given as well.
see Def. 2.11 and Def. 3.1.
"""
function convex_hull(V::AbstractVecOrMat)
p = polytope.Polytope{Rational}(:POINTS=>homogenize(transpose(V), 1))
""" function convex_hull(V::AbstractVecOrMat)
p = polytope.Polytope{Rational}(POINTS = homogenize(transpose(V), 1))
return Polyhedron(HomogeneousPolyhedron(p))
end
function convex_hull(V::AbstractVecOrMat, R::AbstractVecOrMat)
p = polytope.Polytope{Rational}(:POINTS=>vcat(homogenize(transpose(V), 1), homogenize(transpose(R), 0)))
p = polytope.Polytope{Rational}(POINTS = vcat(
homogenize(transpose(V), 1),
homogenize(transpose(R), 0),
))
return Polyhedron(HomogeneousPolyhedron(p))
end
function convex_hull(V::AbstractVecOrMat, R::AbstractVecOrMat, L::AbstractVecOrMat)
p = polytope.Polytope{Rational}(:POINTS=>vcat(homogenize(transpose(V), 1), homogenize(transpose(R), 0)), :INPUT_LINEALITY=>homogenize(transpose(L),0))
function convex_hull(
V::AbstractVecOrMat,
R::AbstractVecOrMat,
L::AbstractVecOrMat,
)
p = polytope.Polytope{Rational}(
POINTS = vcat(homogenize(transpose(V), 1), homogenize(transpose(R), 0)),
INPUT_LINEALITY = homogenize(transpose(L), 0),
)
return Polyhedron(HomogeneousPolyhedron(p))
end
###############################################################################
Expand All @@ -29,16 +38,22 @@ end
###############################################################################
function Base.show(io::IO, P::Polyhedron)
if iscomputed(P, :VERTICES)
println(io, "Polyhedron given as the convex hull of the columns of V, where\nV = ")
println(
io,
"Polyhedron given as the convex hull of the columns of V, where\nV = ",
)
Base.print_array(io, vertices(P))
R = rays(P)
if size(R, 2) > 0
println(io, "\n\nwith rays given as the columns of R, where\nR =")
Base.print_array(io, R)
end
L = lineality_space(P)
if size(L,2) > 0
println(io, "\n\nwith lineality space minimally generated by the columns of L, where\nL =")
if size(L, 2) > 0
println(
io,
"\n\nwith lineality space minimally generated by the columns of L, where\nL =",
)
Base.print_array(io, L)
end
return
Expand All @@ -48,9 +63,12 @@ function Base.show(io::IO, P::Polyhedron)
println(io, "\nA = ")
Base.print_array(io, -dehomogenize(ineq))
println(io, "\n\nb = ")
Base.print_array(io, ineq[:,1])
Base.print_array(io, ineq[:, 1])
else
println(io, "A Polyhedron with neither vertex nor face representation computed.")
println(
io,
"A Polyhedron with neither vertex nor face representation computed.",
)
end
end

Expand Down Expand Up @@ -88,7 +106,7 @@ end
Returns minimal set of generators of the cone of unbounded directions of a polyhedron in column-major format.
"""
function rays(P::Polyhedron)
function rays(P::Polyhedron)
verts_in_rows = transpose(vertices(P.homogeneous_polyhedron))
return transpose(dehomogenize(verts_in_rows, ray_indices(verts_in_rows)))
end
Expand All @@ -100,15 +118,19 @@ Returns a basis of the lineality space of a polyhedron in column-major format.
"""
function lineality_space(P::Polyhedron)
lineality_in_rows = transpose(lineality_space(P.homogeneous_polyhedron))
return transpose(dehomogenize(lineality_in_rows, ray_indices(lineality_in_rows)))
return transpose(dehomogenize(
lineality_in_rows,
ray_indices(lineality_in_rows),
))
end

"""
facets(P::Polyhedron)
Returns the facets of a polyhedron in column-major format.
"""
facets(P::Polyhedron) = decompose_hdata(transpose(facets(P.homogeneous_polyhedron)))
facets(P::Polyhedron) =
decompose_hdata(transpose(facets(P.homogeneous_polyhedron)))

###############################################################################
###############################################################################
Expand All @@ -123,20 +145,21 @@ The (metric) polyhedron defined by
$$P^*(A,c) = \{ y | yA = c, y ≥ 0 \}.$$
see Theorem 4.11.
"""
function dual_polyhedron(A, c)
m, n = size(A)
cA = transpose([c'; -LinearAlgebra.transpose(A)])
nonnegative = [zeros(eltype(A), n, 1) LinearAlgebra.I]
P_star = polytope.Polytope{Rational}(EQUATIONS=cA, INEQUALITIES=nonnegative)
H = HomogeneousPolyhedron(P_star)
return Polyhedron(H)
""" function dual_polyhedron(A, c)
m, n = size(A)
cA = transpose([c'; -LinearAlgebra.transpose(A)])
nonnegative = [zeros(eltype(A), n, 1) LinearAlgebra.I]
P_star = polytope.Polytope{Rational}(
EQUATIONS = cA,
INEQUALITIES = nonnegative,
)
H = HomogeneousPolyhedron(P_star)
return Polyhedron(H)
end

@doc Markdown.doc"""
cube(d [, u, l])
Construct the $[-1,1]$-cube in dimension $d$. If $u$ and $l$ are given, the $[l,u]$-cube in dimension $d$ is returned.
"""
cube(d) = Polyhedron(homogeneous_cube(d))
cube(d, u, l) = Polyhedron(homogeneous_cube(d,u,l))
""" cube(d) = Polyhedron(homogeneous_cube(d))
cube(d, u, l) = Polyhedron(homogeneous_cube(d, u, l))
Loading

0 comments on commit d20bea5

Please sign in to comment.