Skip to content

Commit

Permalink
Merge pull request #472 from JuliaReach/schillic/451
Browse files Browse the repository at this point in the history
#451 - Revise type parameters of LinearMap
  • Loading branch information
schillic authored Aug 5, 2018
2 parents 0856635 + cd6d981 commit 3e12d52
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 40 deletions.
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ formulation:

```jldoctest index_label
julia> typeof(Y)
LazySets.ConvexHull{Float64,LazySets.MinkowskiSum{Float64,LazySets.ExponentialMap{Float64,LazySets.Ball2{Float64}},LazySets.LinearMap{Float64,Float64}},LazySets.Ball2{Float64}}
LazySets.ConvexHull{Float64,LazySets.MinkowskiSum{Float64,LazySets.ExponentialMap{Float64,LazySets.Ball2{Float64}},LazySets.LinearMap{Float64,LazySets.BallInf{Float64},Float64,Array{Float64,2}}},LazySets.Ball2{Float64}}
```

Now suppose that we are interested in observing the projection of $\mathcal{Y}$
Expand Down
8 changes: 4 additions & 4 deletions docs/src/lib/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ Inherited from [`LazySet`](@ref):

```@docs
LinearMap
*(::AbstractMatrix, ::LazySet)
*(::Real, ::LazySet)
*(::AbstractMatrix{Real}, ::LazySet{Real})
*(::Real, ::LazySet{Real})
dim(::LinearMap)
σ(::AbstractVector{Real}, ::LinearMap{Real, Real})
∈(::AbstractVector{Real}, ::LinearMap{Real, Real})
σ(::AbstractVector{Real}, ::LinearMap{Real, LazySet{Real}, Real, Matrix{Real}})
∈(::AbstractVector{Real}, ::LinearMap{Real, LazySet{Real}, Real, Matrix{Real}})
an_element(::LinearMap)
```
Inherited from [`LazySet`](@ref):
Expand Down
69 changes: 39 additions & 30 deletions src/LinearMap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export LinearMap,
an_element

"""
LinearMap{NM, N} <: LazySet{N}
LinearMap{N<:Real, S<:LazySet{N}, NM, MAT<:AbstractMatrix{NM}} <: LazySet{N}
Type that represents a linear transformation ``M⋅S`` of a convex set ``S``.
Expand All @@ -15,29 +15,35 @@ Type that represents a linear transformation ``M⋅S`` of a convex set ``S``.
### Notes
This type is parametric in the elements of the linear map, `NM`, and independently
on the type of elements of the target set (`N`). Typically `NM = N` but it is
not necessarily always the case e.g. if `NM` is an interval that holds numbers of
type `N`, where `N` is a floating point number type such as `Float64`.
This type is parametric in the elements of the linear map, `NM`, which is
independent of the numeric type of the target set (`N`).
Typically `NM = N`, but there may be exceptions, e.g., if `NM` is an interval
that holds numbers of type `N`, where `N` is a floating point number type such
as `Float64`.
"""
struct LinearMap{NM, N} <: LazySet{N}
M::AbstractMatrix{NM}
X::LazySet{N}
struct LinearMap{N<:Real, S<:LazySet{N},
NM, MAT<:AbstractMatrix{NM}} <: LazySet{N}
M::MAT
X::S

# default constructor with dimension match check
function LinearMap{NM, N}(M::AbstractMatrix{NM}, X::LazySet{N}) where {NM, N}
@assert dim(X) == size(M, 2) "a linear map of size $(size(M)) cannot be " *
"applied to a set of dimension $(dim(X))"
function LinearMap{N, S, NM, MAT}(M::MAT, X::S) where {N<:Real,
S<:LazySet{N}, NM, MAT<:AbstractMatrix{NM}}
@assert dim(X) == size(M, 2) "a linear map of size $(size(M)) cannot " *
"be applied to a set of dimension $(dim(X))"
return new(M, X)
end
end

# type-less convenience constructor
LinearMap(M::AbstractMatrix{NM}, X::LazySet{N}) where {NM, N} = LinearMap{NM, N}(M, X)
LinearMap(M::MAT,
X::S) where {N<:Real, S<:LazySet{N}, NM, MAT<:AbstractMatrix{NM}} =
LinearMap{N, S, NM, MAT}(M, X)

# constructor from a linear map: perform the matrix multiplication immediately
LinearMap(M::AbstractMatrix{NM},
map::LinearMap{NM, N}) where {NM, N} = LinearMap{NM, N}(M * map.M, map.X)
LinearMap(M::MAT, lm::LinearMap{N, S, NM, MAT}) where {N<:Real, S<:LazySet{N},
NM, MAT<:AbstractMatrix{NM}} =
LinearMap{N, S, NM, MAT}(M * lm.M, lm.X)

