@@ -21,7 +21,7 @@ RuntimeGeneratedFunctions.init(@__MODULE__)
21
21
using RecursiveArrayTools
22
22
23
23
import SymbolicUtils
24
- import SymbolicUtils: to_symbolic, FnType, @rule , Rewriters, Term
24
+ import SymbolicUtils: Term, Sym, to_symbolic, FnType, @rule , Rewriters, substitute, similarterm
25
25
26
26
using LinearAlgebra: LU, BlasInt
27
27
@@ -31,12 +31,99 @@ import TreeViews
31
31
32
32
using Requires
33
33
34
+ export Num, Variable
34
35
"""
35
36
$(TYPEDEF)
36
37
37
- Base type for a symbolic expression.
38
+ Wrap anything in a type that is a subtype of Real
38
39
"""
39
- abstract type Expression <: Number end
40
+ struct Num <: Real
41
+ val
42
+ end
43
+
44
+ const show_numwrap = Ref (false )
45
+
46
+ Num (x:: Num ) = x # ideally this should never be called
47
+ (n:: Num )(args... ) = Num (value (n)(map (value,args)... ))
48
+ value (x) = x
49
+ value (x:: Num ) = x. val
50
+
51
+
52
+ using SymbolicUtils: to_symbolic
53
+ SymbolicUtils. to_symbolic (n:: Num ) = value (n)
54
+ SymbolicUtils. @number_methods (Num,
55
+ Num (f (value (a))),
56
+ Num (f (value (a), value (b))))
57
+
58
+ SymbolicUtils. simplify (n:: Num ; kw... ) = Num (SymbolicUtils. simplify (value (n); kw... ))
59
+
60
+ SymbolicUtils. symtype (n:: Num ) = symtype (n. val)
61
+
62
+ function Base. iszero (x:: Num )
63
+ _x = SymbolicUtils. to_mpoly (value (x))[1 ]
64
+ return (_x isa Number || _x isa SymbolicUtils. MPoly) && iszero (_x)
65
+ end
66
+
67
+ import SymbolicUtils: < ₑ, Symbolic, Term, operation, arguments
68
+
69
+ Base. show (io:: IO , n:: Num ) = show_numwrap[] ? print (io, :(Num ($ (value (n))))) : Base. show (io, value (n))
70
+
71
+ Base. promote_rule (:: Type{<:Number} , :: Type{<:Num} ) = Num
72
+ Base. promote_rule (:: Type{<:Symbolic{<:Number}} , :: Type{<:Num} ) = Num
73
+ Base. getproperty (t:: Term , f:: Symbol ) = f === :op ? operation (t) : f === :args ? arguments (t) : getfield (t, f)
74
+ < ₑ (s:: Num , x) = value (s) < ₑ value (x)
75
+ < ₑ (s, x:: Num ) = value (s) < ₑ value (x)
76
+ < ₑ (s:: Num , x:: Num ) = value (s) < ₑ value (x)
77
+
78
+ for T in (Integer, Rational)
79
+ @eval Base.:(^ )(n:: Num , i:: $T ) = Num (Term {symtype(n)} (^ , [value (n),i]))
80
+ end
81
+
82
+ macro num_method (f, expr, Ts= nothing )
83
+ if Ts === nothing
84
+ Ts = [Any]
85
+ else
86
+ @assert Ts. head == :tuple
87
+ # e.g. a tuple or vector
88
+ Ts = Ts. args
89
+ end
90
+
91
+ ms = [quote
92
+ $ f (a:: $T , b:: $Num ) = $ expr
93
+ $ f (a:: $Num , b:: $T ) = $ expr
94
+ end for T in Ts]
95
+ quote
96
+ $ f (a:: $Num , b:: $Num ) = $ expr
97
+ $ (ms... )
98
+ end |> esc
99
+ end
100
+
101
+ """
102
+ tosymbolic(a::Union{Sym,Num}) -> Sym{Real}
103
+ tosymbolic(a::T) -> T
104
+ """
105
+ tosymbolic (a:: Num ) = tosymbolic (value (a))
106
+ tosymbolic (a:: Sym ) = tovar (a)
107
+ tosymbolic (a) = a
108
+ @num_method Base. isless isless (tosymbolic (a), tosymbolic (b)) (Real,)
109
+ @num_method Base.:(< ) (tosymbolic (a) < tosymbolic (b)) (Real,)
110
+ @num_method Base.:(<= ) (tosymbolic (a) <= tosymbolic (b)) (Real,)
111
+ @num_method Base.:(> ) (tosymbolic (a) > tosymbolic (b)) (Real,)
112
+ @num_method Base.:(>= ) (tosymbolic (a) >= tosymbolic (b)) (Real,)
113
+ @num_method Base. isequal isequal (tosymbolic (a), tosymbolic (b)) (Number, Symbolic)
114
+ @num_method Base.:(== ) tosymbolic (a) == tosymbolic (b) (Number,)
115
+
116
+ Base. hash (x:: Num , h:: UInt ) = hash (value (x), h)
117
+
118
+ Base. convert (:: Type{Num} , x:: Symbolic{<:Number} ) = Num (x)
119
+ Base. convert (:: Type{Num} , x:: Number ) = Num (x)
120
+ Base. convert (:: Type{Num} , x:: Num ) = x
121
+
122
+ Base. convert (:: Type{<:Array{Num}} , x:: AbstractArray ) = map (Num, x)
123
+ Base. convert (:: Type{<:Array{Num}} , x:: AbstractArray{Num} ) = x
124
+ Base. convert (:: Type{Sym} , x:: Num ) = value (x) isa Sym ? value (x) : error (" cannot convert $x to Sym" )
125
+
126
+ LinearAlgebra. lu (x:: Array{Num} ; kw... ) = lu (x, Val {false} (); kw... )
40
127
41
128
"""
42
129
$(TYPEDEF)
46
133
abstract type AbstractSystem end
47
134
abstract type AbstractODESystem <: AbstractSystem end
48
135
49
- Base. promote_rule (:: Type{<:Number} ,:: Type{<:Expression} ) = Expression
50
- Base. zero (:: Type{<:Expression} ) = Constant (0 )
51
- Base. zero (:: Expression ) = Constant (0 )
52
- Base. one (:: Type{<:Expression} ) = Constant (1 )
53
- Base. one (:: Expression ) = Constant (1 )
54
- Base. oneunit (:: Expression ) = Constant (1 )
55
- Base. oneunit (:: Type{<:Expression} ) = Constant (1 )
56
-
57
136
"""
58
137
$(TYPEDSIGNATURES)
59
138
@@ -77,28 +156,15 @@ function parameters end
77
156
78
157
include (" variables.jl" )
79
158
include (" context_dsl.jl" )
80
- include (" operations.jl" )
81
159
include (" differentials.jl" )
82
160
83
- function Base. convert (:: Type{Variable} ,x:: Operation )
84
- if x. op isa Variable
85
- x. op
86
- elseif x. op isa Differential
87
- var = x. args[1 ]. op
88
- rename (var,Symbol (var. name,:ˍ ,x. op. x))
89
- else
90
- throw (error (" This Operation is not a Variable" ))
91
- end
92
- end
93
-
94
161
include (" equations.jl" )
95
- include (" function_registration.jl" )
96
- include (" simplify.jl" )
97
162
include (" utils.jl" )
98
163
include (" linearity.jl" )
99
164
include (" solve.jl" )
100
165
include (" direct.jl" )
101
166
include (" domains.jl" )
167
+ include (" register_function.jl" )
102
168
103
169
include (" systems/abstractsystem.jl" )
104
170
@@ -145,8 +211,8 @@ export Reaction, ReactionSystem, ismassaction, oderatelaw, jumpratelaw
145
211
export Differential, expand_derivatives, @derivatives
146
212
export IntervalDomain, ProductDomain, ⊗ , CircleDomain
147
213
export Equation, ConstrainedEquation
148
- export Operation, Expression, Variable
149
- export independent_variable, states, controls, parameters, equations, pins, observed
214
+ export Term, Sym
215
+ export independent_variable, states, parameters, equations, controls , pins, observed
150
216
151
217
export calculate_jacobian, generate_jacobian, generate_function
152
218
export calculate_tgrad, generate_tgrad
@@ -160,7 +226,7 @@ export BipartiteGraph, equation_dependencies, variable_dependencies
160
226
export eqeq_dependencies, varvar_dependencies
161
227
export asgraph, asdigraph
162
228
163
- export simplified_expr , rename, get_variables
229
+ export toexpr , rename, get_variables
164
230
export simplify, substitute
165
231
export build_function
166
232
export @register
0 commit comments