Skip to content

Commit

Permalink
Merge pull request #2597 from JuliaReach/schillic/1209_cartesianproduct
Browse files Browse the repository at this point in the history
#1209 - Concrete projection of CartesianProduct
  • Loading branch information
schillic authored Feb 26, 2021
2 parents e1c2ffa + 12678e5 commit a7e6cdc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Interfaces/LazySet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,13 @@ We apply the function `linear_map`.
::Nothing=nothing,
n::Int=dim(S)
) where {N}
return _project_linear_map(S, block, n)
end

@inline function _project_linear_map(S::LazySet{N},
block::AbstractVector{Int},
n::Int=dim(S)
) where {N}
M = projection_matrix(block, n, N)
return linear_map(M, S)
end
Expand Down
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 = _project_linear_map(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 a7e6cdc

Please sign in to comment.