Skip to content

Commit 68ea68b

Browse files
Merge pull request #672 from SciML/myb/docs
Update docs
2 parents 9d38a5f + cdc9890 commit 68ea68b

File tree

4 files changed

+127
-15
lines changed

4 files changed

+127
-15
lines changed

docs/src/IR.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ at the top, an `Equation`, normally written as `op1 ~ op2`, defines the
99
symbolic equality between two operations.
1010

1111
### Types
12+
`Sym`, `Term`, and `FnType` are from `SymbolicUtils.jl`. For more details,
13+
checkout https://juliasymbolics.github.io/SymbolicUtils.jl/api/. Note that in
14+
ModelingToolkit, we always use `Sym{Real}`, `Term{Real}`, and
15+
`FnType{Tuple{Any}, Real}`. To get the arguments of a `Term` use
16+
`arguments(t::Term)`, and to get the operation of a `Term` use
17+
`operation(t::Term)`.
1218

1319
```@docs
14-
Sym
15-
Term
1620
Equation
1721
```
1822

@@ -28,6 +32,21 @@ By default, the `@variables` and `@parameters` functions return Num-wrapped
2832
objects so as to allow calling functions which are restricted to `Number` or
2933
`Real`.
3034

35+
```julia
36+
julia> @parameters t; @variables x y z(t);
37+
38+
julia> ModelingToolkit.operation(ModelingToolkit.value(x + y))
39+
+ (generic function with 377 methods)
40+
41+
julia> ModelingToolkit.operation(ModelingToolkit.value(z))
42+
z(::Any)::Real
43+
44+
julia> ModelingToolkit.arguments(ModelingToolkit.value(x + y))
45+
2-element Vector{Sym{Real}}:
46+
x
47+
y
48+
```
49+
3150
### Function Registration
3251

3352
The ModelingToolkit graph only allowed for registered Julia functions for the
@@ -113,7 +132,11 @@ and easily understandable to all Julia programmers.
113132
Other additional manipulation functions are given below.
114133

115134
```@docs
116-
simplify_constants
117135
get_variables
118-
substitute_expr!
136+
substitute
137+
tovar
138+
toparam
139+
tosymbol
140+
makesym
141+
diff2term
119142
```

