Skip to content

Commit c2649b4

Browse files
authored
fix and test type stability in system concatenation (#643)
* use getproperty instead of getfield for LQG * merge * merge * fix and test type stability in system concatenation
1 parent 4c4c205 commit c2649b4

File tree

4 files changed

+40
-38
lines changed

4 files changed

+40
-38
lines changed

src/connections.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ append(systems::LTISystem...) = append(promote(systems...)...)
4141

4242
function Base.vcat(systems::DelayLtiSystem...)
4343
P = vcat_1([sys.P for sys in systems]...) # See PartitionedStateSpace
44-
Tau = vcat([sys.Tau for sys in systems]...)
44+
Tau = reduce(vcat, sys.Tau for sys in systems)
4545
return DelayLtiSystem(P, Tau)
4646
end
4747

4848
function Base.hcat(systems::DelayLtiSystem...)
4949
P = hcat_1([sys.P for sys in systems]...) # See PartitionedStateSpace
50-
Tau = vcat([sys.Tau for sys in systems]...)
50+
Tau = reduce(vcat, sys.Tau for sys in systems)
5151
return DelayLtiSystem(P, Tau)
5252
end
5353

@@ -59,9 +59,9 @@ function Base.vcat(systems::ST...) where ST <: AbstractStateSpace
5959
error("All systems must have same input dimension")
6060
end
6161
A = blockdiag([s.A for s in systems]...)
62-
B = vcat([s.B for s in systems]...)
62+
B = reduce(vcat, s.B for s in systems)
6363
C = blockdiag([s.C for s in systems]...)
64-
D = vcat([s.D for s in systems]...)
64+
D = reduce(vcat, s.D for s in systems)
6565
timeevol = common_timeevol(systems...)
6666
return ST(A, B, C, D, timeevol)
6767
end
@@ -73,7 +73,7 @@ function Base.vcat(systems::TransferFunction...)
7373
error("All systems must have same input dimension")
7474
end
7575
timeevol = common_timeevol(systems...)
76-
mat = vcat([s.matrix for s in systems]...)
76+
mat = reduce(vcat, s.matrix for s in systems)
7777

7878
return TransferFunction(mat, timeevol)
7979
end
@@ -89,8 +89,8 @@ function Base.hcat(systems::ST...) where ST <: AbstractStateSpace
8989
timeevol = common_timeevol(systems...)
9090
A = blockdiag([s.A for s in systems]...)
9191
B = blockdiag([s.B for s in systems]...)
92-
C = hcat([s.C for s in systems]...)
93-
D = hcat([s.D for s in systems]...)
92+
C = reduce(hcat, s.C for s in systems)
93+
D = reduce(hcat, s.D for s in systems)
9494

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

107107
return TransferFunction(mat, timeevol)
108108
end
109109

110-
Base.hcat(systems::LTISystem...) = hcat(promote(systems...)...)
110+
Base.hcat(systems::LTISystem...) = reduce(hcat, promote(systems...))
111111

112112
function Base._cat_t(::Val{1}, T::Type{<:LTISystem}, X...)
113113
vcat(convert.(T, X)...)

test/test_connections.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ D_022 = ss(4*eye_(2), 0.005)
2828
@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)
2929
@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)
3030

31+
@inferred hcat(C_111, C_221)
32+
3133
@test series(C_111, C_212) == C_212*C_111
3234
@test parallel(C_111, C_211) == C_111 + C_211
3335

test/test_statespace.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@
1111
# SCALARS
1212
for SS in (StateSpace, HeteroStateSpace), SS2 in (StateSpace, HeteroStateSpace)
1313
a_2 = [-5 -3; 2 -9]
14-
CS_111 = SS(-5, 2, 3, [0])
15-
CS_111_d = SS([3], 2, 1, 1)
16-
CS_211 = SS(a_2, [1; 2], [1 0], 0)
17-
CS_221 = SS(a_2, [1 0; 0 2], [1 0], 0)
18-
CS_222 = SS(a_2, [1 0; 0 2], eye_(2), 0)
14+
CS_111 = @inferred SS(-5, 2, 3, [0])
15+
CS_111_d = @inferred SS([3], 2, 1, 1)
16+
CS_211 = @inferred SS(a_2, [1; 2], [1 0], 0)
17+
CS_221 = @inferred SS(a_2, [1 0; 0 2], [1 0], 0)
18+
CS_222 = @inferred SS(a_2, [1 0; 0 2], eye_(2), 0)
1919

