Skip to content

Commit c636fb3

Browse files
committed
reduce allocations in lsim
1 parent 4feb58d commit c636fb3

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/timeresp.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ function lsim(sys::AbstractStateSpace, u::AbstractVecOrMat, t::AbstractVector;
151151
end
152152

153153
x = ltitr(dsys.A, dsys.B, u, x0)
154-
y = sys.C*x + sys.D*u
154+
y = sys.C*x
155+
if !iszero(sys.D)
156+
mul!(y, sys.D, u, 1, 1)
157+
end
155158
return SimResult(y, t, x, u, dsys) # saves the system that actually produced the simulation
156159
end
157160

@@ -205,7 +208,10 @@ function lsim(sys::AbstractStateSpace, u::Function, t::AbstractVector;
205208
uout = reduce(hcat, u(x[:, i], t[i]) for i in eachindex(t))
206209
simsys = sys
207210
end
208-
y = sys.C*x + sys.D*uout
211+
y = sys.C*x
212+
if !iszero(sys.D)
213+
mul!(y, sys.D, uout, 1, 1)
214+
end
209215
return SimResult(y, t, x, uout, simsys) # saves the system that actually produced the simulation
210216
end
211217

0 commit comments

Comments
 (0)