docs/src/tutorials/symbolic_functions.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,24 @@ macro-free usage. For example:
385385
x = Num(Sym{Float64}(:x))
386386
y = Num(Sym{Float64}(:y))
387387
x+y^2.0 # isa Num
388+
389+
t = Num(Variable{ModelingToolkit.Parameter{Real}}(:t)) # independent variables are treated as known
390+
α = Num(Variable{ModelingToolkit.Parameter{Real}}()) # parameters are known
391+
σ = Num(Variable{ModelingToolkit.FnType{Tuple{Any},Real}}()) # left uncalled, since it is used as a function
392+
w = Num(Variable{ModelingToolkit.FnType{Tuple{Any},Real}}(:w)) # unknown, left uncalled
393+
x = Num(Variable{ModelingToolkit.FnType{Tuple{Any},Real}}(:x))(t) # unknown, depends on `t`
394+
y = Num(Variable(:y)) # unknown, no dependents
395+
z = Num(Variable{ModelingToolkit.FnType{NTuple{3,Any},Real}}(:z))(t, α, x) # unknown, multiple arguments
396+
β₁ = Num(Variable(, 1)) # with index 1
397+
β₂ = Num(Variable(, 2)) # with index 2
398+
399+
expr = β₁ * x + y^α + σ(3) * (z - t) - β₂ * w(t - 1)
388400
```
389401

390-
Does what you'd expect. The reference documentation shows how to
391-
define any of the quantities in such a way that the names can come
392-
from runtime values.
402+
Does what you'd expect. Note that `Variable` is simply a convenient function for
403+
making variables with indices that always returns `Sym`. The reference
404+
documentation shows how to define any of the quantities in such a way that the
405+
names can come from runtime values.
393406

394407
If we need to use this to generate new Julia code, we can simply
395408
convert the output to an `Expr`:

src/register_function.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
"""
2+
@register(expr, Ts = [Num, Symbolic, Real])
3+
4+
Overload approperate methods such that ModelingToolkit can stop tracing into the
5+
registered function.
6+
7+
# Examples
8+
```julia
9+
@register foo(x, y)
10+
@register goo(x, y::Int) # `y` is not overloaded to take symbolic objects
11+
```
12+
"""
113
macro register(expr, Ts = [Num, Symbolic, Real])
214
@assert expr.head == :call
315

src/utils.jl

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,28 @@ is_derivative(O::Term) = isa(O.op, Differential)
5353
is_derivative(::Any) = false
5454

5555
"""
56-
get_variables(O)
56+
get_variables(O) -> Vector{Union{Sym, Term}}
5757
58-
Returns the variables in the expression
58+
Returns the variables in the expression. Note that the returned variables are
59+
not wrapped in the `Num` type.
60+
61+
# Examples
62+
```julia
63+
julia> @parameters t
64+
(t,)
65+
66+
julia> @variables x y z(t)
67+
(x, y, z(t))
68+
69+
julia> ex = x + y + sin(z)
70+
(x + y) + sin(z(t))
71+
72+
julia> ModelingToolkit.get_variables(ex)
73+
3-element Vector{Any}:
74+
x
75+
y
76+
z(t)
77+
```
5978
"""
6079
get_variables(e::Num, varlist=nothing) = get_variables(value(e), varlist)
6180
get_variables!(vars, e, varlist=nothing) = vars
@@ -89,11 +108,26 @@ modified_states!(mstates, e::Equation, statelist=nothing) = get_variables!(mstat
89108
# variable substitution
90109
# Piracy but mild
91110
"""
92-
substitute(expr, s::Pair)
93-
substitute(expr, s::Dict)
94-
substitute(expr, s::Vector)
111+
substitute(expr, s::Pair)
112+
substitute(expr, s::Dict)
113+
substitute(expr, s::Vector)
114+
115+
Performs the substitution on `expr` according to rule(s) `s`.
116+
117+
# Examples
118+
```julia
119+
julia> @parameters t
120+
(t,)
121+
122+
julia> @variables x y z(t)
123+
(x, y, z(t))
124+
125+
julia> ex = x + y + sin(z)
126+
(x + y) + sin(z(t))
95127
96-
Performs the substitution `Num => val` on the `expr` Num.
128+
julia> substitute(ex, Dict([x => z, sin(z) => z^2]))
129+
(z(t) + y) + (z(t) ^ 2)
130+
```
97131
"""
98132
substitute(expr::Num, s::Union{Pair, Vector, Dict}; kw...) = Num(substituter(s)(value(expr); kw...))
99133
# TODO: move this to SymbolicUtils
@@ -150,8 +184,9 @@ toparam(s::Sym{<:Parameter}) = s
150184

151185
"""
152186
tovar(s::Sym) -> Sym{Real}
187+
tovar(s::Sym{<:Parameter}) -> Sym{Real}
153188
154-
Maps the variable to a variable (state).
189+
Maps the variable to a state.
155190
"""
156191
tovar(s::Sym{<:Parameter}) = Sym{symtype(s)}(s.name)
157192
tovar(s::Sym) = s
@@ -167,6 +202,15 @@ tosymbol(t::Num; kwargs...) = tosymbol(value(t); kwargs...)
167202
Convert `x` to a symbol. `states` are the states of a system, and `escape`
168203
means if the target has escapes like `val"y⦗t⦘"`. If `escape` then it will only
169204
output `y` instead of `y⦗t⦘`.
205+
206+
# Examples
207+
```julia
208+
julia> @parameters t; @variables z(t)
209+
(z(t),)
210+
211+
julia> ModelingToolkit.tosymbol(z)
212+
Symbol("z⦗t⦘")
213+
```
170214
"""
171215
function tosymbol(t::Term; states=nothing, escape=true)
172216
if t.op isa Sym
@@ -188,6 +232,21 @@ function tosymbol(t::Term; states=nothing, escape=true)
188232
error("Cannot convert $t to a symbol")
189233
end
190234

235+
"""
236+
makesym(x::Union{Num,Symbolic}, kwargs...) -> Sym
237+
238+
`makesym` takes the same arguments as [`tosymbol`](@ref), but it converts a
239+
`Term` in the form of `x(t)` to a `Sym` in the form of `x⦗t⦘`.
240+
241+
# Examples
242+
```julia
243+
julia> @parameters t; @variables x(t)
244+
(x(t),)
245+
246+
julia> ModelingToolkit.makesym(x)
247+
x⦗t⦘
248+
```
249+
"""
191250
makesym(t::Symbolic; kwargs...) = Sym{symtype(t)}(tosymbol(t; kwargs...))
192251
makesym(t::Num; kwargs...) = makesym(value(t); kwargs...)
193252

@@ -224,7 +283,12 @@ end
224283
diff2term(x::Term) -> Term
225284
diff2term(x) -> x
226285
227-
diff2term(D(D(x(t)))) -> xˍtt(t)
286+
Convert a differential variable to a `Symbol`. Note that it only takes a `Term`
287+
not a `Num`.
288+
```julia
289+
julia> ModelingToolkit.diff2term(ModelingToolkit.value(D(D(x))))
290+
xˍtt(t)
291+
```
228292
"""
229293
function diff2term(O)
230294
isa(O, Term) || return O

0 commit comments

Comments
 (0)