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

Better linear_map of AbstractZonotope for 1D output #3364

Merged
merged 1 commit into from
Aug 18, 2023
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
21 changes: 21 additions & 0 deletions src/Interfaces/AbstractZonotope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,27 @@ function linear_map(M::AbstractMatrix, Z::AbstractZonotope)
@assert dim(Z) == size(M, 2) "a linear map of size $(size(M)) cannot be " *
"applied to a set of dimension $(dim(Z))"

if size(M, 1) == 1
# yields only one generator
return _linear_map_zonotope_1D(M, Z)
else
return _linear_map_zonotope_nD(M, Z)
end
end

mforets marked this conversation as resolved.
Show resolved Hide resolved
function _linear_map_zonotope_1D(M::AbstractMatrix, Z::LazySet)
N = promote_type(eltype(M), eltype(Z))
c = M * center(Z)
gi = zero(N)
@inbounds for g in generators(Z)
for i in eachindex(g)
gi += M[1, i] * g[i]
end
end
return Zonotope(c, hcat(gi))
end

function _linear_map_zonotope_nD(M::AbstractMatrix, Z::LazySet)
c = M * center(Z)
gi = M * genmat(Z)
return Zonotope(c, gi)
Expand Down
4 changes: 4 additions & 0 deletions test/Sets/Zonotope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ for N in [Float64, Rational{Int}, Float32]
Z5 = scale(1 // 2, Z3)
@test Z5.center == N[0, 1]
@test Z5.generators == N[1//2 1//2 1//2 0; -1//2 1//2 0 1//2]
# 1D simplifies to 1 generator
M = N[1 1;]
Z6 = linear_map(M, Z3)
@test ngens(Z6) == 1 && genmat(Z6) == hcat(N[4])

# in-place linear map
Zin = convert(Zonotope, BallInf(zeros(N, 2), N(1)))
Expand Down