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

#817 - Add method to swap arguments in a lazy operation #819

Merged
merged 2 commits into from
Oct 15, 2018
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
1 change: 1 addition & 0 deletions docs/src/lib/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ isempty(::Intersection)
∈(::AbstractVector{Real}, ::Intersection{Real})
isempty_known(::Intersection)
set_isempty!(::Intersection, ::Bool)
swap(::Intersection)
use_precise_ρ
_projection
```
Expand Down
2 changes: 1 addition & 1 deletion src/Approximations/overapproximate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,5 @@ function overapproximate(cap::Intersection{N,
dir::AbstractDirections{N};
kwargs...
) where {N<:Real}
return overapproximate(cap.Y ∩ cap.X, dir; kwargs...)
return overapproximate(swap(cap), dir; kwargs...)
end
38 changes: 33 additions & 5 deletions src/Intersection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Base: isempty, ∈, ∩
export Intersection,
isempty_known,
set_isempty!,
swap,
use_precise_ρ,
IntersectionArray,
array
Expand Down Expand Up @@ -92,11 +93,14 @@ struct Intersection{N<:Real, S1<:LazySet{N}, S2<:LazySet{N}} <: LazySet{N}
cache::IntersectionCache

# default constructor with dimension check
function Intersection{N, S1, S2}(X::S1, Y::S2) where
function Intersection{N, S1, S2}(X::S1,
Y::S2;
cache::IntersectionCache=IntersectionCache()
) where
{N<:Real, S1<:LazySet{N}, S2<:LazySet{N}}
@assert dim(X) == dim(Y) "sets in an intersection must have the same " *
"dimension"
return new{N, S1, S2}(X, Y, IntersectionCache())
return new{N, S1, S2}(X, Y, cache)
end
end

Expand Down Expand Up @@ -150,6 +154,30 @@ function set_isempty!(cap::Intersection, isempty::Bool)
return set_isempty!(cap.cache, isempty)
end

"""
swap(cap::Intersection{N, S1, S2})::Intersection{N} where {N<:Real, S1, S2}

Return a new `Intersection` object with the arguments swapped.

### Input

- `cap` -- intersection of two convex sets

### Output

A new `Intersection` object with the arguments swapped.
The old cache is shared between the old and new objects.

### Notes

The advantage of using this function instead of manually swapping the arguments
is that the cache is shared.
"""
function swap(cap::Intersection{N, S1, S2}
)::Intersection{N} where {N<:Real, S1, S2}
return Intersection{N, S2, S1}(cap.Y, cap.X, cache=cap.cache)
end


# --- LazySet interface functions ---

Expand Down Expand Up @@ -351,7 +379,7 @@ function ρ(d::AbstractVector{N},
<:LazySet{N}};
algorithm::String="line_search",
kwargs...) where N<:Real
return ρ_helper(d, cap.Y ∩ cap.X, algorithm; kwargs...)
return ρ_helper(d, swap(cap), algorithm; kwargs...)
end

"""
Expand Down Expand Up @@ -407,7 +435,7 @@ end
function ρ(d::AbstractVector{N},
cap::Intersection{N, <:AbstractPolytope{N}, <:LazySet{N}};
kwargs...) where {N<:Real}
return ρ(d, cap.Y ∩ cap.X; kwargs...)
return ρ(d, swap(cap); kwargs...)
end

# disambiguation
Expand Down Expand Up @@ -436,7 +464,7 @@ function ρ(d::AbstractVector{N},
<:AbstractPolytope{N}};
algorithm::String="line_search",
kwargs...) where N<:Real
return ρ_helper(d, cap.Y ∩ cap.X, algorithm; kwargs...)
return ρ_helper(d, swap(cap), algorithm; kwargs...)
end

"""
Expand Down