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

Outsource HalfSpace to its own module #3591

Merged
merged 5 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions src/Initialization/init_SymEngine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,3 @@ function free_symbols(expr::Expr)
error("the free symbols for the expression $(expr) is not implemented")
end
end

eval(load_symengine_halfspace())
2 changes: 0 additions & 2 deletions src/Initialization/init_Symbolics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ _get_variables(expr::Num) = convert(Vector{Num}, Symbolics.get_variables(expr))
function _get_variables(expr::Vector{<:Num})
return unique(reduce(vcat, _get_variables(ex) for ex in expr))
end

eval(load_symbolics_halfspace())
47 changes: 46 additions & 1 deletion src/Interfaces/AbstractPolyhedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,50 @@ abstract type AbstractPolyhedron{N} <: ConvexSet{N} end
# supertype for all linear map algorithms
abstract type AbstractLinearMapAlgorithm end

# To account for the compilation order, functions are defined in the file
"""
constrained_dimensions(P::AbstractPolyhedron)

Return the indices in which a polyhedron is constrained.

### Input

- `P` -- polyhedron

### Output

A vector of ascending indices `i` such that the polyhedron is constrained in
dimension `i`.

### Examples

A 2D polyhedron with constraint ``x1 ≥ 0`` is constrained in dimension 1 only.
"""
function constrained_dimensions(P::AbstractPolyhedron)
constraints = constraints_list(P)
if isempty(constraints)
return Int[]
end
zero_indices = zeros(Int, dim(P))
for constraint in constraints
for i in constrained_dimensions(constraint)
zero_indices[i] = i
end
end
return filter(x -> x != 0, zero_indices)
end

# generic function for the AbstractPolyhedron interface => returns an HPolyhedron
function _linear_map_hrep_helper(M::AbstractMatrix, P::LazySet,
algo::AbstractLinearMapAlgorithm)
constraints = _linear_map_hrep(M, P, algo)
return HPolyhedron(constraints)
end

# internal function; defined here due to dependency SymEngine and submodules
function _is_halfspace() end

# internal function; defined here due to dependency SymEngine and submodules
function _is_hyperplane() end

# To account for the compilation order, other functions are defined in the file
# AbstractPolyhedron_functions.jl
45 changes: 0 additions & 45 deletions src/Interfaces/AbstractPolyhedron_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,38 +95,6 @@ function isuniversal(P::AbstractPolyhedron{N}, witness::Bool=false) where {N}
end
end

"""
constrained_dimensions(P::AbstractPolyhedron)

Return the indices in which a polyhedron is constrained.

### Input

- `P` -- polyhedron

### Output

A vector of ascending indices `i` such that the polyhedron is constrained in
dimension `i`.

### Examples

A 2D polyhedron with constraint ``x1 ≥ 0`` is constrained in dimension 1 only.
"""
function constrained_dimensions(P::AbstractPolyhedron)
constraints = constraints_list(P)
if isempty(constraints)
return Int[]
end
zero_indices = zeros(Int, dim(P))
for constraint in constraints
for i in constrained_dimensions(constraint)
zero_indices[i] = i
end
end
return filter(x -> x != 0, zero_indices)
end

"""
tosimplehrep(constraints::AbstractVector{LC};
[n]::Int=0) where {N, LC<:HalfSpace{N}}
Expand Down Expand Up @@ -694,13 +662,6 @@ function _linear_map_vrep(M::AbstractMatrix, P::AbstractPolyhedron,
return _linear_map_vrep(M, P, algo; apply_convex_hull=apply_convex_hull)
end

# generic function for the AbstractPolyhedron interface => returns an HPolyhedron
function _linear_map_hrep_helper(M::AbstractMatrix, P::LazySet,
algo::AbstractLinearMapAlgorithm)
constraints = _linear_map_hrep(M, P, algo)
return HPolyhedron(constraints)
end

# preconditions should have been checked in the caller function
function _linear_map_hrep(M::AbstractMatrix, P::LazySet, algo::LinearMapInverse)
return _affine_map_inverse_hrep(algo.inverse, P)
Expand Down Expand Up @@ -1289,9 +1250,3 @@ Determine whether a polyhedron is equivalent to a hyperplane.
``a·x ≤ b`` and ``-a·x ≤ -b``.
"""
function is_hyperplanar(::AbstractPolyhedron) end

# internal function; defined here due to dependency SymEngine and submodules
function _is_halfspace() end

# internal function; defined here due to dependency SymEngine and submodules
function _is_hyperplane() end
9 changes: 8 additions & 1 deletion src/LazySets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ include("Interfaces/ConvexSet.jl")
include("Interfaces/AbstractPolynomialZonotope.jl")
include("Interfaces/AbstractSparsePolynomialZonotope.jl")
include("Interfaces/AbstractPolyhedron.jl")
include("Sets/HalfSpace.jl") # must come before AbstractPolyhedron_functions

include("Sets/HalfSpace/HalfSpaceModule.jl") # must come before AbstractPolyhedron_functions
@reexport using ..HalfSpaceModule: HalfSpace, LinearConstraint,
halfspace_left, halfspace_right,
iscomplement
using ..HalfSpaceModule: is_tighter_same_dir_2D, _non_element_halfspace,
_normalize_halfspace, _normal_Vector

include("Interfaces/AbstractPolyhedron_functions.jl")
include("Interfaces/AbstractPolytope.jl")
include("Interfaces/AbstractCentrallySymmetric.jl")
Expand Down
Loading
Loading