Skip to content

Commit

Permalink
remove zero columns in zonotope constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Mar 23, 2019
1 parent 3653564 commit 31e3b85
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
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
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

0 comments on commit 31e3b85

Please sign in to comment.