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

#1245 - Zonotope conversion from flat hyperrectangle contains zero generators #1246

Merged
merged 3 commits into from
Mar 23, 2019
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/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ end
an_element_helper
binary_search_constraints
cross_product(::AbstractMatrix{N}) where {N<:Real}
delete_zero_columns
dot_zero
get_radius!
isinvertible
Expand Down
18 changes: 16 additions & 2 deletions src/Zonotope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ ball in ``\\mathbb{R}^n`` by an affine transformation.
generators_list::AbstractVector{VN}
) where {N<:Real, VN<:AbstractVector{N}}`

The optional argument `remove_zero_generators` controls whether we remove zero
columns from the `generators` matrix.
This option is active by default.

### Examples

A two-dimensional zonotope with given center and set of generators:
Expand Down Expand Up @@ -87,12 +91,22 @@ julia> Z.generators
struct Zonotope{N<:Real} <: AbstractCentrallySymmetricPolytope{N}
center::AbstractVector{N}
generators::AbstractMatrix{N}

function Zonotope(center::AbstractVector{N}, generators::AbstractMatrix{N};
remove_zero_generators::Bool=true) where {N<:Real}
if remove_zero_generators
generators = delete_zero_columns(generators)
end
new{N}(center, generators)
end
end

# constructor from center and list of generators
Zonotope(center::AbstractVector{N}, generators_list::AbstractVector{VN}
Zonotope(center::AbstractVector{N}, generators_list::AbstractVector{VN};
remove_zero_generators::Bool=true
) where {N<:Real, VN<:AbstractVector{N}} =
Zonotope(center, hcat(generators_list...))
Zonotope(center, hcat(generators_list...);
remove_zero_generators=remove_zero_generators)


# --- AbstractCentrallySymmetric interface functions ---
Expand Down
43 changes: 43 additions & 0 deletions src/helper_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,46 @@ function subtypes(interface, concrete::Bool)
end
return sort(result, by=string)
end

"""
delete_zero_columns(A::AbstractMatrix)

Remove all columns that only contain zeros from a given matrix.

### Input

- `A` -- matrix
- `copy` -- (optional, default: `false`) flag to copy the matrix

### Output

A matrix.

If the input matrix `A` does not contain any zero column, we return `A` unless
schillic marked this conversation as resolved.
Show resolved Hide resolved
the option `copy` is set.
If the input matrix contains zero columns, we always return a copy if the option
`copy` is set and otherwise a `SubArray` via `@view`.
"""
function delete_zero_columns(A::AbstractMatrix, copy::Bool=false)
n = size(A, 2)
nonzero_columns = Vector{Int}()
sizehint!(nonzero_columns, n)
for i in 1:n
if !iszero(A[:, i])
push!(nonzero_columns, i)
end
end
if copy
if length(nonzero_columns) == n
return copy(A)
else
return A[:, nonzero_columns]
end
else
if length(nonzero_columns) == n
return A
else
return @view A[:, nonzero_columns]
end
end
end
10 changes: 10 additions & 0 deletions test/unit_Zonotope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ for N in [Float64, Rational{Int}, Float32]
d = N[0, -1]
@test σ(d, z) == N[2, 1]

# zero column in generators
g = zeros(N, 2, 5)
g[:, 3] = ones(N, 2)
g[1, 2] = N(2)
z = Zonotope(N[1, 2], g)
@test size(z.generators) == (2, 2)

# boundedness
@test isbounded(z)

Expand Down Expand Up @@ -100,6 +107,9 @@ for N in [Float64, Rational{Int}, Float32]
Z = convert(Zonotope, Hyperrectangle(N[2, 3], N[4, 5]))
@test Z.center == N[2, 3] && diag(Z.generators) == N[4, 5]
convert(Zonotope, BallInf(N[5, 3], N(2)))
# flat hyperrectangle
Z = convert(Zonotope, Hyperrectangle(N[2, 3], N[0, 0]))
@test Z.center == N[2, 3] && isempty(Z.generators)

# convert the cartesian product of two hyperrectangles to a zonotope
h1 = Hyperrectangle(N[1/2], N[1/2])
Expand Down
8 changes: 8 additions & 0 deletions test/unit_util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,13 @@ for _dummy_ in 1:1 # avoid global variable warnings
# modified dot product
@test isnan(dot(N[1, 0], N[Inf, -Inf]))
@test LazySets.dot_zero(N[1, 0], N[Inf, -Inf]) == N(Inf)

# remove zero columns
m = 3
n = 10
A = rand(N, m, n)
A[:, 1] = A[:, 5] = A[:, n] = zeros(N, m)
B = LazySets.delete_zero_columns(A)
@test size(B) == (m, n-3)
end
end