Skip to content

Commit 08eee7e

Browse files
authored
Merge pull request #654 from JuliaControl/zerotype
add method for `zero(::Type{<:Discrete})`
2 parents d60e8f8 + 63e5577 commit 08eee7e

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

src/simplification.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Compute a bit-vector, expressing whether a state of the pair (A, B) is
4040
structurally controllable based on the location of zeros in the matrices.
4141
"""
4242
function struct_ctrb_states(A::AbstractVecOrMat, B::AbstractVecOrMat)
43-
size(A,1) > typemax(UInt16) && error("Maximum size of A exceeded. If you encounter this error, please open an issue. This limit is not fundamental and excists for performance reasons only.")
43+
size(A,1) > typemax(UInt16) && error("Maximum size of A exceeded. If you encounter this error, please open an issue. This limit is not fundamental and exists for performance reasons only.")
4444
# UInt16 can only store up to 65535, so if A is completely dense and of size larger than 65535, the computations below might overflow. This is exceedingly unlikely though.
4545
bitA = UInt16.(.!iszero.(A)) # Convert to Int because mutiplying with a bit matrix is slow
4646
x = vec(any(B .!= 0, dims=2)) # index vector indicating states that have been affected by input

src/types/StateSpace.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ end
224224

225225
## ADDITION ##
226226
Base.zero(sys::AbstractStateSpace) = basetype(sys)(zero(sys.D), sys.timeevol)
227-
Base.zero(::Type{StateSpace{Continuous, F}}) where {F} = ss([zero(F)], Continuous()) # Cannot make a zero of discrete system since sample time is not stored in type.
227+
Base.zero(::Type{StateSpace{Continuous, F}}) where {F} = ss([zero(F)], Continuous())
228+
Base.zero(::Type{StateSpace{D, F}}) where {D<:Discrete, F} = ss([zero(F)], undef_sampletime(D))
228229

229230
function +(s1::ST, s2::ST) where {ST <: AbstractStateSpace}
230231
#Ensure systems have same dimensions

src/types/TransferFunction.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ ninputs(G::TransferFunction) = size(G.matrix, 2)
3131
Base.ndims(::TransferFunction) = 2
3232
Base.size(G::TransferFunction) = size(G.matrix)
3333
Base.eltype(::Type{S}) where {S<:TransferFunction} = S
34-
Base.zero(G::TransferFunction{TE,S}) where {TE,S} = tf(zeros(numeric_type(S), size(G)), G.timeevol) # can not create a zero of a discrete system from the type alone, the sampletime is not stored.
35-
34+
Base.zero(G::TransferFunction{TE,S}) where {TE,S} = tf(zeros(numeric_type(S), size(G)), G.timeevol)
35+
Base.zero(::Type{TransferFunction{TE,S}}) where {TE,S} = TransferFunction([zero(S);;], undef_sampletime(TE))
3636
function Base.getproperty(G::TransferFunction, s::Symbol)
3737
s fieldnames(typeof(G)) && return getfield(G, s)
3838
if s === :ny

test/test_connections.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ D_022 = ss(4*eye_(2), 0.005)
3333
@test series(C_111, C_212) == C_212*C_111
3434
@test parallel(C_111, C_211) == C_111 + C_211
3535

36+
# test that zero(::Type) promotes to correct sample time
37+
@test zero(D_111) == ss(0, 0.005)
38+
D_1110 = [D_111 zero(typeof(D_111))]
39+
@test D_1110.Ts == D_111.Ts
40+
@test D_1110 == [D_111 zero(D_111)]
41+
3642
# Errors
3743
@test_throws ErrorException [C_111 D_111] # Sampling time mismatch
3844
@test_throws ErrorException [C_111; D_111] # Sampling time mismatch
@@ -91,6 +97,11 @@ s = tf("s")
9197
@test zero(ss(randn(2,3))) == ss(zeros(2,3))
9298
@test zero(tf(randn(2,3))) == tf(zeros(2,3))
9399

100+
# test that zero(::Type) promotes to correct sample time
101+
@test zero(Dtf_111) == tf(0, 0.005)
102+
Dtf_1110 = [Dtf_111 zero(typeof(Dtf_111))]
103+
@test Dtf_1110.Ts == Dtf_111.Ts
104+
@test Dtf_1110 == [Dtf_111 zero(Dtf_111)]
94105

95106

96107

0 commit comments

Comments
 (0)