Expression Manipulation
Symbolics.jl provides functionality for easily manipulating expressions. Most of the functionality comes by the expression objects obeying the standard mathematical semantics. For example, if one has A
a matrix of symbolic expressions wrapped in Num
, then A^2
calculates the expressions for the squared matrix. In that sense, it is encouraged that one uses standard Julia for performing a lot of the manipulation on the IR, as, for example, calculating the sparse form of the matrix via sparse(A)
is valid, legible, and easily understandable to all Julia programmers.
Functionality Inherited From SymbolicUtils.jl
SymbolicUtils.substitute
— Functionsubstitute(expr, s::Dict)
Performs the substitution on expr
according to rule(s) s
.
Examples
julia> @parameters t
(t,)
julia> @variables x y z(t)
(x, y, z(t))
julia> ex = x + y + sin(z)
(x + y) + sin(z(t))
julia> substitute(ex, Dict([x => z, sin(z) => z^2]))
(z(t) + y) + (z(t) ^ 2)
substitute(expr, dict)
substitute any subexpression that matches a key in dict
with the corresponding value.
SymbolicUtils.simplify
— Functionsimplify(x; expand=false,
threaded=false,
thread_subtree_cutoff=100,
rewriter=nothing)
Simplify an expression (x
) by applying rewriter
until there are no changes. expand=true
applies expand
in the beginning of each fixpoint iteration.
Additional Manipulation Functions
Other additional manipulation functions are given below.
Symbolics.get_variables
— Functionget_variables(O) -> Vector{Union{Sym, Term}}
Returns the variables in the expression. Note that the returned variables are not wrapped in the Num
type.
Examples
julia> @parameters t
(t,)
julia> @variables x y z(t)
(x, y, z(t))
julia> ex = x + y + sin(z)
(x + y) + sin(z(t))
julia> Symbolics.get_variables(ex)
3-element Vector{Any}:
x
y
z(t)
Symbolics.tosymbol
— Functiontosymbol(x::Union{Num,Symbolic}; states=nothing, escape=true) -> Symbol
Convert x
to a symbol. states
are the states of a system, and escape
means if the target has escapes like val"y(t)"
. If escape
then it will only output y
instead of y(t)
.
Examples
julia> @parameters t; @variables z(t)
(z(t),)
julia> Symbolics.tosymbol(z)
Symbol("z(t)")
Symbolics.makesym
— Functionmakesym(x::Union{Num,Symbolic}, kwargs...) -> Sym
makesym
takes the same arguments as tosymbol
, but it converts a Term
in the form of x(t)
to a Sym
in the form of x⦗t⦘
.
Examples
julia> @parameters t; @variables x(t)
(x(t),)
julia> Symbolics.makesym(x)
x⦗t⦘
Symbolics.diff2term
— Functiondiff2term(x::Term) -> Symbolic
diff2term(x) -> x
Convert a differential variable to a Term
. Note that it only takes a Term
not a Num
.
julia> @variables x t u(x, t); Dt = Differential(t); Dx = Differential(x);
julia> Symbolics.diff2term(Symbolics.value(Dx(Dt(u))))
uˍtx(x, t)
Symbolics.solve_for
— Functionsolve_for(eq::Any, var::Any; simplify, check) -> Any
Solve equation(s) eqs
for a set of variables vars
.
Assumes length(eqs) == length(vars)
Currently only works if all equations are linear. check
if the expr is linear w.r.t vars
.