Skip to content

Commit 69c4106

Browse files
authored
Merge pull request #658 from JuliaControl/append
reduce allocations from append and blockdiag
2 parents 8deea9e + 873d556 commit 69c4106

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

src/connections.jl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@ Append systems in block diagonal form
2323
function append(systems::(ST where ST<:AbstractStateSpace)...)
2424
ST = promote_type(typeof.(systems)...)
2525
timeevol = common_timeevol(systems...)
26-
A = blockdiag([s.A for s in systems]...)
27-
B = blockdiag([s.B for s in systems]...)
28-
C = blockdiag([s.C for s in systems]...)
29-
D = blockdiag([s.D for s in systems]...)
26+
A = blockdiag(s.A for s in systems)
27+
B = blockdiag(s.B for s in systems)
28+
C = blockdiag(s.C for s in systems)
29+
D = blockdiag(s.D for s in systems)
3030
return ST(A, B, C, D, timeevol)
3131
end
3232

3333
function append(systems::TransferFunction...)
3434
timeevol = common_timeevol(systems...)
35-
mat = blockdiag([s.matrix for s in systems]...)
35+
mat = blockdiag(s.matrix for s in systems)
3636
return TransferFunction(mat, timeevol)
3737
end
3838

39-
append(systems::LTISystem...) = append(promote(systems...)...)
39+
append(systems::LTISystem...) = append(promote(systems...))
40+
41+
append(systems::Union{<:Tuple, <:Base.Generator}) = append(systems...)
4042

4143

4244
function Base.vcat(systems::DelayLtiSystem...)
@@ -58,9 +60,9 @@ function Base.vcat(systems::ST...) where ST <: AbstractStateSpace
5860
if !all(s.nu == nu for s in systems)
5961
error("All systems must have same input dimension")
6062
end
61-
A = blockdiag([s.A for s in systems]...)
63+
A = blockdiag(s.A for s in systems)
6264
B = reduce(vcat, s.B for s in systems)
63-
C = blockdiag([s.C for s in systems]...)
65+
C = blockdiag(s.C for s in systems)
6466
D = reduce(vcat, s.D for s in systems)
6567
timeevol = common_timeevol(systems...)
6668
return ST(A, B, C, D, timeevol)
@@ -87,8 +89,8 @@ function Base.hcat(systems::ST...) where ST <: AbstractStateSpace
8789
error("All systems must have same output dimension")
8890
end
8991
timeevol = common_timeevol(systems...)
90-
A = blockdiag([s.A for s in systems]...)
91-
B = blockdiag([s.B for s in systems]...)
92+
A = blockdiag(s.A for s in systems)
93+
B = blockdiag(s.B for s in systems)
9294
C = reduce(hcat, s.C for s in systems)
9395
D = reduce(hcat, s.D for s in systems)
9496

@@ -133,6 +135,7 @@ end
133135

134136

135137
blockdiag(anything...) = cat(anything..., dims=(1,2))
138+
blockdiag(anything::Union{<:Tuple, <:Base.Generator}) = cat(anything..., dims=(1,2))
136139

137140

138141
"""

src/delay_systems.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function c2d(G::DelayLtiSystem, Ts::Real, method=:zoh)
5858
if !(method === :zoh)
5959
error("c2d for DelayLtiSystems only supports zero-order hold")
6060
end
61-
X = append([delayd_ss(τ, Ts) for τ in G.Tau]...)
61+
X = append(delayd_ss(τ, Ts) for τ in G.Tau)
6262
Pd = c2d(G.P.P, Ts)
6363
return lft(Pd, X)
6464
end
@@ -333,6 +333,6 @@ Approximate all time-delays in `G` by Padé approximations of degree `N`.
333333
function pade(G::DelayLtiSystem, N)
334334
ny, nu = size(G)
335335
nTau = length(G.Tau)
336-
X = append([ss(pade(τ,N)) for τ in G.Tau]...) # Perhaps append should be renamed blockdiag
336+
X = append(ss(pade(τ,N)) for τ in G.Tau) # Perhaps append should be renamed blockdiag
337337
return lft(G.P.P, X)
338338
end

src/utilities.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ poly2vec(p::Polynomial) = p.coeffs[1:end]
113113

114114

115115
function unwrap!(M::Array, dim=1)
116-
alldims(i) = [ n==dim ? i : (1:size(M,n)) for n in 1:ndims(M) ]
116+
alldims(i) = ntuple(n->n==dim ? i : (1:size(M,n)), ndims(M))
117117
for i = 2:size(M, dim)
118118
#This is a copy of slicedim from the JuliaLang but enables us to write to it
119119
#The code (with dim=1) is equivalent to
120120
# d = M[i,:,:,...,:] - M[i-1,:,...,:]
121121
# M[i,:,:,...,:] -= floor((d+π) / (2π)) * 2π
122122
d = M[alldims(i)...] - M[alldims(i-1)...]
123123
π2 = eltype(M)(2π)
124-
M[alldims(i)...] -= floor.((d .+ π) / π2) * π2
124+
M[alldims(i)...] -= @. floor((d + π) / π2) * π2
125125
end
126126
return M
127127
end

test/test_connections.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ K1d = ss(-1, 1, 1, 0, 1)
220220
@test feedback(K1, ss(1.0)) == ss(-2, 1, 1, 0)
221221
@test feedback(K1, 1.0) == ss(-2, 1, 1, 0)
222222
@test feedback(K1d, ss(1.0, 1)) == ss(-2, 1, 1, 0, 1)
223-
@test_broken feedback(G2d, 1.0) == ss(-2, 1, 1, 0, 1)
223+
@test feedback(G2d, 1.0) == ss(-62, 7, 8, 0, 1)
224224

225225
# Check that errors for sample-time mismatc are thrown
226226
@test_throws ErrorException feedback(G2, K1d)

0 commit comments

Comments
 (0)