Skip to content

Commit 9247b5c

Browse files
committed
Use Symbolics.jl
1 parent a958405 commit 9247b5c

15 files changed

+87
-1697
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
3232
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
3333
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
3434
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
35+
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
3536
TreeViews = "a2a6695c-b41b-5b7d-aed9-dbfdeacea5d7"
3637
UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
3738
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
@@ -60,7 +61,8 @@ SciMLBase = "1.3"
6061
Setfield = "0.7"
6162
SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0"
6263
StaticArrays = "0.10, 0.11, 0.12, 1.0"
63-
SymbolicUtils = "0.8"
64+
SymbolicUtils = "0.8.3"
65+
Symbolics = "0.1"
6466
TreeViews = "0.3"
6567
UnPack = "0.1, 1.0"
6668
Unitful = "1.1"

src/ModelingToolkit.jl

Lines changed: 14 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module ModelingToolkit
22

33
using DiffEqBase, SciMLBase
4-
using Distributed
54
using StaticArrays, LinearAlgebra, SparseArrays, LabelledArrays
65
using Latexify, Unitful, ArrayInterface
76
using MacroTools
@@ -25,159 +24,29 @@ RuntimeGeneratedFunctions.init(@__MODULE__)
2524
using RecursiveArrayTools
2625

2726
import SymbolicUtils
28-
import SymbolicUtils: Term, Add, Mul, Pow, Sym, FnType,
29-
@rule, Rewriters, substitute, similarterm,
30-
promote_symtype
31-
27+
import SymbolicUtils: istree, arguments, operation, similarterm, promote_symtype,
28+
Symbolic, Term, Add, Mul, Pow, Sym, FnType,
29+
@rule, Rewriters, substitute
30+
using SymbolicUtils.Code
3231
import SymbolicUtils.Code: toexpr
33-
3432
import SymbolicUtils.Rewriters: Chain, Postwalk, Prewalk, Fixpoint
33+
34+
using Reexport
35+
@reexport using Symbolics
36+
export @derivatives
37+
using Symbolics: _parse_vars, value, makesym, @derivatives, get_variables,
38+
exprs_occur_in
39+
import Symbolics: rename, get_variables!, _solve, hessian_sparsity,
40+
jacobian_sparsity, islinear
41+
3542
import DiffEqBase: @add_kwonly
36-
using LinearAlgebra: LU, BlasInt
3743

3844
import LightGraphs: SimpleDiGraph, add_edge!
3945

4046
import TreeViews
4147

4248
using Requires
4349

