Skip to content
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
18 changes: 9 additions & 9 deletions src/identity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Base.:(*)(𝐼::IdentityMultiple, x::Number) = IdentityMultiple(x * 𝐼.M, 𝐼
Base.:(/)(𝐼::IdentityMultiple, x::Number) = IdentityMultiple(𝐼.M / x, 𝐼.n)

function Base.:(*)(𝐼::IdentityMultiple, v::AbstractVector)
@assert 𝐼.n == length(v)
𝐼.n != length(v) && throw(DimensionMismatch("incompatible dimensions"))
return 𝐼.M.λ * v
end

Expand All @@ -111,12 +111,12 @@ for M in @static VERSION < v"1.6" ? [:AbstractMatrix] :
:(Adjoint{<:Any,<:AbstractVector}), :(LinearAlgebra.AbstractTriangular))
@eval begin
function Base.:(*)(𝐼::IdentityMultiple, A::$M)
@assert 𝐼.n == size(A, 1)
𝐼.n != size(A, 1) && throw(DimensionMismatch("incompatible dimensions"))
return 𝐼.M.λ * A
end

function Base.:(*)(A::$M, 𝐼::IdentityMultiple)
@assert size(A, 2) == 𝐼.n
size(A, 2) != 𝐼.n && throw(DimensionMismatch("incompatible dimensions"))
return A * 𝐼.M.λ
end
end
Expand All @@ -129,24 +129,24 @@ for M in @static VERSION < v"1.6" ? [:AbstractMatrix] :
:(Adjoint{<:Any,<:AbstractVector}))
@eval begin
function Base.:(/)(A::$M, 𝐼::IdentityMultiple)
@assert size(A, 2) == 𝐼.n
size(A, 2) != 𝐼.n && throw(DimensionMismatch("incompatible dimensions"))
return A * inv(𝐼.M.λ)
end
end
end

function Base.:(+)(𝐼1::IdentityMultiple, 𝐼2::IdentityMultiple)
@assert 𝐼1.n == 𝐼2.n
𝐼1.n != 𝐼2.n && throw(DimensionMismatch("incompatible dimensions"))
return IdentityMultiple(𝐼1.M + 𝐼2.M, 𝐼1.n)
end

function Base.:(-)(𝐼1::IdentityMultiple, 𝐼2::IdentityMultiple)
@assert 𝐼1.n == 𝐼2.n
𝐼1.n != 𝐼2.n && throw(DimensionMismatch("incompatible dimensions"))
return IdentityMultiple(𝐼1.M - 𝐼2.M, 𝐼1.n)
end

function Base.:(*)(𝐼1::IdentityMultiple, 𝐼2::IdentityMultiple)
@assert 𝐼1.n == 𝐼2.n
𝐼1.n != 𝐼2.n && throw(DimensionMismatch("incompatible dimensions"))
return IdentityMultiple(𝐼1.M * 𝐼2.M, 𝐼1.n)
end

