Skip to content

Commit ef893cd

Browse files
baggepinnenclaude
andcommitted
Tests for compressed StateSpace printing; fix D for 0-state systems
Move the D-matrix println back outside `if nstates(sys) > 0` so pure feedthrough systems (state dimension 0) still show D. Add tests covering the new compressed format for large A/B/C/D, the zero-D shortcut, and the mixed case (large A with small B/C/D). Update the four existing print test strings to drop the trailing blank line that the previous `println(..., "\n")` produced. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 8567949 commit ef893cd

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

lib/ControlSystemsBase/src/types/StateSpace.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,9 @@ function Base.show(io::IO, sys::AbstractStateSpace)
573573
println(io, "B = $(typeof(B))$(size(B))")
574574
length(C) < 200 ? println(io, "C = \n", _string_mat_with_headers(C)) :
575575
println(io, "C = $(typeof(C))$(size(C))")
576-
length(D) < 200 ? println(io, "D = \n", _string_mat_with_headers(D)) :
577-
println(io, "D = ", iszero(D) ? " 0" : "$(typeof(D))$(size(D))")
578576
end
577+
length(D) < 200 ? println(io, "D = \n", _string_mat_with_headers(D)) :
578+
println(io, "D = ", iszero(D) ? " 0" : "$(typeof(D))$(size(D))")
579579
# Print sample time
580580
if isdiscrete(sys)
581581
println(io, "Sample Time: ", sys.Ts, " (seconds)")

lib/ControlSystemsBase/test/test_statespace.jl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,24 @@
217217

218218
# Printing
219219
if SS <: StateSpace
220-
@test sprint(show, C_222) == "StateSpace{Continuous, Int64}\nA = \n -5 -3\n 2 -9\nB = \n 1 0\n 0 2\nC = \n 1 0\n 0 1\nD = \n 0 0\n 0 0\n\nContinuous-time state-space model"
221-
@test sprint(show, C_022) == "StateSpace{Continuous, Float64}\nD = \n 4.0 0.0\n 0.0 4.0\n\nContinuous-time state-space model"
222-
@test sprint(show, D_022) == "StateSpace{Discrete{Float64}, Float64}\nD = \n 4.0 0.0\n 0.0 4.0\n\nSample Time: 0.005 (seconds)\nDiscrete-time state-space model"
223-
@test sprint(show, D_222) == "StateSpace{Discrete{Float64}, Float64}\nA = \n 0.2 -0.8\n -0.8 0.07\nB = \n 1.0 0.0\n 0.0 2.0\nC = \n 1.0 0.0\n 0.0 1.0\nD = \n 0.0 0.0\n 0.0 0.0\n\nSample Time: 0.005 (seconds)\nDiscrete-time state-space model"
220+
@test sprint(show, C_222) == "StateSpace{Continuous, Int64}\nA = \n -5 -3\n 2 -9\nB = \n 1 0\n 0 2\nC = \n 1 0\n 0 1\nD = \n 0 0\n 0 0\nContinuous-time state-space model"
221+
@test sprint(show, C_022) == "StateSpace{Continuous, Float64}\nD = \n 4.0 0.0\n 0.0 4.0\nContinuous-time state-space model"
222+
@test sprint(show, D_022) == "StateSpace{Discrete{Float64}, Float64}\nD = \n 4.0 0.0\n 0.0 4.0\nSample Time: 0.005 (seconds)\nDiscrete-time state-space model"
223+
@test sprint(show, D_222) == "StateSpace{Discrete{Float64}, Float64}\nA = \n 0.2 -0.8\n -0.8 0.07\nB = \n 1.0 0.0\n 0.0 2.0\nC = \n 1.0 0.0\n 0.0 1.0\nD = \n 0.0 0.0\n 0.0 0.0\nSample Time: 0.005 (seconds)\nDiscrete-time state-space model"
224+
225+
# Large systems use a compressed format
226+
G_large_zeroD = ss(zeros(15,15), zeros(15,15), zeros(15,15), zeros(15,15))
227+
@test sprint(show, G_large_zeroD) == "StateSpace{Continuous, Float64}\nA = Matrix{Float64}(15, 15)\nB = Matrix{Float64}(15, 15)\nC = Matrix{Float64}(15, 15)\nD = 0\nContinuous-time state-space model"
228+
229+
G_large_nonzeroD = ss(zeros(15,15), zeros(15,15), zeros(15,15), ones(15,15))
230+
@test sprint(show, G_large_nonzeroD) == "StateSpace{Continuous, Float64}\nA = Matrix{Float64}(15, 15)\nB = Matrix{Float64}(15, 15)\nC = Matrix{Float64}(15, 15)\nD = Matrix{Float64}(15, 15)\nContinuous-time state-space model"
231+
232+
G_large_A = ss(zeros(15,15), zeros(15,1), zeros(1,15), 0)
233+
out_large_A = sprint(show, G_large_A)
234+
@test occursin("A = Matrix{Float64}(15, 15)", out_large_A)
235+
@test occursin("B = \n", out_large_A)
236+
@test occursin("C = \n", out_large_A)
237+
@test occursin("D = \n", out_large_A)
224238
end
225239

226240
G = ssrand(1,2,3)

0 commit comments

Comments
 (0)