"""
```
Expand Down Expand Up @@ -75,36 +81,37 @@ Return a linear map of a convex set by a scalar value.
The linear map of the convex set.
"""
function *(a::N, X::LazySet) where {N}
function *(a::N, X::LazySet{N}) where {N}
return LinearMap(a * speye(N, dim(X)), X)
end

"""
```
*(a::N, map::S)::S where {S<:LinearMap{NM, N}} where {NM, N}
*(a::N, lm::LM)::LM where {N<:Real, S<:LazySet{N}, LM<:LinearMap{N, S}}
```
Return a linear map scaled by a scalar value.
### Input
- `a` -- scalar
- `map` -- linear map
- `a` -- scalar
- `lm` -- linear map
### Output
The scaled linear map.
"""
function *(a::N, map::S)::S where {S<:LinearMap{NM, N}} where {NM, N}
return LinearMap(a * map.M, map.X)
function *(a::N, lm::LM)::LM where {N<:Real, S<:LazySet{N}, LM<:LinearMap{N, S}}
return LinearMap(a * lm.M, lm.X)
end

"""
```
*(M::AbstractMatrix{N}, Z::ZeroSet{N})::ZeroSet{N} where N<:Real
*(M::MAT, Z::ZeroSet{N})::ZeroSet{N} where {N<:Real, MAT<:AbstractMatrix}
```
A linear map of a zero set, which is simplified to a zero set.
A linear map of a zero set, which is simplified to a zero set (the absorbing
element).
### Input
Expand All @@ -115,8 +122,10 @@ A linear map of a zero set, which is simplified to a zero set.
The zero set with the output dimension of the linear map.
"""
function *(M::AbstractMatrix{N}, Z::ZeroSet{N})::ZeroSet{N} where N<:Real
@assert dim(Z) == size(M, 2)
function *(M::MAT,
Z::ZeroSet{N})::ZeroSet{N} where {N<:Real, MAT<:AbstractMatrix}
@assert dim(Z) == size(M, 2) "a linear map of size $(size(M)) cannot " *
"be applied to a set of dimension $(dim(Z))"
return ZeroSet{N}(size(M, 1))
end

Expand All @@ -138,7 +147,7 @@ function dim(lm::LinearMap)::Int
end

"""
σ(d::AbstractVector{<:Real}, lm::LinearMap)::AbstractVector{<:Real}
σ(d::V, lm::LinearMap) where {N<:Real, V<:AbstractVector{N}}
Return the support vector of the linear map.
Expand All @@ -162,7 +171,7 @@ function σ(d::V, lm::LinearMap) where {N<:Real, V<:AbstractVector{N}}
end

"""
∈(x::AbstractVector{N}, lm::LinearMap{NM, N})::Bool where {NM, N<:Real}
∈(x::AbstractVector{N}, lm::LinearMap{N})::Bool where {N<:Real}
Check whether a given point is contained in a linear map of a convex set.
Expand Down Expand Up @@ -202,7 +211,7 @@ julia> ∈([0.5, 0.5], M*B)
true
```
"""
function (x::AbstractVector{N}, lm::LinearMap{NM, N})::Bool where {NM, N<:Real}
function (x::AbstractVector{N}, lm::LinearMap{N})::Bool where {N<:Real}
return (lm.M \ x, lm.X)
end

Expand All @@ -213,12 +222,12 @@ Return some element of a linear map.
### Input
- `lmap` -- linear map
- `lm` -- linear map
### Output
An element in the linear map. It relies on the `an_element` function of the wrapped
set.
An element in the linear map.
It relies on the `an_element` function of the wrapped set.
"""
function an_element(lm::LinearMap)
return lm.M * an_element(lm.X)
Expand Down
7 changes: 2 additions & 5 deletions test/unit_LinearMap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,9 @@ for N in [Float64, Rational{Int}, Float32]
@test an_element(lm) lm

# check linear map between vector and set
X = Interval([0.9, 1.10445])
a = [-1., 2.]
@test a * X isa LinearMap
X = BallInf(N[1.0], N(1.10445))
a = N[-1, 2.]
@test a * X isa LinearMap{N, N}
a = N[-1., 2.]
@test a * X isa LinearMap{N, BallInf{N}, N, Matrix{N}}

# linear map with a ZeroSet
X = N[0. -1. ; 1. 0.] * ZeroSet{N}(2)
Expand Down

0 comments on commit 3e12d52

Please sign in to comment.