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

#56 - Add apply for maps #57

Merged
merged 3 commits into from
Feb 2, 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
8 changes: 7 additions & 1 deletion docs/src/lib/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ nextinput
```@docs
outputdim
outputmap
```
```

## Maps

```@docs
apply
```
3 changes: 2 additions & 1 deletion src/MathematicalSystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export outputmap,
ConstrainedLinearControlMap,
AffineControlMap,
ConstrainedAffineControlMap,
ResetMap
ResetMap,
apply

include("maps.jl")

Expand Down
7 changes: 7 additions & 0 deletions src/abstract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,10 @@ function outputdim end
Returns the output map of a system with output.
"""
function outputmap end

"""
apply(m::AbstractMap, args...)

Apply the rule specified by the map to the given arguments.
"""
function apply end
15 changes: 15 additions & 0 deletions src/maps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct IdentityMap <: AbstractMap
dim::Int
end
outputdim(m::IdentityMap) = m.dim
apply(m::IdentityMap, x) = x

"""
LinearMap
Expand All @@ -31,6 +32,7 @@ struct LinearMap{T, MT<:AbstractMatrix{T}} <: AbstractMap
A::MT
end
outputdim(m::LinearMap) = size(m.A, 1)
apply(m::LinearMap, x) = m.A * x

@static if VERSION < v"0.7-"
LinearMap{T, MT <: AbstractMatrix{T}}(A::MT) = LinearMap{T, MT}(A)
Expand Down Expand Up @@ -58,6 +60,7 @@ struct AffineMap{T, MT<:AbstractMatrix{T}, VT<:AbstractVector{T}} <: AbstractMap
end
end
outputdim(m::AffineMap) = length(m.b)
apply(m::AffineMap, x) = m.A * x + m.b

"""
LinearControlMap
Expand All @@ -81,6 +84,7 @@ struct LinearControlMap{T, MTA<:AbstractMatrix{T}, MTB<:AbstractMatrix{T}} <: Ab
end
end
outputdim(m::LinearControlMap) = size(m.A, 1)
apply(m::LinearControlMap, x, u) = m.A * x + m.B * u

"""
ConstrainedLinearControlMap
Expand All @@ -106,6 +110,7 @@ struct ConstrainedLinearControlMap{T, MTA <: AbstractMatrix{T}, MTB <: AbstractM
end
end
outputdim(m::ConstrainedLinearControlMap) = size(m.A, 1)
apply(m::ConstrainedLinearControlMap, x, u) = m.A * x + m.B * u

"""
AffineControlMap
Expand All @@ -131,6 +136,7 @@ struct AffineControlMap{T, MTA <: AbstractMatrix{T}, MTB <: AbstractMatrix{T}, V
end
end
outputdim(m::AffineControlMap) = size(m.A, 1)
apply(m::AffineControlMap, x, u) = m.A * x + m.B * u + m.c

"""
ConstrainedAffineControlMap
Expand Down Expand Up @@ -158,6 +164,7 @@ struct ConstrainedAffineControlMap{T, MTA<:AbstractMatrix{T}, MTB<:AbstractMatri
end
end
outputdim(m::ConstrainedAffineControlMap) = size(m.A, 1)
apply(m::ConstrainedAffineControlMap, x, u) = m.A * x + m.B * u + m.c

"""
ResetMap
Expand All @@ -182,3 +189,11 @@ end
outputdim(m::ResetMap) = m.dim

ResetMap(dim::Int, args::Pair{Int, <:N}...) where {N} = ResetMap(dim, Dict{Int, N}(args))

function apply(m::ResetMap, x)
y = copy(x)
for (index, value) in pairs(m.dict)
y[index] = value
end
return y
end
35 changes: 35 additions & 0 deletions test/maps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import MathematicalSystems.LinearMap
@testset "Identity map" begin
m = IdentityMap(5)
@test outputdim(m) == 5
@test apply(m, ones(2)) == ones(2)
end

@testset "Linear map" begin
A = [1. 1; 1 -1]
m = LinearMap(A)
@test outputdim(m) == 2

# applying the linear map on a vector
@test apply(m, ones(2)) == [2.0, 0.0]

# applying the linear map on a set
X = BallInf(zeros(2), 1.0)
@test apply(m, X) == LazySets.LinearMap(A, X)
end

@testset "Affine map" begin
Expand All @@ -18,6 +26,9 @@ end
m = AffineMap(A, b)
@test outputdim(m) == 2

# applying the affine map on a vector
@test apply(m, ones(2)) == [2.5, 0.5]

A = [1. 1.]
b = [0.5]
m = AffineMap(A, b)
Expand All @@ -29,6 +40,11 @@ end
B = Matrix([0.5 1.5]')
m = LinearControlMap(A, B)
@test outputdim(m) == 2

# applying the affine map on a vector
x = ones(2)
u = [1/2]
@test apply(m, x, u) == A * x + B * u
end

@testset "Constrained linear control map" begin
Expand All @@ -37,6 +53,11 @@ end
U = Interval(-1, 1)
m = ConstrainedLinearControlMap(A, B, U)
@test outputdim(m) == 2

# applying the affine map on a vector
x = ones(2)
u = [1/2]
@test apply(m, x, u) == A*x + B*u
end

@testset "Affine control map" begin
Expand All @@ -45,6 +66,11 @@ end
c = [0.5, 0.5]
m = AffineControlMap(A, B, c)
@test outputdim(m) == 2

# applying the affine map on a vector
x = ones(2)
u = [1/2]
@test apply(m, x, u) == A*x + B*u + c
end

@testset "Constrained affine control map" begin
Expand All @@ -54,6 +80,11 @@ end
U = Interval(-1, 1)
m = ConstrainedAffineControlMap(A, B, c, U)
@test outputdim(m) == 2

# applying the affine map on a vector
x = ones(2)
u = [1/2]
@test apply(m, x, u) == A * x + B * u + c
end

@testset "Reset map" begin
Expand All @@ -62,4 +93,8 @@ end

m = ResetMap(10, 2 => -1., 5 => 1.)
@test outputdim(m) == 10

# applying the affine map on a vector
x = zeros(10)
@test apply(m, x) == [0, -1., 0, 0, 1., 0, 0, 0, 0, 0]
end