Skip to content

Commit 9a5da6c

Browse files
committed
more robust SymPy -> Symbolics translation
1 parent 8a33e77 commit 9a5da6c

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/SymbolicControlSystems.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,21 @@ function ControlSystemsBase.tf(sys::Sym, h = nothing)
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

0 commit comments

Comments
 (0)