From 62daeb8ecc5eb5b192606217c18304bdac422937 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Wed, 11 Nov 2026 12:30:45 +0000 Subject: [PATCH] Order the roots of polynomials with particle coefficients consistently The roots of a polynomial with MonteCarloMeasurements particle coefficients are computed for each particle separately. Without sorting, the order of the roots differs between particles, so the particle-valued roots do not form conjugate pairs and the conversion to zpk fails. A package extension computes these roots with the default sorting by real and imaginary part. Co-Authored-By: Claude Opus 5.5 --- lib/ControlSystemsBase/Project.toml | 6 +++++- ...trolSystemsBaseMonteCarloMeasurementsExt.jl | 15 +++++++++++++++ lib/ControlSystemsBase/test/runtests.jl | 1 + .../test/test_monte_carlo_measurements.jl | 18 ++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 lib/ControlSystemsBase/ext/ControlSystemsBaseMonteCarloMeasurementsExt.jl create mode 100644 lib/ControlSystemsBase/test/test_monte_carlo_measurements.jl diff --git a/lib/ControlSystemsBase/Project.toml b/lib/ControlSystemsBase/Project.toml index 64a957af7..9ce8d7cb2 100644 --- a/lib/ControlSystemsBase/Project.toml +++ b/lib/ControlSystemsBase/Project.toml @@ -24,11 +24,13 @@ ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66" DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2" ImplicitDifferentiation = "57b37032-215b-411a-8a7c-41a003a55207" Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" +MonteCarloMeasurements = "0987c9cc-fe09-11e8-30f0-b96dd679fdca" [extensions] ControlSystemsBaseDSPExt = ["DSP"] ControlSystemsBaseImplicitDifferentiationExt = ["ImplicitDifferentiation", "ComponentArrays"] ControlSystemsBaseMakieExt = ["Makie"] +ControlSystemsBaseMonteCarloMeasurementsExt = ["MonteCarloMeasurements"] [compat] Aqua = "0.5" @@ -42,6 +44,7 @@ MacroTools = "0.5" Makie = "0.22, 0.23, 0.24" MatrixEquations = "1, 2.1, 3" MatrixPencils = "1.8.3, 2" +MonteCarloMeasurements = "1" Polynomials = "3.0, 4.0" PrecompileTools = "1" Printf = "<0.0.1, 1" @@ -62,10 +65,11 @@ GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71" GenericSchur = "c145ed77-6b09-5dd9-b285-bf645a82121e" ImplicitDifferentiation = "57b37032-215b-411a-8a7c-41a003a55207" Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" +MonteCarloMeasurements = "0987c9cc-fe09-11e8-30f0-b96dd679fdca" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Test", "Aqua", "ComponentArrays", "Documenter", "DSP", "FiniteDifferences", "ImplicitDifferentiation", "Makie", "CairoMakie", "GenericSchur", "GR", "Plots", "SparseArrays", "StaticArrays"] +test = ["Test", "Aqua", "ComponentArrays", "Documenter", "DSP", "FiniteDifferences", "ImplicitDifferentiation", "Makie", "CairoMakie", "MonteCarloMeasurements", "GenericSchur", "GR", "Plots", "SparseArrays", "StaticArrays"] diff --git a/lib/ControlSystemsBase/ext/ControlSystemsBaseMonteCarloMeasurementsExt.jl b/lib/ControlSystemsBase/ext/ControlSystemsBaseMonteCarloMeasurementsExt.jl new file mode 100644 index 000000000..f92ae12e3 --- /dev/null +++ b/lib/ControlSystemsBase/ext/ControlSystemsBaseMonteCarloMeasurementsExt.jl @@ -0,0 +1,15 @@ +module ControlSystemsBaseMonteCarloMeasurementsExt +using ControlSystemsBase +using ControlSystemsBase: Polynomials +using MonteCarloMeasurements: AbstractParticles + +# The roots of a polynomial with particle coefficients are computed for each particle +# separately. Without sorting, the order of the roots returned by LAPACK may differ +# between particles, so that a particle-valued root combines different roots and the +# particle-valued roots no longer form exact conjugate pairs. Sorting by real and +# imaginary part places the members of each conjugate pair at the same adjacent +# positions in every particle. +ControlSystemsBase.roots(p::Polynomials.AbstractPolynomial{<:AbstractParticles}; kwargs...) = + Polynomials.roots(p; kwargs...) + +end diff --git a/lib/ControlSystemsBase/test/runtests.jl b/lib/ControlSystemsBase/test/runtests.jl index 5f63ea64b..3ef6908f8 100644 --- a/lib/ControlSystemsBase/test/runtests.jl +++ b/lib/ControlSystemsBase/test/runtests.jl @@ -52,6 +52,7 @@ my_tests = [ "test_implicit_diff", "test_rootlocus", "test_root_locus_matrix", + "test_monte_carlo_measurements", ] @testset "All Tests" begin diff --git a/lib/ControlSystemsBase/test/test_monte_carlo_measurements.jl b/lib/ControlSystemsBase/test/test_monte_carlo_measurements.jl new file mode 100644 index 000000000..ff4f96110 --- /dev/null +++ b/lib/ControlSystemsBase/test/test_monte_carlo_measurements.jl @@ -0,0 +1,18 @@ +# MonteCarloMeasurements depends on GenericSchur, so this file must run after the tests that require generic linear algebra to be unavailable. +import MonteCarloMeasurements as MCM +@testset "MonteCarloMeasurements interoperability" begin + @info "Testing MonteCarloMeasurements interoperability" + # The roots of a polynomial with particle coefficients must be ordered consistently + # over the particles so that the particle-valued roots form conjugate pairs. + ω = MCM.:..(4, 12) + ζ = MCM.:..(0.054, 0.084) + G = tf([2*ζ/ω, 1], [1/ω^2, 2*ζ/ω, 1, 0, 0]) + C = tf([1.0, 1], [0.1, 1]) + Gc = feedback(C*G, 1) + Z = zpk(Gc) + p = Z.matrix[1].p + @test length(p) == 5 + @test count(x -> !iszero(imag(x)), p) == 4 + w = exp10.(range(-1, 2, length=20)) + @test MCM.pmean.(bode(Z, w)[1]) ≈ MCM.pmean.(bode(Gc, w)[1]) rtol=1e-6 +end