Skip to content

Commit 83312e3

Browse files
authoredMar 1, 2021
Merge pull request #825 from SciML/myb/symbolics
Use Symbolics
2 parents e69f88e + a92e5a6 commit 83312e3

17 files changed

+54
-2198
lines changed
 

‎Project.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelingToolkit"
22
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
33
authors = ["Chris Rackauckas <accounts@chrisrackauckas.com>"]
4-
version = "5.9.1"
4+
version = "5.10.0"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
@@ -33,6 +33,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
3333
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
3434
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
3535
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
36+
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
3637
TreeViews = "a2a6695c-b41b-5b7d-aed9-dbfdeacea5d7"
3738
UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
3839
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
@@ -62,7 +63,8 @@ SciMLBase = "1.3"
6263
Setfield = "0.7"
6364
SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0"
6465
StaticArrays = "0.10, 0.11, 0.12, 1.0"
65-
SymbolicUtils = "0.8"
66+
SymbolicUtils = "0.8.3"
67+
Symbolics = "0.1"
6668
TreeViews = "0.3"
6769
UnPack = "0.1, 1.0"
6870
Unitful = "1.1"

‎src/ModelingToolkit.jl

+25-150
Original file line numberDiff line numberDiff line change
@@ -25,159 +25,45 @@ RuntimeGeneratedFunctions.init(@__MODULE__)
2525
using RecursiveArrayTools
2626

2727
import SymbolicUtils
28-
import SymbolicUtils: Term, Add, Mul, Pow, Sym, FnType,
29-
@rule, Rewriters, substitute, similarterm,
30-
promote_symtype
31-
28+
import SymbolicUtils: istree, arguments, operation, similarterm, promote_symtype,
29+
Symbolic, Term, Add, Mul, Pow, Sym, FnType,
30+
@rule, Rewriters, substitute
31+
using SymbolicUtils.Code
3232
import SymbolicUtils.Code: toexpr
33-
3433
import SymbolicUtils.Rewriters: Chain, Postwalk, Prewalk, Fixpoint
34+
35+
using Reexport
36+
@reexport using Symbolics
37+
export @derivatives
38+
using Symbolics: _parse_vars, value, makesym, @derivatives, get_variables,
39+
exprs_occur_in, solve_for, build_expr
40+
import Symbolics: rename, get_variables!, _solve, hessian_sparsity,
41+
jacobian_sparsity, islinear, _iszero, _isone,
42+
tosymbol, lower_varname, diff2term, var_from_nested_derivative,
43+
BuildTargets, JuliaTarget, StanTarget, CTarget, MATLABTarget,
44+
ParallelForm, SerialForm, MultithreadedForm, build_function,
45+
unflatten_long_ops
46+
3547
import DiffEqBase: @add_kwonly
36-
using LinearAlgebra: LU, BlasInt
3748

3849
import LightGraphs: SimpleDiGraph, add_edge!
3950

4051
import TreeViews
4152

4253
using Requires
4354

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}]
55+
for fun in [:toexpr]
6956
@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
57+
function $fun(eq::Equation; kw...)
58+
Expr(:(=), $fun(eq.lhs; kw...), $fun(eq.rhs; kw...))
59+
end
9160

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
61+
$fun(eqs::AbstractArray; kw...) = map(eq->$fun(eq; kw...), eqs)
62+
$fun(x::Integer; kw...) = x
63+
$fun(x::AbstractFloat; kw...) = x
14264
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
15265
end
15366

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-
18167
"""
18268
$(TYPEDEF)
18369
@@ -211,16 +97,10 @@ include("bipartite_graph.jl")
21197
using .BipartiteGraphs
21298

21399
include("variables.jl")
214-
include("context_dsl.jl")
215-
include("differentials.jl")
100+
include("parameters.jl")
216101

217-
include("equations.jl")
218102
include("utils.jl")
219-
include("linearity.jl")
220-
include("solve.jl")
221-
include("direct.jl")
222103
include("domains.jl")
223-
include("register_function.jl")
224104

225105
include("systems/abstractsystem.jl")
226106

@@ -252,10 +132,6 @@ include("systems/alias_elimination.jl")
252132
include("structural_transformation/StructuralTransformations.jl")
253133
@reexport using .StructuralTransformations
254134

255-
include("latexify_recipes.jl")
256-
include("build_function.jl")
257-
include("extra_functions.jl")
258-
259135
export ODESystem, ODEFunction, ODEFunctionExpr, ODEProblemExpr
260136
export SDESystem, SDEFunction, SDEFunctionExpr, SDESystemExpr
261137
export SystemStructure
@@ -296,7 +172,6 @@ export asgraph, asdigraph
296172
export toexpr, get_variables
297173
export simplify, substitute
298174
export build_function
299-
export @register
300175
export modelingtoolkitize
301176
export @variables, @parameters
302177
export @named

2 commit comments

Comments
 (2)

YingboMa commented on Mar 1, 2021

@YingboMa
MemberAuthor

JuliaRegistrator commented on Mar 1, 2021

@JuliaRegistrator

Registration pull request created: JuliaRegistries/General/31052

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v5.10.0 -m "<description of version>" 83312e349ca6e51c67a7229b969b6b0ca9902078
git push origin v5.10.0
Please sign in to comment.