Skip to content

Commit 61ecb95

Browse files
committed
replace assertions with errors
1 parent ac757cc commit 61ecb95

File tree

4 files changed

+87
-47
lines changed

4 files changed

+87
-47
lines changed

src/identity.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Base.:(*)(𝐼::IdentityMultiple, x::Number) = IdentityMultiple(x * 𝐼.M, 𝐼
101101
Base.:(/)(𝐼::IdentityMultiple, x::Number) = IdentityMultiple(𝐼.M / x, 𝐼.n)
102102

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

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

118118
function Base.:(*)(A::$M, 𝐼::IdentityMultiple)
119-
@assert size(A, 2) == 𝐼.n
119+
size(A, 2) != 𝐼.n && throw(DimensionMismatch("incompatible dimensions"))
120120
return A * 𝐼.M.λ
121121
end
122122
end
@@ -129,24 +129,24 @@ for M in @static VERSION < v"1.6" ? [:AbstractMatrix] :
129129
:(Adjoint{<:Any,<:AbstractVector}))
130130
@eval begin
131131
function Base.:(/)(A::$M, 𝐼::IdentityMultiple)
132-
@assert size(A, 2) == 𝐼.n
132+
size(A, 2) != 𝐼.n && throw(DimensionMismatch("incompatible dimensions"))
133133
return A * inv(𝐼.M.λ)
134134
end
135135
end
136136
end
137137

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

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

148148
function Base.:(*)(𝐼1::IdentityMultiple, 𝐼2::IdentityMultiple)
149-
@assert 𝐼1.n == 𝐼2.n
149+
𝐼1.n != 𝐼2.n && throw(DimensionMismatch("incompatible dimensions"))
150150
return IdentityMultiple(𝐼1.M * 𝐼2.M, 𝐼1.n)
151151
end
152152

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

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

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