44-
export Num, Variable
45-
"""
46-
$(TYPEDEF)
47-
48-
Wrap anything in a type that is a subtype of Real
49-
"""
50-
struct Num <: Real
51-
val
52-
end
53-
54-
const show_numwrap = Ref(false)
55-
56-
Num(x::Num) = x # ideally this should never be called
57-
(n::Num)(args...) = Num(value(n)(map(value,args)...))
58-
value(x) = x
59-
value(x::Num) = x.val
60-
61-
SciMLBase.issymbollike(::Num) = true
62-
SciMLBase.issymbollike(::SymbolicUtils.Symbolic) = true
63-
64-
SymbolicUtils.@number_methods(Num,
65-
Num(f(value(a))),
66-
Num(f(value(a), value(b))))
67-
68-
for C in [Complex, Complex{Bool}]
69-
@eval begin
70-
Base.:*(x::Num, z::$C) = Complex(x * real(z), x * imag(z))
71-
Base.:*(z::$C, x::Num) = Complex(real(z) * x, imag(z) * x)
72-
end
73-
end
74-
75-
Base.:+(x::Num, z::Complex) = Complex(x + real(z), imag(z))
76-
Base.:+(z::Complex, x::Num) = Complex(real(z) + x, imag(z))
77-
Base.:-(x::Num, z::Complex) = Complex(x - real(z), -imag(z))
78-
Base.:-(z::Complex, x::Num) = Complex(real(z) - x, imag(z))
79-
80-
function Base.inv(z::Complex{Num})
81-
a, b = reim(z)
82-
den = a^2 + b^2
83-
Complex(a/den, -b/den)
84-
end
85-
function Base.:/(x::Complex{Num}, y::Complex{Num})
86-
a, b = reim(x)
87-
c, d = reim(y)
88-
den = c^2 + d^2
89-
Complex((a*c + b*d)/den, (b*c - a*d)/den)
90-
end
91-
92-
function Base.show(io::IO, z::Complex{<:Num})
93-
r, i = reim(z)
94-
compact = get(io, :compact, false)
95-
show(io, r)
96-
print(io, (compact ? "+" : " + ") * "(")
97-
show(io, i)
98-
print(io, ")*im")
99-
end
100-
101-
SymbolicUtils.simplify(n::Num; kw...) = Num(SymbolicUtils.simplify(value(n); kw...))
102-
103-
SymbolicUtils.symtype(n::Num) = symtype(n.val)
104-
105-
function Base.iszero(x::Num)
106-
_x = SymbolicUtils.to_mpoly(value(x))[1]
107-
return (_x isa Number || _x isa SymbolicUtils.MPoly) && iszero(_x)
108-
end
109-
110-
import SymbolicUtils: <ₑ, Symbolic, Term, operation, arguments
111-
112-
Base.show(io::IO, n::Num) = show_numwrap[] ? print(io, :(Num($(value(n))))) : Base.show(io, value(n))
113-
114-
Base.promote_rule(::Type{<:Number}, ::Type{<:Num}) = Num
115-
Base.promote_rule(::Type{<:Symbolic{<:Number}}, ::Type{<:Num}) = Num
116-
function Base.getproperty(t::Union{Add, Mul, Pow, Term}, f::Symbol)
117-
if f === :op
118-
Base.depwarn("`x.op` is deprecated, use `operation(x)` instead", :getproperty, force=true)
119-
operation(t)
120-
elseif f === :args
121-
Base.depwarn("`x.args` is deprecated, use `arguments(x)` instead", :getproperty, force=true)
122-
arguments(t)
123-
else
124-
getfield(t, f)
125-
end
126-
end
127-
<(s::Num, x) = value(s) <value(x)
128-
<(s, x::Num) = value(s) <value(x)
129-
<(s::Num, x::Num) = value(s) <value(x)
130-
131-
for T in (Integer, Rational)
132-
@eval Base.:(^)(n::Num, i::$T) = Num(value(n)^i)
133-
end
134-
135-
macro num_method(f, expr, Ts=nothing)
136-
if Ts === nothing
137-
Ts = [Any]
138-
else
139-
@assert Ts.head == :tuple
140-
# e.g. a tuple or vector
141-
Ts = Ts.args
142-
end
143-
144-
ms = [quote
145-
$f(a::$T, b::$Num) = $expr
146-
$f(a::$Num, b::$T) = $expr
147-
end for T in Ts]
148-
quote
149-
$f(a::$Num, b::$Num) = $expr
150-
$(ms...)
151-
end |> esc
152-
end
153-
154-
"""
155-
tosymbolic(a::Union{Sym,Num}) -> Sym{Real}
156-
tosymbolic(a::T) -> T
157-
"""
158-
tosymbolic(a::Num) = tosymbolic(value(a))
159-
tosymbolic(a::Sym) = tovar(a)
160-
tosymbolic(a) = a
161-
@num_method Base.isless (val = isless(tosymbolic(a), tosymbolic(b)); val isa Bool ? val : Num(val)) (Real,)
162-
@num_method Base.:(<) (val = tosymbolic(a) < tosymbolic(b) ; val isa Bool ? val : Num(val)) (Real,)
163-
@num_method Base.:(<=) (val = tosymbolic(a) <= tosymbolic(b) ; val isa Bool ? val : Num(val)) (Real,)
164-
@num_method Base.:(>) (val = tosymbolic(a) > tosymbolic(b) ; val isa Bool ? val : Num(val)) (Real,)
165-
@num_method Base.:(>=) (val = tosymbolic(a) >= tosymbolic(b) ; val isa Bool ? val : Num(val)) (Real,)
166-
@num_method Base.:(==) (val = tosymbolic(a) == tosymbolic(b) ; val isa Bool ? val : Num(val)) (AbstractFloat,Number)
167-
@num_method Base.isequal isequal(tosymbolic(a), tosymbolic(b)) (AbstractFloat, Number, Symbolic)
168-
169-
Base.hash(x::Num, h::UInt) = hash(value(x), h)
170-
171-
Base.convert(::Type{Num}, x::Symbolic{<:Number}) = Num(x)
172-
Base.convert(::Type{Num}, x::Number) = Num(x)
173-
Base.convert(::Type{Num}, x::Num) = x
174-
175-
Base.convert(::Type{<:Array{Num}}, x::AbstractArray) = map(Num, x)
176-
Base.convert(::Type{<:Array{Num}}, x::AbstractArray{Num}) = x
177-
Base.convert(::Type{Sym}, x::Num) = value(x) isa Sym ? value(x) : error("cannot convert $x to Sym")
178-
179-
LinearAlgebra.lu(x::Array{Num}; check=true, kw...) = sym_lu(x; check=check)
180-
18150
"""
18251
$(TYPEDEF)
18352
@@ -211,16 +80,11 @@ include("bipartite_graph.jl")
21180
using .BipartiteGraphs
21281

21382
include("variables.jl")
214-
include("context_dsl.jl")
215-
include("differentials.jl")
83+
include("parameters.jl")
21684

217-
include("equations.jl")
21885
include("utils.jl")
219-
include("linearity.jl")
220-
include("solve.jl")
22186
include("direct.jl")
22287
include("domains.jl")
223-
include("register_function.jl")
22488

22589
include("systems/abstractsystem.jl")
22690

@@ -252,7 +116,6 @@ include("systems/reduction.jl")
252116

253117
include("latexify_recipes.jl")
254118
include("build_function.jl")
255-
include("extra_functions.jl")
256119

257120
export ODESystem, ODEFunction, ODEFunctionExpr, ODEProblemExpr
258121
export SDESystem, SDEFunction, SDEFunctionExpr, SDESystemExpr
@@ -292,7 +155,6 @@ export asgraph, asdigraph
292155
export toexpr, get_variables
293156
export simplify, substitute
294157
export build_function
295-
export @register
296158
export modelingtoolkitize
297159
export @variables, @parameters
298160

0 commit comments

Comments
 (0)