Skip to content

Commit b955799

Browse files
authored
handle conversion from number to Sym tf (#25)
* handle conversion from number to Sym tf * fix * more robust SymPy -> Symbolics translation
1 parent 9b15ccd commit b955799

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

.github/workflows/ci.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
name: CI
22
on:
3-
- push
43
- pull_request
54
jobs:
65
test:

src/SymbolicControlSystems.jl

+15-10
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,31 @@ function ControlSystemsBase.tf(sys::Sym, h = nothing)
8989
# d = d.monic() # Don't do this here
9090
if h === nothing || h isa Continuous
9191
d = sp.Poly(d, s)
92-
n = n isa Number ? n : sp.Poly(n, s)
92+
n = n isa Number ? 1.0*n : sp.Poly(n, s)
9393
tf(expand_coeffs(n, s), expand_coeffs(d, s))
9494
else
9595
d = sp.Poly(d, z)
96-
n = n isa Number ? n : sp.Poly(n, z)
96+
n = n isa Number ? 1.0*n : sp.Poly(n, z)
9797
tf(expand_coeffs(n, z), expand_coeffs(d, z), h)
9898
end
9999
end
100100

101101
function expand_coeffs(n, var; numeric = false)
102-
n = sp.Poly(n, var)
103-
deg = n.degree() |> Float64 |> Int
104-
c = n.all_coeffs() # This fails if the coeffs are symbolic
105-
numeric && (c = Float64.(c))
106-
[c; zeros(eltype(c), deg - length(c) + 1)]
102+
if n == 0
103+
[0.0]
104+
else
105+
n = sp.Poly(n, var)
106+
deg = n.degree() |> Float64 |> Int
107+
c = n.all_coeffs() # This fails if the coeffs are symbolic
108+
numeric && (c = Float64.(c))
109+
[c; zeros(eltype(c), deg - length(c) + 1)]
110+
end
107111
end
108112
expand_coeffs(n::Real, args...; numeric = false) = n
109113

110114
function ControlSystemsBase.tf(sys::NumOrDiv, h = nothing)
111-
sp = Symb.symbolics_to_sympy(sys)
112-
G = tf(sp, h)
115+
sys_sp = Symb.symbolics_to_sympy(sys)
116+
G = h === nothing || h === Continuous() ? tf(sys_sp) : tf(sys_sp, h)
113117
tf(to_num.(numvec(G)[]), to_num.(denvec(G)[]), G.timeevol)
114118
end
115119

@@ -144,7 +148,8 @@ function to_num(x::Sym)::Num
144148
try
145149
return Float64(x)
146150
catch
147-
Symb.Num(Symb.variable(Symbol(x); T=Real))
151+
# Symb.Num(Symb.variable(Symbol(x); T=Real))
152+
Symb.parse_expr_to_symbolic(Meta.parse(string(x)), Main)
148153
end
149154
end
150155

test/runtests.jl

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using ControlSystemsBase
44
s = SymbolicControlSystems.s
55
z = SymbolicControlSystems.z
66
import Symbolics
7-
import Symbolics: Num
7+
using Symbolics: Num
88
using LinearAlgebra
99
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
1010

@@ -96,6 +96,13 @@ end
9696
@test_both tf([b, a], [1, c, 1]) (b*s + a)/(s^2 + c*s + 1)
9797
@test_both tf([b, a], [1, c, 1], 0.1) (b*z + a)/(z^2 + c*z + 1)
9898

99+
100+
@syms T
101+
@test convert(typeof(tf(1, [T, 1])), 0) == tf(0)
102+
@test_nowarn [0 tf(1, [T, 1]); 0 tf(1)]
103+
@test [tf(0) tf(1, [T, 1])] == [0 tf(1, [T, 1])]
104+
105+
99106
end
100107

101108

0 commit comments

Comments
 (0)