src/maps.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ struct AffineMap{T,MT<:AbstractMatrix{T},VT<:AbstractVector{T}} <: AbstractMap
118118
A::MT
119119
c::VT
120120
function AffineMap(A::MT, c::VT) where {T,MT<:AbstractMatrix{T},VT<:AbstractVector{T}}
121-
@assert size(A, 1) == length(c)
121+
size(A, 1) != length(c) && throw(DimensionMismatch("incompatible dimensions"))
122122
return new{T,MT,VT}(A, c)
123123
end
124124
end
@@ -152,7 +152,7 @@ struct ConstrainedAffineMap{T,MT<:AbstractMatrix{T},VT<:AbstractVector{T},ST} <:
152152
X::ST
153153
function ConstrainedAffineMap(A::MT, c::VT,
154154
X::ST) where {T,MT<:AbstractMatrix{T},VT<:AbstractVector{T},ST}
155-
@assert size(A, 1) == length(c)
155+
size(A, 1) != length(c) && throw(DimensionMismatch("incompatible dimensions"))
156156
return new{T,MT,VT,ST}(A, c, X)
157157
end
158158
end
@@ -185,7 +185,7 @@ struct LinearControlMap{T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T}} <: Abst
185185
B::MTB
186186
function LinearControlMap(A::MTA,
187187
B::MTB) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T}}
188-
@assert size(A, 1) == size(B, 1)
188+
size(A, 1) != size(B, 1) && throw(DimensionMismatch("incompatible dimensions"))
189189
return new{T,MTA,MTB}(A, B)
190190
end
191191
end
@@ -223,7 +223,7 @@ struct ConstrainedLinearControlMap{T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{
223223
function ConstrainedLinearControlMap(A::MTA, B::MTB, X::ST,
224224
U::UT) where {T,MTA<:AbstractMatrix{T},
225225
MTB<:AbstractMatrix{T},ST,UT}
226-
@assert size(A, 1) == size(B, 1)
226+
size(A, 1) != size(B, 1) && throw(DimensionMismatch("incompatible dimensions"))
227227
return new{T,MTA,MTB,ST,UT}(A, B, X, U)
228228
end
229229
end
@@ -261,7 +261,9 @@ struct AffineControlMap{T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},VT<:Abst
261261
function AffineControlMap(A::MTA, B::MTB,
262262
c::VT) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
263263
VT<:AbstractVector{T}}
264-
@assert size(A, 1) == size(B, 1) == length(c)
264+
if !(size(A, 1) == size(B, 1) == length(c))
265+
throw(DimensionMismatch("incompatible dimensions"))
266+
end
265267
return new{T,MTA,MTB,VT}(A, B, c)
266268
end
267269
end
@@ -303,7 +305,9 @@ struct ConstrainedAffineControlMap{T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{
303305
U::UT) where {T,MTA<:AbstractMatrix{T},
304306
MTB<:AbstractMatrix{T},VT<:AbstractVector{T},
305307
ST,UT}
306-
@assert size(A, 1) == size(B, 1) == length(c)
308+
if !(size(A, 1) == size(B, 1) == length(c))
309+
throw(DimensionMismatch("incompatible dimensions"))
310+
end
307311
return new{T,MTA,MTB,VT,ST,UT}(A, B, c, X, U)
308312
end
309313
end

src/systems.jl

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# check if a matrix is square
2-
@inline function issquare(A::AbstractMatrix)
3-
return size(A, 1) == size(A, 2)
4-
end
5-
61
for (Z, AZ) in ((:ContinuousIdentitySystem, :AbstractContinuousSystem),
72
(:DiscreteIdentitySystem, :AbstractDiscreteSystem))
83
@eval begin
@@ -120,7 +115,7 @@ for (Z, AZ) in ((:LinearContinuousSystem, :AbstractContinuousSystem),
120115
struct $(Z){T,MT<:AbstractMatrix{T}} <: $(AZ)
121116
A::MT
122117
function $(Z)(A::MT) where {T,MT<:AbstractMatrix{T}}
123-
@assert issquare(A)
118+
checksquare(A)
124119
return new{T,MT}(A)
125120
end
126121
end
@@ -183,7 +178,9 @@ for (Z, AZ) in ((:AffineContinuousSystem, :AbstractContinuousSystem),
183178
A::MT
184179
c::VT
185180
function $(Z)(A::MT, c::VT) where {T,MT<:AbstractMatrix{T},VT<:AbstractVector{T}}
186-
@assert checksquare(A) == length(c)
181+
if checksquare(A) != length(c)
182+
throw(DimensionMismatch("incompatible dimensions"))
183+
end
187184
return new{T,MT,VT}(A, c)
188185
end
189186
end
@@ -249,7 +246,9 @@ for (Z, AZ) in ((:LinearControlContinuousSystem, :AbstractContinuousSystem),
249246
A::MTA
250247
B::MTB
251248
function $(Z)(A::MTA, B::MTB) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T}}
252-
@assert checksquare(A) == size(B, 1)
249+
if checksquare(A) != size(B, 1)
250+
throw(DimensionMismatch("incompatible dimensions"))
251+
end
253252
return new{T,MTA,MTB}(A, B)
254253
end
255254
end
@@ -315,7 +314,7 @@ for (Z, AZ) in ((:ConstrainedLinearContinuousSystem, :AbstractContinuousSystem),
315314
A::MT
316315
X::ST
317316
function $(Z)(A::MT, X::ST) where {T,MT<:AbstractMatrix{T},ST}
318-
@assert issquare(A)
317+
checksquare(A)
319318
return new{T,MT,ST}(A, X)
320319
end
321320
end
@@ -383,7 +382,9 @@ for (Z, AZ) in ((:ConstrainedAffineContinuousSystem, :AbstractContinuousSystem),
383382
X::ST
384383
function $(Z)(A::MT, c::VT,
385384
X::ST) where {T,MT<:AbstractMatrix{T},VT<:AbstractVector{T},ST}
386-
@assert checksquare(A) == length(c)
385+
if checksquare(A) != length(c)
386+
throw(DimensionMismatch("incompatible dimensions"))
387+
end
387388
return new{T,MT,VT,ST}(A, c, X)
388389
end
389390
end
@@ -455,7 +456,9 @@ for (Z, AZ) in ((:AffineControlContinuousSystem, :AbstractContinuousSystem),
455456
function $(Z)(A::MTA, B::MTB,
456457
c::VT) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
457458
VT<:AbstractVector{T}}
458-
@assert checksquare(A) == length(c) == size(B, 1)
459+
if !(checksquare(A) == length(c) == size(B, 1))
460+
throw(DimensionMismatch("incompatible dimensions"))
461+
end
459462
return new{T,MTA,MTB,VT}(A, B, c)
460463
end
461464
end
@@ -530,7 +533,9 @@ for (Z, AZ) in ((:ConstrainedAffineControlContinuousSystem, :AbstractContinuousS
530533
function $(Z)(A::MTA, B::MTB, c::VT, X::ST,
531534
U::UT) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
532535
VT<:AbstractVector{T},ST,UT}
533-
@assert checksquare(A) == length(c) == size(B, 1)
536+
if !(checksquare(A) == length(c) == size(B, 1))
537+
throw(DimensionMismatch("incompatible dimensions"))
538+
end
534539
return new{T,MTA,MTB,VT,ST,UT}(A, B, c, X, U)
535540
end
536541
end
@@ -608,7 +613,9 @@ for (Z, AZ) in ((:ConstrainedLinearControlContinuousSystem, :AbstractContinuousS
608613
U::UT
609614
function $(Z)(A::MTA, B::MTB, X::ST,
610615
U::UT) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},ST,UT}
611-
@assert checksquare(A) == size(B, 1)
616+
if checksquare(A) != size(B, 1)
617+
throw(DimensionMismatch("incompatible dimensions"))
618+
end
612619
return new{T,MTA,MTB,ST,UT}(A, B, X, U)
613620
end
614621
end
@@ -679,7 +686,9 @@ for (Z, AZ) in ((:LinearDescriptorContinuousSystem, :AbstractContinuousSystem),
679686
A::MTA
680687
E::MTE
681688
function $(Z)(A::MTA, E::MTE) where {T,MTA<:AbstractMatrix{T},MTE<:AbstractMatrix{T}}
682-
@assert size(A) == size(E)
689+
if size(A) != size(E)
690+
throw(DimensionMismatch("incompatible dimensions"))
691+
end
683692
return new{T,MTA,MTE}(A, E)
684693
end
685694
end
@@ -746,7 +755,9 @@ for (Z, AZ) in ((:ConstrainedLinearDescriptorContinuousSystem, :AbstractContinuo
746755
X::ST
747756
function $(Z)(A::MTA, E::MTE,
748757
X::ST) where {T,MTA<:AbstractMatrix{T},MTE<:AbstractMatrix{T},ST}
749-
@assert size(A) == size(E)
758+
if size(A) != size(E)
759+
throw(DimensionMismatch("incompatible dimensions"))
760+
end
750761
return new{T,MTA,MTE,ST}(A, E, X)
751762
end
752763
end
@@ -816,7 +827,9 @@ for (Z, AZ) in ((:PolynomialContinuousSystem, :AbstractContinuousSystem),
816827
function $(Z)(p::VPT,
817828
statedim::Int) where {T,PT<:AbstractPolynomialLike{T},
818829
VPT<:AbstractVector{PT}}
819-
@assert statedim == MultivariatePolynomials.nvariables(p) "the state dimension $(statedim) does not match the number of state variables"
830+
if statedim != MultivariatePolynomials.nvariables(p)
831+
throw(DimensionMismatch("the state dimension $(statedim) does not match the number of state variables"))
832+
end
820833
return new{T,PT,VPT}(p, statedim)
821834
end
822835
end
@@ -887,7 +900,9 @@ for (Z, AZ) in ((:ConstrainedPolynomialContinuousSystem, :AbstractContinuousSyst
887900
X::ST
888901
function $(Z)(p::VPT, statedim::Int,
889902
X::ST) where {T,PT<:AbstractPolynomialLike{T},VPT<:AbstractVector{PT},ST}
890-
@assert statedim == MultivariatePolynomials.nvariables(p) "the state dimension $(statedim) does not match the number of state variables"
903+
if statedim != MultivariatePolynomials.nvariables(p)
904+
throw(DimensionMismatch("the state dimension $(statedim) does not match the number of state variables"))
905+
end
891906
return new{T,PT,VPT,ST}(p, statedim, X)
892907
end
893908
end
@@ -1213,7 +1228,9 @@ for (Z, AZ) in ((:NoisyLinearContinuousSystem, :AbstractContinuousSystem),
12131228
A::MTA
12141229
D::MTD
12151230
function $(Z)(A::MTA, D::MTD) where {T,MTA<:AbstractMatrix{T},MTD<:AbstractMatrix{T}}
1216-
@assert checksquare(A) == size(D, 1)
1231+
if checksquare(A) != size(D, 1)
1232+
throw(DimensionMismatch("incompatible dimensions"))
1233+
end
12171234
return new{T,MTA,MTD}(A, D)
12181235
end
12191236
end
@@ -1282,7 +1299,9 @@ for (Z, AZ) in ((:NoisyConstrainedLinearContinuousSystem, :AbstractContinuousSys
12821299
W::WT
12831300
function $(Z)(A::MTA, D::MTD, X::ST,
12841301
W::WT) where {T,MTA<:AbstractMatrix{T},MTD<:AbstractMatrix{T},ST,WT}
1285-
@assert checksquare(A) == size(D, 1)
1302+
if checksquare(A) != size(D, 1)
1303+
throw(DimensionMismatch("incompatible dimensions"))
1304+
end
12861305
return new{T,MTA,MTD,ST,WT}(A, D, X, W)
12871306
end
12881307
end
@@ -1357,7 +1376,9 @@ for (Z, AZ) in ((:NoisyLinearControlContinuousSystem, :AbstractContinuousSystem)
13571376
function $(Z)(A::MTA, B::MTB,
13581377
D::MTD) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
13591378
MTD<:AbstractMatrix{T}}
1360-
@assert checksquare(A) == size(B, 1) == size(D, 1)
1379+
if !(checksquare(A) == size(B, 1) == size(D, 1))
1380+
throw(DimensionMismatch("incompatible dimensions"))
1381+
end
13611382
return new{T,MTA,MTB,MTD}(A, B, D)
13621383
end
13631384
end
@@ -1435,7 +1456,9 @@ for (Z, AZ) in ((:NoisyConstrainedLinearControlContinuousSystem, :AbstractContin
14351456
function $(Z)(A::MTA, B::MTB, D::MTD, X::ST, U::UT,
14361457
W::WT) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
14371458
MTD<:AbstractMatrix{T},ST,UT,WT}
1438-
@assert checksquare(A) == size(B, 1) == size(D, 1)
1459+
if !(checksquare(A) == size(B, 1) == size(D, 1))
1460+
throw(DimensionMismatch("incompatible dimensions"))
1461+
end
14391462
return new{T,MTA,MTB,MTD,ST,UT,WT}(A, B, D, X, U, W)
14401463
end
14411464
end
@@ -1522,7 +1545,9 @@ for (Z, AZ) in ((:NoisyAffineControlContinuousSystem, :AbstractContinuousSystem)
15221545
function $(Z)(A::MTA, B::MTB, c::VT,
15231546
D::MTD) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
15241547
VT<:AbstractVector{T},MTD<:AbstractMatrix{T}}
1525-
@assert checksquare(A) == length(c) == size(B, 1) == size(D, 1)
1548+
if !(checksquare(A) == length(c) == size(B, 1) == size(D, 1))
1549+
throw(DimensionMismatch("incompatible dimensions"))
1550+
end
15261551
return new{T,MTA,MTB,VT,MTD}(A, B, c, D)
15271552
end
15281553
end
@@ -1602,7 +1627,9 @@ for (Z, AZ) in ((:NoisyConstrainedAffineControlContinuousSystem, :AbstractContin
16021627
function $(Z)(A::MTA, B::MTB, c::VT, D::MTD, X::ST, U::UT,
16031628
W::WT) where {T,MTA<:AbstractMatrix{T},MTB<:AbstractMatrix{T},
16041629
VT<:AbstractVector{T},MTD<:AbstractMatrix{T},ST,UT,WT}
1605-
@assert checksquare(A) == length(c) == size(B, 1) == size(D, 1)
1630+
if !(checksquare(A) == length(c) == size(B, 1) == size(D, 1))
1631+
throw(DimensionMismatch("incompatible dimensions"))
1632+
end
16061633
return new{T,MTA,MTB,VT,MTD,ST,UT,WT}(A, B, c, D, X, U, W)
16071634
end
16081635
end
@@ -1867,7 +1894,9 @@ for (Z, AZ) in ((:SecondOrderLinearContinuousSystem, :AbstractContinuousSystem),
18671894
K::MTK) where {T,MTM<:AbstractMatrix{T},
18681895
MTC<:AbstractMatrix{T},
18691896
MTK<:AbstractMatrix{T}}
1870-
@assert checksquare(M) == checksquare(C) == checksquare(K)
1897+
if !(checksquare(M) == checksquare(C) == checksquare(K))
1898+
throw(DimensionMismatch("incompatible dimensions"))
1899+
end
18711900
return new{T,MTM,MTC,MTK}(M, C, K)
18721901
end
18731902
end
@@ -1947,7 +1976,7 @@ for (Z, AZ) in ((:SecondOrderAffineContinuousSystem, :AbstractContinuousSystem),
19471976
MTC<:AbstractMatrix{T},
19481977
MTK<:AbstractMatrix{T},
19491978
VT<:AbstractVector{T}}
1950-
@assert checksquare(M) == checksquare(C) == checksquare(K) == length(b)
1979+
checksquare(M) == checksquare(C) == checksquare(K) == length(b)
19511980
return new{T,MTM,MTC,MTK,VT}(M, C, K, b)
19521981
end
19531982
end
@@ -2037,7 +2066,9 @@ for (Z, AZ) in ((:SecondOrderConstrainedLinearControlContinuousSystem, :Abstract
20372066
MTB<:AbstractMatrix{T},
20382067
ST,
20392068
UT}
2040-
@assert checksquare(M) == checksquare(C) == checksquare(K) == size(B, 1)
2069+
if !(checksquare(M) == checksquare(C) == checksquare(K) == size(B, 1))
2070+
throw(DimensionMismatch("incompatible dimensions"))
2071+
end
20412072
return new{T,MTM,MTC,MTK,MTB,ST,UT}(M, C, K, B, X, U)
20422073
end
20432074
end
@@ -2135,8 +2166,9 @@ for (Z, AZ) in ((:SecondOrderConstrainedAffineControlContinuousSystem, :Abstract
21352166
VT<:AbstractVector{T},
21362167
ST,
21372168
UT}
2138-
@assert checksquare(M) == checksquare(C) == checksquare(K) == size(B, 1) ==
2139-
length(d)
2169+
if !(checksquare(M) == checksquare(C) == checksquare(K) == size(B, 1) == length(d))
2170+
throw(DimensionMismatch("incompatible dimensions"))
2171+
end
21402172
return new{T,MTM,MTC,MTK,MTB,VT,ST,UT}(M, C, K, B, d, X, U)
21412173
end
21422174
end
@@ -2226,7 +2258,9 @@ for (Z, AZ) in ((:SecondOrderContinuousSystem, :AbstractContinuousSystem),
22262258
fe::FE
22272259
function $(Z)(M::MTM, C::MTC, fi::FI,
22282260
fe::FE) where {T,MTM<:AbstractMatrix{T},MTC<:AbstractMatrix{T},FI,FE}
2229-
@assert checksquare(M) == checksquare(C)
2261+
if checksquare(M) != checksquare(C)
2262+
throw(DimensionMismatch("incompatible dimensions"))
2263+
end
22302264
return new{T,MTM,MTC,FI,FE}(M, C, fi, fe)
22312265
end
22322266
end
@@ -2309,7 +2343,9 @@ for (Z, AZ) in ((:SecondOrderConstrainedContinuousSystem, :AbstractContinuousSys
23092343

23102344
function $(Z)(M::MTM, C::MTC, fi::FI, fe::FE, X::ST,
23112345
U::UT) where {T,MTM<:AbstractMatrix{T},MTC<:AbstractMatrix{T},FI,FE,ST,UT}
2312-
@assert checksquare(M) == checksquare(C)
2346+
if checksquare(M) != checksquare(C)
2347+
throw(DimensionMismatch("incompatible dimensions"))
2348+
end
23132349
return new{T,MTM,MTC,FI,FE,ST,UT}(M, C, fi, fe, X, U)
23142350
end
23152351
end

0 commit comments

Comments
 (0)