Expand All @@ -159,12 +159,12 @@ function Base.:(*)(U::UniformScaling{T}, 𝐼::IdentityMultiple{S}) where {T<:Nu
end

function Base.:(/)(𝐼::IdentityMultiple{T}, U::UniformScaling{S}) where {T<:Number,S<:Number}
@assert !iszero(U.λ)
iszero(U.λ) && throw(DivideError())
return IdentityMultiple(𝐼.M * inv(U.λ), 𝐼.n)
end

function Base.:(/)(U::UniformScaling{T}, 𝐼::IdentityMultiple{S}) where {T<:Number,S<:Number}
@assert !iszero(𝐼.M.λ)
iszero(𝐼.M.λ) && throw(DivideError())
return IdentityMultiple(U * inv(𝐼.M.λ), 𝐼.n)
end

Expand Down
16 changes: 10 additions & 6 deletions src/maps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ struct AffineMap{T,MT<:AbstractMatrix{T},VT<:AbstractVector{T}} <: AbstractMap
A::MT
c::VT
function AffineMap(A::MT, c::VT) where {T,MT<:AbstractMatrix{T},VT<:AbstractVector{T}}
@assert size(A, 1) == length(c)
size(A, 1) != length(c) && throw(DimensionMismatch("incompatible dimensions"))
return new{T,MT,VT}(A, c)
end
end
Expand Down Expand Up @@ -152,7 +152,7 @@ struct ConstrainedAffineMap{T,MT<:AbstractMatrix{T},VT<:AbstractVector{T},ST} <:
X::ST
function ConstrainedAffineMap(A::MT, c::VT,
X::ST) where {T,MT<:AbstractMatrix{T},VT<:AbstractVector{T},ST}
@assert size(A, 1) == length(c)
size(A, 1) != length(c) && throw(DimensionMismatch("incompatible dimensions"))
return new{T,MT,VT,ST}(A, c, X)
end
end
Expand Down Expand Up @@ -185,7 +185,7 @@ struct LinearControlMap{T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T}} <: Abst
B::MTB
function LinearControlMap(A::MTA,
B::MTB) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T}}
@assert size(A, 1) == size(B, 1)
size(A, 1) != size(B, 1) && throw(DimensionMismatch("incompatible dimensions"))
return new{T,MTA,MTB}(A, B)
end
end
Expand Down Expand Up @@ -223,7 +223,7 @@ struct ConstrainedLinearControlMap{T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{
function ConstrainedLinearControlMap(A::MTA, B::MTB, X::ST,
U::UT) where {T,MTA<:AbstractMatrix{T},
MTB<:AbstractMatrix{T},ST,UT}
@assert size(A, 1) == size(B, 1)
size(A, 1) != size(B, 1) && throw(DimensionMismatch("incompatible dimensions"))
return new{T,MTA,MTB,ST,UT}(A, B, X, U)
end
end
Expand Down Expand Up @@ -261,7 +261,9 @@ struct AffineControlMap{T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},VT<:Abst
function AffineControlMap(A::MTA, B::MTB,
c::VT) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
VT<:AbstractVector{T}}
@assert size(A, 1) == size(B, 1) == length(c)
if !(size(A, 1) == size(B, 1) == length(c))
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTB,VT}(A, B, c)
end
end
Expand Down Expand Up @@ -303,7 +305,9 @@ struct ConstrainedAffineControlMap{T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{
U::UT) where {T,MTA<:AbstractMatrix{T},
MTB<:AbstractMatrix{T},VT<:AbstractVector{T},
ST,UT}
@assert size(A, 1) == size(B, 1) == length(c)
if !(size(A, 1) == size(B, 1) == length(c))
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTB,VT,ST,UT}(A, B, c, X, U)
end
end
Expand Down
96 changes: 66 additions & 30 deletions src/systems.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
# check if a matrix is square
@inline function issquare(A::AbstractMatrix)
return size(A, 1) == size(A, 2)
end

