Skip to content

Commit

Permalink
fix and test type stability in system concatenation (#643)
Browse files Browse the repository at this point in the history
* use getproperty instead of getfield for LQG

* merge

* merge

* fix and test type stability in system concatenation
  • Loading branch information
baggepinnen authored Feb 19, 2022
1 parent 4c4c205 commit c2649b4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 38 deletions.
18 changes: 9 additions & 9 deletions src/connections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ append(systems::LTISystem...) = append(promote(systems...)...)

function Base.vcat(systems::DelayLtiSystem...)
P = vcat_1([sys.P for sys in systems]...) # See PartitionedStateSpace
Tau = vcat([sys.Tau for sys in systems]...)
Tau = reduce(vcat, sys.Tau for sys in systems)
return DelayLtiSystem(P, Tau)
end

function Base.hcat(systems::DelayLtiSystem...)
P = hcat_1([sys.P for sys in systems]...) # See PartitionedStateSpace
Tau = vcat([sys.Tau for sys in systems]...)
Tau = reduce(vcat, sys.Tau for sys in systems)
return DelayLtiSystem(P, Tau)
end

Expand All @@ -59,9 +59,9 @@ function Base.vcat(systems::ST...) where ST <: AbstractStateSpace
error("All systems must have same input dimension")
end
A = blockdiag([s.A for s in systems]...)
B = vcat([s.B for s in systems]...)
B = reduce(vcat, s.B for s in systems)
C = blockdiag([s.C for s in systems]...)
D = vcat([s.D for s in systems]...)
D = reduce(vcat, s.D for s in systems)
timeevol = common_timeevol(systems...)
return ST(A, B, C, D, timeevol)
end
Expand All @@ -73,7 +73,7 @@ function Base.vcat(systems::TransferFunction...)
error("All systems must have same input dimension")
end
timeevol = common_timeevol(systems...)
mat = vcat([s.matrix for s in systems]...)
mat = reduce(vcat, s.matrix for s in systems)

return TransferFunction(mat, timeevol)
end
Expand All @@ -89,8 +89,8 @@ function Base.hcat(systems::ST...) where ST <: AbstractStateSpace
timeevol = common_timeevol(systems...)
A = blockdiag([s.A for s in systems]...)
B = blockdiag([s.B for s in systems]...)
C = hcat([s.C for s in systems]...)
D = hcat([s.D for s in systems]...)
C = reduce(hcat, s.C for s in systems)
D = reduce(hcat, s.D for s in systems)

return ST(A, B, C, D, timeevol)
end
Expand All @@ -102,12 +102,12 @@ function Base.hcat(systems::TransferFunction...)
error("All systems must have same output dimension")
end
timeevol = common_timeevol(systems...)
mat = hcat([s.matrix for s in systems]...)
mat = reduce(hcat, s.matrix for s in systems)

return TransferFunction(mat, timeevol)
end

Base.hcat(systems::LTISystem...) = hcat(promote(systems...)...)
Base.hcat(systems::LTISystem...) = reduce(hcat, promote(systems...))

function Base._cat_t(::Val{1}, T::Type{<:LTISystem}, X...)
vcat(convert.(T, X)...)
Expand Down
2 changes: 2 additions & 0 deletions test/test_connections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ D_022 = ss(4*eye_(2), 0.005)
@test [D_022 D_222] == ss(eye_(2), [0 0 1 0; 0 0 0 2], [1 0; 0 1], [4 0 0 0; 0 4 0 0], 0.005)
@test [D_022; D_222] == ss(eye_(2), [1 0; 0 2], [0 0; 0 0; 1 0; 0 1], [4 0; 0 4; 0 0; 0 0], 0.005)

@inferred hcat(C_111, C_221)

@test series(C_111, C_212) == C_212*C_111
@test parallel(C_111, C_211) == C_111 + C_211

Expand Down
36 changes: 18 additions & 18 deletions test/test_statespace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@
# SCALARS
for SS in (StateSpace, HeteroStateSpace), SS2 in (StateSpace, HeteroStateSpace)
a_2 = [-5 -3; 2 -9]
CS_111 = SS(-5, 2, 3, [0])
CS_111_d = SS([3], 2, 1, 1)
CS_211 = SS(a_2, [1; 2], [1 0], 0)
CS_221 = SS(a_2, [1 0; 0 2], [1 0], 0)
CS_222 = SS(a_2, [1 0; 0 2], eye_(2), 0)
CS_111 = @inferred SS(-5, 2, 3, [0])
CS_111_d = @inferred SS([3], 2, 1, 1)
CS_211 = @inferred SS(a_2, [1; 2], [1 0], 0)
CS_221 = @inferred SS(a_2, [1 0; 0 2], [1 0], 0)
CS_222 = @inferred SS(a_2, [1 0; 0 2], eye_(2), 0)

# CONTINUOUS
a_1 = [-5]
C_111 = SS(a_1, [2], [3], [0])
C_211 = SS2(a_2, [1; 2], [1 0], [0])
C_212 = SS2(a_2, [1; 2], eye_(2), [0; 0])
C_221 = SS2(a_2, [1 0; 0 2], [1 0], [0 0])
C_222 = SS(a_2, [1 0; 0 2], eye_(2), zeros(Int,2,2))
C_222_d = SS(a_2, [1 0; 0 2], eye_(2), eye_(2))
C_022 = SS(4.0*eye_(2))
C_111 = @inferred SS(a_1, [2], [3], [0])
C_211 = @inferred SS2(a_2, [1; 2], [1 0], [0])
C_212 = @inferred SS2(a_2, [1; 2], eye_(2), [0; 0])
C_221 = @inferred SS2(a_2, [1 0; 0 2], [1 0], [0 0])
C_222 = @inferred SS(a_2, [1 0; 0 2], eye_(2), zeros(Int,2,2))
C_222_d = @inferred SS(a_2, [1 0; 0 2], eye_(2), eye_(2))
C_022 = @inferred SS(4.0*eye_(2))

# DISCRETE
da_1 = [-0.5]
da_2 = [0.2 -0.8; -0.8 0.07]
D_111 = SS(da_1, [2], [3], [0], 0.005)
D_211 = SS2(da_2, [1; 2], [1 0], [0], 0.005)
D_221 = SS2(da_2, [1 0; 0 2], [1 0], [0 0], 0.005)
D_222 = SS(da_2, [1 0; 0 2], eye_(2), zeros(2,2), 0.005)
D_222_d = SS(da_2, [1 0; 0 2], eye_(2), eye_(2), 0.005)
D_022 = SS(4.0*eye_(2), 0.005)
D_111 = @inferred SS(da_1, [2], [3], [0], 0.005)
D_211 = @inferred SS2(da_2, [1; 2], [1 0], [0], 0.005)
D_221 = @inferred SS2(da_2, [1 0; 0 2], [1 0], [0 0], 0.005)
D_222 = @inferred SS(da_2, [1 0; 0 2], eye_(2), zeros(2,2), 0.005)
D_222_d = @inferred SS(da_2, [1 0; 0 2], eye_(2), eye_(2), 0.005)
D_022 = @inferred SS(4.0*eye_(2), 0.005)

# Definition of input, output and state names
C_222_d_n = SS(a_2, [1 0; 0 2], eye_(2), eye_(2))
Expand Down
22 changes: 11 additions & 11 deletions test/test_transferfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ s = tf("s")
@inferred 1/s

# CONTINUOUS
C_111 = tf([1, 2], [1, 5])
C_211 = tf([1, 2, 3], [1, 8, 15])
C_212 = tf(vecarray(2, 1,[1, 2, 3], [1, 2]), vecarray(2, 1, [1, 8, 15], [1, 8, 15]))
C_221 = tf(vecarray(1, 2,[1, 2, 3], [1, 2]), vecarray(1, 2, [1, 8, 15], [1, 8, 15]))
C_111 = @inferred tf([1, 2], [1, 5])
C_211 = @inferred tf([1, 2, 3], [1, 8, 15])
C_212 = @inferred tf(vecarray(2, 1,[1, 2, 3], [1, 2]), vecarray(2, 1, [1, 8, 15], [1, 8, 15]))
C_221 = @inferred tf(vecarray(1, 2,[1, 2, 3], [1, 2]), vecarray(1, 2, [1, 8, 15], [1, 8, 15]))
C_222 = [C_221; C_221]
C_022 = tf(4*[1 0; 0 1])
s = tf("s")
C_022 = @inferred tf(4*[1 0; 0 1])
s = @inferred tf("s")

# DISCRETE
D_111 = tf([1, 2], [1, -0.5], 0.005)
D_211 = tf([1, 2, 3], [1, -0.2, -0.15], 0.005)
D_221 = tf(vecarray(1, 2, [1, 2, 3], [1, 2]), vecarray(1, 2, [1, -0.2, -0.15], [1, -0.2, -0.15]), 0.005)
D_111 = @inferred tf([1, 2], [1, -0.5], 0.005)
D_211 = @inferred tf([1, 2, 3], [1, -0.2, -0.15], 0.005)
D_221 = @inferred tf(vecarray(1, 2, [1, 2, 3], [1, 2]), vecarray(1, 2, [1, -0.2, -0.15], [1, -0.2, -0.15]), 0.005)
D_222 = [D_221; D_221]
D_022 = tf(4*[1 0; 0 1], 0.005)
z = tf("z", 0.005)
D_022 = @inferred tf(4*[1 0; 0 1], 0.005)
z = @inferred tf("z", 0.005)

# TESTS
# Constructors
Expand Down

0 comments on commit c2649b4

Please sign in to comment.