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. It is thus encouraged to use standard Julia for performing many of the manipulation on the IR. 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 — Function
substitute(expr, s)
Performs the substitution on expr according to rule(s) s.
Examples
julia> @variables t x y z(t)
4-element Vector{Num}:
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)
#
SymbolicUtils.simplify — Function
simplify(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.
By default, simplify will assume denominators are not zero and allow cancellation in fractions. Pass simplify_fractions=false to prevent this.
Documentation for rewriter can be found here, using the @rule macro or the @acrule macro from SymbolicUtils.jl.
Additional Manipulation Functions
Other additional manipulation functions are given below.
#
Symbolics.get_variables — Function
get_variables(O) -> Vector{BasicSymbolic}
Returns the variables in the expression. Note that the returned variables are not wrapped in the Num type.
Examples
julia> @variables t x y z(t)
4-element Vector{Num}:
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 — Function
tosymbol(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 is false, then it will only output y instead of y(t).
Examples
julia> @variables t z(t)
2-element Vector{Num}:
t
z(t)
julia> Symbolics.tosymbol(z)
Symbol("z(t)")
julia> Symbolics.tosymbol(z; escape=false)
:z
#
Symbolics.diff2term — Function
diff2term(x) -> Symbolic
Convert a differential variable to a Term. Note that it only takes a Term not a Num.
julia> @variables x t u(x, t) z(t)[1:2]; Dt = Differential(t); Dx = Differential(x);
julia> Symbolics.diff2term(Symbolics.value(Dx(Dt(u))))
uˍtx(x, t)
julia> Symbolics.diff2term(Symbolics.value(Dt(z[1])))
var"z(t)[1]ˍt"
#
Symbolics.solve_for — Function
solve_for(eq, var; 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.
Examples
julia> @variables x y
2-element Vector{Num}:
x
y
julia> Symbolics.solve_for(x + y ~ 0, x)
-y
julia> Symbolics.solve_for([x + y ~ 0, x - y ~ 2], [x, y])
2-element Vector{Float64}:
1.0
-1.0
#
Symbolics.degree — Function
degree(p, sym=nothing)
Extract the degree of p with respect to sym.
Examples
julia> @variables x;
julia> Symbolics.degree(x^0)
0
julia> Symbolics.degree(x)
1
julia> Symbolics.degree(x^2)
2
#
Symbolics.coeff — Function
coeff(p, sym=nothing)
Extract the coefficient of p with respect to sym. Note that p might need to be expanded and/or simplified with expand and/or simplify.
Examples
julia> @variables a x y;
julia> Symbolics.coeff(2a, x)
0
julia> Symbolics.coeff(3x + 2y, y)
2
julia> Symbolics.coeff(x^2 + y, x^2)
1