Skip to content

Commit

Permalink
Merge pull request #2963 from JuliaReach/schillic/2947
Browse files Browse the repository at this point in the history
center for lazy operations
  • Loading branch information
schillic authored Apr 15, 2022
2 parents 8a09f87 + 6534b3f commit f390b6c
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/lib/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ an_element(::AbstractAffineMap)
isempty(::AbstractAffineMap)
isbounded(::AbstractAffineMap)
∈(::AbstractVector, ::AbstractAffineMap)
center(::AbstractAffineMap)
vertices_list(::AbstractAffineMap)
constraints_list(::AbstractAffineMap)
linear_map(::AbstractMatrix, ::AbstractAffineMap)
Expand Down
2 changes: 2 additions & 0 deletions docs/src/lib/lazy_operations/CartesianProduct.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dim(::CartesianProduct)
isbounded(::CartesianProduct)
∈(::AbstractVector, ::CartesianProduct)
isempty(::CartesianProduct)
center(::CartesianProduct)
constraints_list(::CartesianProduct)
vertices_list(cp::CartesianProduct{N}) where {N}
linear_map(M::AbstractMatrix, cp::CartesianProduct)
Expand All @@ -41,6 +42,7 @@ dim(::CartesianProductArray)
isbounded(::CartesianProductArray)
∈(::AbstractVector, ::CartesianProductArray)
isempty(::CartesianProductArray)
center(::CartesianProductArray)
constraints_list(cpa::CartesianProductArray{N}) where {N}
vertices_list(::CartesianProductArray{N}) where {N}
linear_map(M::AbstractMatrix, cpa::CartesianProductArray)
Expand Down
2 changes: 2 additions & 0 deletions docs/src/lib/lazy_operations/MinkowskiSum.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dim(::MinkowskiSum)
σ(::AbstractVector, ::MinkowskiSum)
isbounded(::MinkowskiSum)
isempty(::MinkowskiSum)
center(::MinkowskiSum)
constraints_list(::MinkowskiSum)
∈(::AbstractVector, ::MinkowskiSum{N, S1, S2}) where {N, S1<:AbstractSingleton, S2<:LazySet}
vertices_list(::MinkowskiSum)
Expand All @@ -37,6 +38,7 @@ dim(::MinkowskiSumArray)
isbounded(::MinkowskiSumArray)
isempty(::MinkowskiSumArray)
array(::MinkowskiSumArray)
center(::MinkowskiSumArray)
```
Inherited from [`LazySet`](@ref):
* [`norm`](@ref norm(::LazySet, ::Real))
Expand Down
17 changes: 17 additions & 0 deletions src/Interfaces/AbstractAffineMap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,23 @@ function ∈(x::AbstractVector, am::AbstractAffineMap)
return matrix(am) \ (x - vector(am)) set(am)
end

"""
center(am::AbstractAffineMap)
Return the center of an affine map of a centrally-symmetric set.
### Input
- `cp` -- affine map of a centrally-symmetric set
### Output
The center of the affine map.
"""
function center(am::AbstractAffineMap)
matrix(am) * center(set(am)) + vector(am)
end

"""
vertices_list(am::AbstractAffineMap; [apply_convex_hull]::Bool)
Expand Down
17 changes: 17 additions & 0 deletions src/LazyOperations/CartesianProduct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,23 @@ function isempty(cp::CartesianProduct)
return isempty(cp.X) || isempty(cp.Y)
end

"""
center(cp::CartesianProduct)
Return the center of a Cartesian product of centrally-symmetric sets.
### Input
- `cp` -- Cartesian product of centrally-symmetric sets
### Output
The center of the Cartesian product.
"""
function center(cp::CartesianProduct)
vcat(center(cp.X), center(cp.Y))
end

"""
constraints_list(cp::CartesianProduct)
Expand Down
17 changes: 17 additions & 0 deletions src/LazyOperations/CartesianProductArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,23 @@ function isempty(cpa::CartesianProductArray)
return any(isempty, array(cpa))
end

"""
center(cpa::CartesianProductArray)
Return the center of a Cartesian product array of centrally-symmetric sets.
### Input
- `cpa` -- Cartesian product array of centrally-symmetric sets
### Output
The center of the Cartesian product array.
"""
function center(cpa::CartesianProductArray)
reduce(vcat, center(X) for X in array(cpa))
end

"""
constraints_list(cpa::CartesianProductArray{N}) where {N}
Expand Down
17 changes: 17 additions & 0 deletions src/LazyOperations/MinkowskiSum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ function isempty(ms::MinkowskiSum)
return isempty(ms.X) || isempty(ms.Y)
end

"""
center(ms::MinkowskiSum)
Return the center of a Minkowski sum of centrally-symmetric sets.
### Input
- `ms` -- Minkowski sum of centrally-symmetric sets
### Output
The center of the Minkowski sum.
"""
function center(ms::MinkowskiSum)
center(ms.X) + center(ms.Y)
end

"""
constraints_list(ms::MinkowskiSum)
Expand Down
17 changes: 17 additions & 0 deletions src/LazyOperations/MinkowskiSumArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,23 @@ function isempty(msa::MinkowskiSumArray)
return any(isempty, array(msa))
end

"""
center(msa::MinkowskiSumArray)
Return the center of a Minkowski sum array of centrally-symmetric sets.
### Input
- `msa` -- Minkowski sum array of centrally-symmetric sets
### Output
The center of the Minkowski sum array.
"""
function center(msa::MinkowskiSumArray)
sum(center(X) for X in array(msa))
end

function concretize(msa::MinkowskiSumArray)
a = array(msa)
@assert !isempty(a) "an empty Minkowski sum is not allowed"
Expand Down
6 changes: 6 additions & 0 deletions test/LazyOperations/CartesianProduct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ for N in [Float64, Float32, Rational{Int}]
# isempty
@test !isempty(cp)

# center
@test center(cp) == N[0, 0, 0]

# Cartesian Product of a not-centered 1D BallInf and a not-centered 2D BallInf
# Here a Hyperrectangle where c = [1, -3, 4] and r = [3, 2, 2]
b1 = BallInf(N[1], N(3))
Expand Down Expand Up @@ -244,6 +247,9 @@ for N in [Float64, Float32, Rational{Int}]
@test ρ(dd, cpa) == ρ(ds, cpa) == dot(svec, dd)
end

# center
@test center(cpa) == N[1, 2, 3, 4]

# boundedness
@test isbounded(cpa) && isboundedtype(typeof(cpa))
cpa2 = CartesianProductArray([Singleton(N[1]), HalfSpace(N[1], N(1))])
Expand Down
2 changes: 2 additions & 0 deletions test/LazyOperations/LinearMap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ for N in [Float64, Rational{Int}, Float32]
@test σ(d, lm1) == N[-3, 0]
d = N[1, -1]
@test σ(d, lm1) == N[-1, 0]
# center
@test center(lm1) == N[-2, 1]

# 2D -> 1D Projection
b = BallInf(N[1, 2], N(1))
Expand Down
6 changes: 6 additions & 0 deletions test/LazyOperations/MinkowskiSum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ for N in [Float64, Rational{Int}, Float32]
v = σ(d, s)
@test v[2] == N(2)

# center
@test center(s) == N[0, 5]

# Sum of array of LazySet
# 2-elements
ms = MinkowskiSum(Singleton(N[1]), Singleton(N[2]))
Expand Down Expand Up @@ -147,6 +150,9 @@ for N in [Float64, Rational{Int}, Float32]
# isempty
@test !isempty(msa)

# center
@test center(msa) == N[0, 5]

# convert m-sum of m-sum array to m-sum array (#1678)
s = Singleton(N[1, 2, 3])
X = s MinkowskiSumArray([s, s])
Expand Down

0 comments on commit f390b6c

Please sign in to comment.