for (Z, AZ) in ((:ContinuousIdentitySystem, :AbstractContinuousSystem),
(:DiscreteIdentitySystem, :AbstractDiscreteSystem))
@eval begin
Expand Down Expand Up @@ -120,7 +115,7 @@ for (Z, AZ) in ((:LinearContinuousSystem, :AbstractContinuousSystem),
struct $(Z){T,MT<:AbstractMatrix{T}} <: $(AZ)
A::MT
function $(Z)(A::MT) where {T,MT<:AbstractMatrix{T}}
@assert issquare(A)
checksquare(A)
return new{T,MT}(A)
end
end
Expand Down Expand Up @@ -183,7 +178,9 @@ for (Z, AZ) in ((:AffineContinuousSystem, :AbstractContinuousSystem),
A::MT
c::VT
function $(Z)(A::MT, c::VT) where {T,MT<:AbstractMatrix{T},VT<:AbstractVector{T}}
@assert checksquare(A) == length(c)
if checksquare(A) != length(c)
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MT,VT}(A, c)
end
end
Expand Down Expand Up @@ -249,7 +246,9 @@ for (Z, AZ) in ((:LinearControlContinuousSystem, :AbstractContinuousSystem),
A::MTA
B::MTB
function $(Z)(A::MTA, B::MTB) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T}}
@assert checksquare(A) == size(B, 1)
if checksquare(A) != size(B, 1)
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTB}(A, B)
end
end
Expand Down Expand Up @@ -315,7 +314,7 @@ for (Z, AZ) in ((:ConstrainedLinearContinuousSystem, :AbstractContinuousSystem),
A::MT
X::ST
function $(Z)(A::MT, X::ST) where {T,MT<:AbstractMatrix{T},ST}
@assert issquare(A)
checksquare(A)
return new{T,MT,ST}(A, X)
end
end
Expand Down Expand Up @@ -383,7 +382,9 @@ for (Z, AZ) in ((:ConstrainedAffineContinuousSystem, :AbstractContinuousSystem),
X::ST
function $(Z)(A::MT, c::VT,
X::ST) where {T,MT<:AbstractMatrix{T},VT<:AbstractVector{T},ST}
@assert checksquare(A) == length(c)
if checksquare(A) != length(c)
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MT,VT,ST}(A, c, X)
end
end
Expand Down Expand Up @@ -455,7 +456,9 @@ for (Z, AZ) in ((:AffineControlContinuousSystem, :AbstractContinuousSystem),
function $(Z)(A::MTA, B::MTB,
c::VT) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
VT<:AbstractVector{T}}
@assert checksquare(A) == length(c) == size(B, 1)
if !(checksquare(A) == length(c) == size(B, 1))
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTB,VT}(A, B, c)
end
end
Expand Down Expand Up @@ -530,7 +533,9 @@ for (Z, AZ) in ((:ConstrainedAffineControlContinuousSystem, :AbstractContinuousS
function $(Z)(A::MTA, B::MTB, c::VT, X::ST,
U::UT) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
VT<:AbstractVector{T},ST,UT}
@assert checksquare(A) == length(c) == size(B, 1)
if !(checksquare(A) == length(c) == size(B, 1))
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTB,VT,ST,UT}(A, B, c, X, U)
end
end
Expand Down Expand Up @@ -608,7 +613,9 @@ for (Z, AZ) in ((:ConstrainedLinearControlContinuousSystem, :AbstractContinuousS
U::UT
function $(Z)(A::MTA, B::MTB, X::ST,
U::UT) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},ST,UT}
@assert checksquare(A) == size(B, 1)
if checksquare(A) != size(B, 1)
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTB,ST,UT}(A, B, X, U)
end
end
Expand Down Expand Up @@ -679,7 +686,9 @@ for (Z, AZ) in ((:LinearDescriptorContinuousSystem, :AbstractContinuousSystem),
A::MTA
E::MTE
function $(Z)(A::MTA, E::MTE) where {T,MTA<:AbstractMatrix{T},MTE<:AbstractMatrix{T}}
@assert size(A) == size(E)
if size(A) != size(E)
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTE}(A, E)
end
end
Expand Down Expand Up @@ -746,7 +755,9 @@ for (Z, AZ) in ((:ConstrainedLinearDescriptorContinuousSystem, :AbstractContinuo
X::ST
function $(Z)(A::MTA, E::MTE,
X::ST) where {T,MTA<:AbstractMatrix{T},MTE<:AbstractMatrix{T},ST}
@assert size(A) == size(E)
if size(A) != size(E)
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTE,ST}(A, E, X)
end
end
Expand Down Expand Up @@ -816,7 +827,9 @@ for (Z, AZ) in ((:PolynomialContinuousSystem, :AbstractContinuousSystem),
function $(Z)(p::VPT,
statedim::Int) where {T,PT<:AbstractPolynomialLike{T},
VPT<:AbstractVector{PT}}
@assert statedim == MultivariatePolynomials.nvariables(p) "the state dimension $(statedim) does not match the number of state variables"
if statedim != MultivariatePolynomials.nvariables(p)
throw(DimensionMismatch("the state dimension $(statedim) does not match the number of state variables"))
end
return new{T,PT,VPT}(p, statedim)
end
end
Expand Down Expand Up @@ -887,7 +900,9 @@ for (Z, AZ) in ((:ConstrainedPolynomialContinuousSystem, :AbstractContinuousSyst
X::ST
function $(Z)(p::VPT, statedim::Int,
X::ST) where {T,PT<:AbstractPolynomialLike{T},VPT<:AbstractVector{PT},ST}
@assert statedim == MultivariatePolynomials.nvariables(p) "the state dimension $(statedim) does not match the number of state variables"
if statedim != MultivariatePolynomials.nvariables(p)
throw(DimensionMismatch("the state dimension $(statedim) does not match the number of state variables"))
end
return new{T,PT,VPT,ST}(p, statedim, X)
end
end
Expand Down Expand Up @@ -1213,7 +1228,9 @@ for (Z, AZ) in ((:NoisyLinearContinuousSystem, :AbstractContinuousSystem),
A::MTA
D::MTD
function $(Z)(A::MTA, D::MTD) where {T,MTA<:AbstractMatrix{T},MTD<:AbstractMatrix{T}}
@assert checksquare(A) == size(D, 1)
if checksquare(A) != size(D, 1)
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTD}(A, D)
end
end
Expand Down Expand Up @@ -1282,7 +1299,9 @@ for (Z, AZ) in ((:NoisyConstrainedLinearContinuousSystem, :AbstractContinuousSys
W::WT
function $(Z)(A::MTA, D::MTD, X::ST,
W::WT) where {T,MTA<:AbstractMatrix{T},MTD<:AbstractMatrix{T},ST,WT}
@assert checksquare(A) == size(D, 1)
if checksquare(A) != size(D, 1)
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTD,ST,WT}(A, D, X, W)
end
end
Expand Down Expand Up @@ -1357,7 +1376,9 @@ for (Z, AZ) in ((:NoisyLinearControlContinuousSystem, :AbstractContinuousSystem)
function $(Z)(A::MTA, B::MTB,
D::MTD) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
MTD<:AbstractMatrix{T}}
@assert checksquare(A) == size(B, 1) == size(D, 1)
if !(checksquare(A) == size(B, 1) == size(D, 1))
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTB,MTD}(A, B, D)
end
end
Expand Down Expand Up @@ -1435,7 +1456,9 @@ for (Z, AZ) in ((:NoisyConstrainedLinearControlContinuousSystem, :AbstractContin
function $(Z)(A::MTA, B::MTB, D::MTD, X::ST, U::UT,
W::WT) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
MTD<:AbstractMatrix{T},ST,UT,WT}
@assert checksquare(A) == size(B, 1) == size(D, 1)
if !(checksquare(A) == size(B, 1) == size(D, 1))
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTB,MTD,ST,UT,WT}(A, B, D, X, U, W)
end
end
Expand Down Expand Up @@ -1522,7 +1545,9 @@ for (Z, AZ) in ((:NoisyAffineControlContinuousSystem, :AbstractContinuousSystem)
function $(Z)(A::MTA, B::MTB, c::VT,
D::MTD) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
VT<:AbstractVector{T},MTD<:AbstractMatrix{T}}
@assert checksquare(A) == length(c) == size(B, 1) == size(D, 1)
if !(checksquare(A) == length(c) == size(B, 1) == size(D, 1))
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTB,VT,MTD}(A, B, c, D)
end
end
Expand Down Expand Up @@ -1602,7 +1627,9 @@ for (Z, AZ) in ((:NoisyConstrainedAffineControlContinuousSystem, :AbstractContin
function $(Z)(A::MTA, B::MTB, c::VT, D::MTD, X::ST, U::UT,
W::WT) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
VT<:AbstractVector{T},MTD<:AbstractMatrix{T},ST,UT,WT}
@assert checksquare(A) == length(c) == size(B, 1) == size(D, 1)
if !(checksquare(A) == length(c) == size(B, 1) == size(D, 1))
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTA,MTB,VT,MTD,ST,UT,WT}(A, B, c, D, X, U, W)
end
end
Expand Down Expand Up @@ -1867,7 +1894,9 @@ for (Z, AZ) in ((:SecondOrderLinearContinuousSystem, :AbstractContinuousSystem),
K::MTK) where {T,MTM<:AbstractMatrix{T},
MTC<:AbstractMatrix{T},
MTK<:AbstractMatrix{T}}
@assert checksquare(M) == checksquare(C) == checksquare(K)
if !(checksquare(M) == checksquare(C) == checksquare(K))
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTM,MTC,MTK}(M, C, K)
end
end
Expand Down Expand Up @@ -1947,7 +1976,7 @@ for (Z, AZ) in ((:SecondOrderAffineContinuousSystem, :AbstractContinuousSystem),
MTC<:AbstractMatrix{T},
MTK<:AbstractMatrix{T},
VT<:AbstractVector{T}}
@assert checksquare(M) == checksquare(C) == checksquare(K) == length(b)
checksquare(M) == checksquare(C) == checksquare(K) == length(b)
return new{T,MTM,MTC,MTK,VT}(M, C, K, b)
end
end
Expand Down Expand Up @@ -2037,7 +2066,9 @@ for (Z, AZ) in ((:SecondOrderConstrainedLinearControlContinuousSystem, :Abstract
MTB<:AbstractMatrix{T},
ST,
UT}
@assert checksquare(M) == checksquare(C) == checksquare(K) == size(B, 1)
if !(checksquare(M) == checksquare(C) == checksquare(K) == size(B, 1))
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTM,MTC,MTK,MTB,ST,UT}(M, C, K, B, X, U)
end
end
Expand Down Expand Up @@ -2135,8 +2166,9 @@ for (Z, AZ) in ((:SecondOrderConstrainedAffineControlContinuousSystem, :Abstract
VT<:AbstractVector{T},
ST,
UT}
@assert checksquare(M) == checksquare(C) == checksquare(K) == size(B, 1) ==
length(d)
if !(checksquare(M) == checksquare(C) == checksquare(K) == size(B, 1) == length(d))
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTM,MTC,MTK,MTB,VT,ST,UT}(M, C, K, B, d, X, U)
end
end
Expand Down Expand Up @@ -2226,7 +2258,9 @@ for (Z, AZ) in ((:SecondOrderContinuousSystem, :AbstractContinuousSystem),
fe::FE
function $(Z)(M::MTM, C::MTC, fi::FI,
fe::FE) where {T,MTM<:AbstractMatrix{T},MTC<:AbstractMatrix{T},FI,FE}
@assert checksquare(M) == checksquare(C)
if checksquare(M) != checksquare(C)
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTM,MTC,FI,FE}(M, C, fi, fe)
end
end
Expand Down Expand Up @@ -2309,7 +2343,9 @@ for (Z, AZ) in ((:SecondOrderConstrainedContinuousSystem, :AbstractContinuousSys

function $(Z)(M::MTM, C::MTC, fi::FI, fe::FE, X::ST,
U::UT) where {T,MTM<:AbstractMatrix{T},MTC<:AbstractMatrix{T},FI,FE,ST,UT}
@assert checksquare(M) == checksquare(C)
if checksquare(M) != checksquare(C)
throw(DimensionMismatch("incompatible dimensions"))
end
return new{T,MTM,MTC,FI,FE,ST,UT}(M, C, fi, fe, X, U)
end
end
Expand Down
Loading
Loading