@@ -53,9 +53,28 @@ is_derivative(O::Term) = isa(O.op, Differential)
5353is_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"""
6079get_variables (e:: Num , varlist= nothing ) = get_variables (value (e), varlist)
6180get_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"""
98132substitute (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"""
156191tovar (s:: Sym{<:Parameter} ) = Sym {symtype(s)} (s. name)
157192tovar (s:: Sym ) = s
@@ -167,6 +202,15 @@ tosymbol(t::Num; kwargs...) = tosymbol(value(t); kwargs...)
167202Convert `x` to a symbol. `states` are the states of a system, and `escape`
168203means if the target has escapes like `val"y⦗t⦘"`. If `escape` then it will only
169204output `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"""
171215function 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" )
189233end
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+ """
191250makesym (t:: Symbolic ; kwargs... ) = Sym {symtype(t)} (tosymbol (t; kwargs... ))
192251makesym (t:: Num ; kwargs... ) = makesym (value (t); kwargs... )
193252
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"""
229293function diff2term (O)
230294 isa (O, Term) || return O
0 commit comments