From c843f5f700668ac1ca31619ab6e8416180a761a1 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Wed, 21 Oct 2026 13:51:00 +0000 Subject: [PATCH] Reject spurious integrator clusters in state-space models --- lib/ControlSystemsBase/src/analysis.jl | 27 ++++++++++++-- lib/ControlSystemsBase/test/test_analysis.jl | 37 +++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/lib/ControlSystemsBase/src/analysis.jl b/lib/ControlSystemsBase/src/analysis.jl index f400d38e7..9a7d822aa 100644 --- a/lib/ControlSystemsBase/src/analysis.jl +++ b/lib/ControlSystemsBase/src/analysis.jl @@ -66,17 +66,40 @@ function count_eigval_multiplicity(p, location, e=nothing; scale=maximum(abs, p, (0, radius(1)) end +_integrator_poles_with_tol(P::LTISystem, p, location) = count_eigval_multiplicity(p, location) +function _integrator_poles_with_tol(P::AbstractStateSpace, p, location) + n, tol = count_eigval_multiplicity(p, location) + n == 0 && return (n, tol) + # An expanding eigenvalue cluster can contain ordinary slow modes rather + # than a perturbed Jordan block. A full-rank A - location*I rules out an + # integrator, even when the pole-only heuristic finds such a cluster. + A = balance_statespace(P; verbose=false)[1].A + F = qr(Matrix(float.(A - location*I)), ColumnNorm()) + # Use pivoted QR rather than SVD to also support BigFloat. Spell out the + # full-rank test since rank(::QRPivoted) requires Julia 1.12. + rank_tol = size(A, 1) * eps(real(float(eltype(F.factors)))) * abs(F.factors[1, 1]) + if all(i -> abs(F.factors[i, i]) > rank_tol, axes(A, 1)) + scale = maximum(abs, p, init=0.0) + return (0, 100eps(real(float(eltype(p)))) * scale) + end + n, tol +end + """ count_integrators(P) Count the number of poles in the origin by finding the first value of `n` for which the number of poles within a circle of radius `(100ϵ)^(1/n) * maximum(abs, p)` around the origin (1 in discrete time) equals `n`, see `ControlSystemsBase.count_eigval_multiplicity`. +For state-space models, a rank test of the balanced state matrix (minus the +identity in discrete time) rejects clusters when the matrix is nonsingular. +This avoids mistaking distinct slow modes for repeated integrators. + See also [`integrator_excess`](@ref). """ function count_integrators(P::LTISystem) p = poles(P) location = iscontinuous(P) ? 0 : 1 - count_eigval_multiplicity(p, location)[1] + _integrator_poles_with_tol(P, p, location)[1] end """ @@ -94,7 +117,7 @@ function integrator_excess_with_tol(P::LTISystem) p = poles(P) z = tzeros(P) location = iscontinuous(P) ? 0 : 1 - np, tolp = count_eigval_multiplicity(p, location) + np, tolp = _integrator_poles_with_tol(P, p, location) # The zeros are computed from the system pencil, whose scale is at least that of the poles. A single zero in the origin does not provide a scale of its own. nz, tolz = count_eigval_multiplicity(z, location; scale=max(maximum(abs, p, init=0.0), maximum(abs, z, init=0.0))) np - nz, p, z, tolp, tolz diff --git a/lib/ControlSystemsBase/test/test_analysis.jl b/lib/ControlSystemsBase/test/test_analysis.jl index fc0814f46..18c32c97c 100644 --- a/lib/ControlSystemsBase/test/test_analysis.jl +++ b/lib/ControlSystemsBase/test/test_analysis.jl @@ -504,6 +504,41 @@ nintbig = ControlSystemsBase.count_integrators(big(1.0)*pade(OL, 2)) @test ControlSystemsBase.count_integrators(pade(OL, 4)) == nintbig @test ControlSystemsBase.count_integrators(pade(OL, 10)) == nintbig +# A fast pole and several ordinary slow modes can form a spurious large +# cluster in the pole-only multiplicity heuristic (DyadControlSystems #821). +@testset "Slow modes are not integrators" begin + blocks = ([0.0035;;], [-20.0;;], [-0.0714;;], + [-0.002 0.0027; -0.0027 -0.002], + [-0.0172 0.339; -0.339 -0.0172], + [-0.113 0.656; -0.656 -0.113], + [-0.172 0.691; -0.691 -0.172]) + A = cat(blocks...; dims=(1, 2)) + Q = Matrix(qr([sin(i+2j) + (i==j) for i in 1:11, j in 1:11]).Q) + for c in (1e-4, 1.0, 1e4), rotate in (false, true) + Ac = c * (rotate ? Q'*A*Q : A) + P = ss(Ac, Matrix(1.0I, 11, 11), Matrix(1.0I, 11, 11), zeros(11, 11)) + @test ControlSystemsBase.count_integrators(P) == 0 + @test ControlSystemsBase.integrator_excess(P) == 0 + @test ControlSystemsBase.isunstable(P) + end + Ad = Q'*(I + 1e-3*A)*Q + Pd = ss(Ad, Matrix(1.0I, 11, 11), Matrix(1.0I, 11, 11), zeros(11, 11), 1.0) + @test ControlSystemsBase.count_integrators(Pd) == 0 + @test ControlSystemsBase.integrator_excess(Pd) == 0 + @test ControlSystemsBase.isunstable(Pd) + + # The matrix test must retain repeated, numerically split integrators. + for T in (Float32, Float64, BigFloat), n in (1, 2, 3) + A = diagm([T[-1, -2, -3]; zeros(T, n)]) + for i in 4:size(A, 1)-1 + A[i, i+1] = 1 + end + Qn = Matrix(qr(T[sin(i+2j) + (i==j) for i in 1:3+n, j in 1:3+n]).Q) + G = ss(Qn'*A*Qn, ones(T, 3+n, 1), ones(T, 1, 3+n), zeros(T, 1, 1)) + @test ControlSystemsBase.count_integrators(G) == n + end +end + # The integrator count must not depend on the time unit of the system let # The orthogonal similarity transformation prevents the eigenvalues in the origin from being computed exactly @@ -701,4 +736,4 @@ using ControlSystemsBase: isunstable @test isunstable(tf(1, [1,2,1], 1)) # Double pole at z = -1 @test !isunstable(ss(c2d(tf(1, [1,0,1]), 0.1))) # Simple pole pair on the unit circle @test isunstable(ss(c2d(tf(1, [1,0,1])^2, 0.1))) # Repeated pole pair on the unit circle -@test isunstable(zpk(Float64[], [cis(0.5), cis(0.5), cis(-0.5), cis(-0.5)], 1.0, 1)) \ No newline at end of file +@test isunstable(zpk(Float64[], [cis(0.5), cis(0.5), cis(-0.5), cis(-0.5)], 1.0, 1))