Skip to content

Commit

Permalink
projection of CartesianProduct
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Feb 26, 2021
1 parent 412eab1 commit 1f12e81
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/LazyOperations/CartesianProduct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,26 @@ function concretize(cp::CartesianProduct)
return cartesian_product(concretize(cp.X), concretize(cp.Y))
end

function project(cp::CartesianProduct, block::AbstractVector{Int})
res = _project_cp_same_block(cp, block)
if res == nothing
res = invoke(project, Tuple{LazySet, typeof(block)}, cp, block)
end
return res
end

function _project_cp_same_block(cp, block)
min, max = extrema(block)
n1 = dim(cp.X)
if max <= n1
return project(cp.X, block)
elseif min > n1
return project(cp.Y, block .- n1)
else
return nothing
end
end

"""
project(cp::CartesianProduct{N, IT, HT}, block::AbstractVector{Int}) where {N, IT<:Interval, HT<:AbstractHyperrectangle{N}}
Expand Down
15 changes: 15 additions & 0 deletions test/unit_CartesianProduct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ for N in [Float64, Float32, Rational{Int}]
res, w = (cp2, cp1, true)
@test cp2 cp1 && !res && w cp2 && w cp1

# projection
P = Singleton(N[11, 12])
Q = Singleton(N[13, 14, 15])
cp = P × Q
@test project(cp, [2]) == Singleton(N[12])
@test project(cp, [3, 5]) == Singleton(N[13, 15])

# ==================================
# Conversions of Cartesian Products
# ==================================
Expand Down Expand Up @@ -420,4 +427,12 @@ for N in [Float64]
Q = array(cap)[2]
@test ispermutation(constraints_list(Q), [HalfSpace(N[-1, 0], N(-1)),
HalfSpace(N[0, -1], N(-2)), HalfSpace(N[1, 1], N(3))])

if test_suite_polyhedra
# projection to mixed dimensions
P = Singleton(N[11, 12])
Q = Singleton(N[13, 14, 15])
cp = P × Q
@test isequivalent(project(cp, [1, 4]), Singleton(N[11, 14]))
end
end

0 comments on commit 1f12e81

Please sign in to comment.