Skip to content

Commit

Permalink
Merge pull request #654 from JuliaControl/zerotype
Browse files Browse the repository at this point in the history
add method for `zero(::Type{<:Discrete})`
  • Loading branch information
baggepinnen authored Feb 23, 2022
2 parents d60e8f8 + 63e5577 commit 08eee7e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/simplification.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Compute a bit-vector, expressing whether a state of the pair (A, B) is
structurally controllable based on the location of zeros in the matrices.
"""
function struct_ctrb_states(A::AbstractVecOrMat, B::AbstractVecOrMat)
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.")
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.")
# 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.
bitA = UInt16.(.!iszero.(A)) # Convert to Int because mutiplying with a bit matrix is slow
x = vec(any(B .!= 0, dims=2)) # index vector indicating states that have been affected by input
Expand Down
3 changes: 2 additions & 1 deletion src/types/StateSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ end

## ADDITION ##
Base.zero(sys::AbstractStateSpace) = basetype(sys)(zero(sys.D), sys.timeevol)
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.
Base.zero(::Type{StateSpace{Continuous, F}}) where {F} = ss([zero(F)], Continuous())
Base.zero(::Type{StateSpace{D, F}}) where {D<:Discrete, F} = ss([zero(F)], undef_sampletime(D))

function +(s1::ST, s2::ST) where {ST <: AbstractStateSpace}
#Ensure systems have same dimensions
Expand Down
4 changes: 2 additions & 2 deletions src/types/TransferFunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ ninputs(G::TransferFunction) = size(G.matrix, 2)
Base.ndims(::TransferFunction) = 2
Base.size(G::TransferFunction) = size(G.matrix)
Base.eltype(::Type{S}) where {S<:TransferFunction} = S
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.

Base.zero(G::TransferFunction{TE,S}) where {TE,S} = tf(zeros(numeric_type(S), size(G)), G.timeevol)
Base.zero(::Type{TransferFunction{TE,S}}) where {TE,S} = TransferFunction([zero(S);;], undef_sampletime(TE))
function Base.getproperty(G::TransferFunction, s::Symbol)
s fieldnames(typeof(G)) && return getfield(G, s)
if s === :ny
Expand Down
11 changes: 11 additions & 0 deletions test/test_connections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ D_022 = ss(4*eye_(2), 0.005)
@test series(C_111, C_212) == C_212*C_111
@test parallel(C_111, C_211) == C_111 + C_211

# test that zero(::Type) promotes to correct sample time
@test zero(D_111) == ss(0, 0.005)
D_1110 = [D_111 zero(typeof(D_111))]
@test D_1110.Ts == D_111.Ts
@test D_1110 == [D_111 zero(D_111)]

# Errors
@test_throws ErrorException [C_111 D_111] # Sampling time mismatch
@test_throws ErrorException [C_111; D_111] # Sampling time mismatch
Expand Down Expand Up @@ -91,6 +97,11 @@ s = tf("s")
@test zero(ss(randn(2,3))) == ss(zeros(2,3))
@test zero(tf(randn(2,3))) == tf(zeros(2,3))

# test that zero(::Type) promotes to correct sample time
@test zero(Dtf_111) == tf(0, 0.005)
Dtf_1110 = [Dtf_111 zero(typeof(Dtf_111))]
@test Dtf_1110.Ts == Dtf_111.Ts
@test Dtf_1110 == [Dtf_111 zero(Dtf_111)]



Expand Down

0 comments on commit 08eee7e

Please sign in to comment.