Skip to content

Commit f8f9037

Browse files
committed
scaled names for matrix multiplication
1 parent 387acad commit f8f9037

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/named_systems2.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,34 @@ function Base.:*(s1::NamedStateSpace{T, S}, s2::Number) where {T <: CS.TimeEvolu
299299
)
300300
end
301301

302+
function Base.:*(s1::AbstractMatrix, s2::NamedStateSpace{T, S}) where {T <: CS.TimeEvolution, S}
303+
if isdiag(s1)
304+
return NamedStateSpace{T,S}(
305+
s1*s2.sys,
306+
s2.x,
307+
s2.u,
308+
[Symbol(string(y)*"_scaled") for y in s2.y],
309+
isempty(s2.name) ? "" : s2.name*"_scaled",
310+
)
311+
else
312+
return *(promote(s1, s2)...)
313+
end
314+
end
315+
316+
function Base.:*(s1::NamedStateSpace{T, S}, s2::AbstractMatrix) where {T <: CS.TimeEvolution, S}
317+
if isdiag(s2)
318+
return NamedStateSpace{T,S}(
319+
s1.sys*s2,
320+
s1.x,
321+
[Symbol(string(u)*"_scaled") for u in s1.u],
322+
s1.y,
323+
isempty(s1.name) ? "" : s1.name*"_scaled",
324+
)
325+
else
326+
return *(promote(s1, s2)...)
327+
end
328+
end
329+
302330
function Base.:/(s::NamedStateSpace{T, S}, n::Number) where {T <: CS.TimeEvolution, S}
303331
s*(1/n)
304332
end

test/test_named_systems2.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ s2 = named_ss(G2, x = [:z], u = [:u1], y=[:y2])
200200
@test (G1*tf(1, [1,1])).sys == (G1*ss(tf(1, [1,1]))).sys
201201
@test (tf(1, [1,1])*G1).sys == (ss(tf(1, [1,1]))*G1).sys
202202

203+
# Matrix
204+
@test (G1*ones(1,1)).sys == (G1*ss(ones(1,1))).sys
205+
@test (ones(1,1)*G1).sys == (ss(ones(1,1))*G1).sys
206+
207+
# if the matrix is diagonal, the names are `u_scaled`
208+
@test endswith(string((G1*ones(1,1)).u[]), "_scaled")
209+
210+
# If the matrix is not diagonal, the names are generic
211+
G1 = named_ss(ssrand(1,2,1), "G1")
212+
@test !endswith(string((G1*ones(2,2)).u[1]), "_scaled")
203213
end
204214

205215

0 commit comments

Comments
 (0)