From c38e6a6288ea64e3a5d7788a4276a55ec6d77e15 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Sat, 18 Jan 2025 12:26:30 +0100 Subject: [PATCH 1/3] handle conversion from number to Sym tf --- .github/workflows/ci.yml | 1 - src/SymbolicControlSystems.jl | 4 ++-- test/runtests.jl | 9 ++++++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 57c749d..48d94ad 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,5 @@ name: CI on: - - push - pull_request jobs: test: diff --git a/src/SymbolicControlSystems.jl b/src/SymbolicControlSystems.jl index 000b61b..9f35855 100644 --- a/src/SymbolicControlSystems.jl +++ b/src/SymbolicControlSystems.jl @@ -89,11 +89,11 @@ function ControlSystemsBase.tf(sys::Sym, h = nothing) # d = d.monic() # Don't do this here if h === nothing || h isa Continuous d = sp.Poly(d, s) - n = n isa Number ? n : sp.Poly(n, s) + n = n isa Number ? float(n) : sp.Poly(n, s) tf(expand_coeffs(n, s), expand_coeffs(d, s)) else d = sp.Poly(d, z) - n = n isa Number ? n : sp.Poly(n, z) + n = n isa Number ? float(n) : sp.Poly(n, z) tf(expand_coeffs(n, z), expand_coeffs(d, z), h) end end diff --git a/test/runtests.jl b/test/runtests.jl index e285b45..d0d86e5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -4,7 +4,7 @@ using ControlSystemsBase s = SymbolicControlSystems.s z = SymbolicControlSystems.z import Symbolics -import Symbolics: Num +using Symbolics: Num using LinearAlgebra isinteractive() && (Base.active_repl.options.hint_tab_completes = false) # This messes with sympy https://discourse.julialang.org/t/sympy-makes-repl-to-stuck/124814/6 @@ -96,6 +96,13 @@ end @test_both tf([b, a], [1, c, 1]) (b*s + a)/(s^2 + c*s + 1) @test_both tf([b, a], [1, c, 1], 0.1) (b*z + a)/(z^2 + c*z + 1) + + @syms T + @test convert(typeof(tf(1, [T, 1])), 0) == tf(0) + @test_nowarn [0 tf(1, [T, 1]); 0 tf(1)] + @test [tf(0) tf(1, [T, 1])] == [0 tf(1, [T, 1])] + + end From 8a33e77810e2eaf11d6b253e9bfd70991babdaa1 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Sat, 18 Jan 2025 12:36:07 +0100 Subject: [PATCH 2/3] fix --- src/SymbolicControlSystems.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SymbolicControlSystems.jl b/src/SymbolicControlSystems.jl index 9f35855..76b7a8a 100644 --- a/src/SymbolicControlSystems.jl +++ b/src/SymbolicControlSystems.jl @@ -89,11 +89,11 @@ function ControlSystemsBase.tf(sys::Sym, h = nothing) # d = d.monic() # Don't do this here if h === nothing || h isa Continuous d = sp.Poly(d, s) - n = n isa Number ? float(n) : sp.Poly(n, s) + n = n isa Number ? 1.0*n : sp.Poly(n, s) tf(expand_coeffs(n, s), expand_coeffs(d, s)) else d = sp.Poly(d, z) - n = n isa Number ? float(n) : sp.Poly(n, z) + n = n isa Number ? 1.0*n : sp.Poly(n, z) tf(expand_coeffs(n, z), expand_coeffs(d, z), h) end end From 9a5da6c3480c692334838c41b5965144d8d8448c Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Sun, 19 Jan 2025 11:46:57 +0100 Subject: [PATCH 3/3] more robust SymPy -> Symbolics translation --- src/SymbolicControlSystems.jl | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/SymbolicControlSystems.jl b/src/SymbolicControlSystems.jl index 76b7a8a..2939fa6 100644 --- a/src/SymbolicControlSystems.jl +++ b/src/SymbolicControlSystems.jl @@ -99,17 +99,21 @@ function ControlSystemsBase.tf(sys::Sym, h = nothing) end function expand_coeffs(n, var; numeric = false) - n = sp.Poly(n, var) - deg = n.degree() |> Float64 |> Int - c = n.all_coeffs() # This fails if the coeffs are symbolic - numeric && (c = Float64.(c)) - [c; zeros(eltype(c), deg - length(c) + 1)] + if n == 0 + [0.0] + else + n = sp.Poly(n, var) + deg = n.degree() |> Float64 |> Int + c = n.all_coeffs() # This fails if the coeffs are symbolic + numeric && (c = Float64.(c)) + [c; zeros(eltype(c), deg - length(c) + 1)] + end end expand_coeffs(n::Real, args...; numeric = false) = n function ControlSystemsBase.tf(sys::NumOrDiv, h = nothing) - sp = Symb.symbolics_to_sympy(sys) - G = tf(sp, h) + sys_sp = Symb.symbolics_to_sympy(sys) + G = h === nothing || h === Continuous() ? tf(sys_sp) : tf(sys_sp, h) tf(to_num.(numvec(G)[]), to_num.(denvec(G)[]), G.timeevol) end @@ -144,7 +148,8 @@ function to_num(x::Sym)::Num try return Float64(x) catch - Symb.Num(Symb.variable(Symbol(x); T=Real)) + # Symb.Num(Symb.variable(Symbol(x); T=Real)) + Symb.parse_expr_to_symbolic(Meta.parse(string(x)), Main) end end