2020
# CONTINUOUS
2121
a_1 = [-5]
22-
C_111 = SS(a_1, [2], [3], [0])
23-
C_211 = SS2(a_2, [1; 2], [1 0], [0])
24-
C_212 = SS2(a_2, [1; 2], eye_(2), [0; 0])
25-
C_221 = SS2(a_2, [1 0; 0 2], [1 0], [0 0])
26-
C_222 = SS(a_2, [1 0; 0 2], eye_(2), zeros(Int,2,2))
27-
C_222_d = SS(a_2, [1 0; 0 2], eye_(2), eye_(2))
28-
C_022 = SS(4.0*eye_(2))
22+
C_111 = @inferred SS(a_1, [2], [3], [0])
23+
C_211 = @inferred SS2(a_2, [1; 2], [1 0], [0])
24+
C_212 = @inferred SS2(a_2, [1; 2], eye_(2), [0; 0])
25+
C_221 = @inferred SS2(a_2, [1 0; 0 2], [1 0], [0 0])
26+
C_222 = @inferred SS(a_2, [1 0; 0 2], eye_(2), zeros(Int,2,2))
27+
C_222_d = @inferred SS(a_2, [1 0; 0 2], eye_(2), eye_(2))
28+
C_022 = @inferred SS(4.0*eye_(2))
2929

3030
# DISCRETE
3131
da_1 = [-0.5]
3232
da_2 = [0.2 -0.8; -0.8 0.07]
33-
D_111 = SS(da_1, [2], [3], [0], 0.005)
34-
D_211 = SS2(da_2, [1; 2], [1 0], [0], 0.005)
35-
D_221 = SS2(da_2, [1 0; 0 2], [1 0], [0 0], 0.005)
36-
D_222 = SS(da_2, [1 0; 0 2], eye_(2), zeros(2,2), 0.005)
37-
D_222_d = SS(da_2, [1 0; 0 2], eye_(2), eye_(2), 0.005)
38-
D_022 = SS(4.0*eye_(2), 0.005)
33+
D_111 = @inferred SS(da_1, [2], [3], [0], 0.005)
34+
D_211 = @inferred SS2(da_2, [1; 2], [1 0], [0], 0.005)
35+
D_221 = @inferred SS2(da_2, [1 0; 0 2], [1 0], [0 0], 0.005)
36+
D_222 = @inferred SS(da_2, [1 0; 0 2], eye_(2), zeros(2,2), 0.005)
37+
D_222_d = @inferred SS(da_2, [1 0; 0 2], eye_(2), eye_(2), 0.005)
38+
D_022 = @inferred SS(4.0*eye_(2), 0.005)
3939

4040
# Definition of input, output and state names
4141
C_222_d_n = SS(a_2, [1 0; 0 2], eye_(2), eye_(2))

test/test_transferfunction.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ s = tf("s")
99
@inferred 1/s
1010

1111
# CONTINUOUS
12-
C_111 = tf([1, 2], [1, 5])
13-
C_211 = tf([1, 2, 3], [1, 8, 15])
14-
C_212 = tf(vecarray(2, 1,[1, 2, 3], [1, 2]), vecarray(2, 1, [1, 8, 15], [1, 8, 15]))
15-
C_221 = tf(vecarray(1, 2,[1, 2, 3], [1, 2]), vecarray(1, 2, [1, 8, 15], [1, 8, 15]))
12+
C_111 = @inferred tf([1, 2], [1, 5])
13+
C_211 = @inferred tf([1, 2, 3], [1, 8, 15])
14+
C_212 = @inferred tf(vecarray(2, 1,[1, 2, 3], [1, 2]), vecarray(2, 1, [1, 8, 15], [1, 8, 15]))
15+
C_221 = @inferred tf(vecarray(1, 2,[1, 2, 3], [1, 2]), vecarray(1, 2, [1, 8, 15], [1, 8, 15]))
1616
C_222 = [C_221; C_221]
17-
C_022 = tf(4*[1 0; 0 1])
18-
s = tf("s")
17+
C_022 = @inferred tf(4*[1 0; 0 1])
18+
s = @inferred tf("s")
1919

2020
# DISCRETE
21-
D_111 = tf([1, 2], [1, -0.5], 0.005)
22-
D_211 = tf([1, 2, 3], [1, -0.2, -0.15], 0.005)
23-
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)
21+
D_111 = @inferred tf([1, 2], [1, -0.5], 0.005)
22+
D_211 = @inferred tf([1, 2, 3], [1, -0.2, -0.15], 0.005)
23+
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)
2424
D_222 = [D_221; D_221]
25-
D_022 = tf(4*[1 0; 0 1], 0.005)
26-
z = tf("z", 0.005)
25+
D_022 = @inferred tf(4*[1 0; 0 1], 0.005)
26+
z = @inferred tf("z", 0.005)
2727

2828
# TESTS
2929
# Constructors

0 commit comments

Comments
 (0)