SymbolicUtils.jl — home

SymbolicUtils is an practical symbolic programming utility in Julia. It lets you create, rewrite and simplify symbolic expressions, and generate Julia code from them.

Features:


Getting Started

Table of contents

Creating symbolic expressions

First, let's use the @syms macro to create a few symbols.

using SymbolicUtils

@syms w z α::Real β::Real
(w, z, α, β)

Type annotations are optional when creating symbols. Here α, β behave like Real numbers. w and z behave like Number, which is the default. You can use the symtype function to find the type of a symbol.

using SymbolicUtils: symtype

symtype(w), symtype(z),  symtype(α), symtype(β)
(Number, Number, Real, Real)

Note however that they are not subtypes of these types!

@show w isa Number
@show α isa Real
w isa Number = false
α isa Real = false

As their types are different:

@show typeof(w)
@show typeof(α)
typeof(w) = SymbolicUtils.Sym{Number, Nothing}
typeof(α) = SymbolicUtils.Sym{Real, Nothing}

(see this post for why they are all not just subtypes of Number)

You can do basic arithmetic on symbols to get symbolic expressions:

expr1 = α*sin(w)^2 + β*cos(z)^2
expr2 = α*cos(z)^2 + β*sin(w)^2

expr1 + expr2
α*(sin(w)^2) + α*(cos(z)^2) + β*(sin(w)^2) + β*(cos(z)^2)

SymbolicUtils automatically simplifies

2w + 3w - 3z + α
α + 5w - 3z

and reorders

(z + w)*(α + β)
(w + z)*(α + β)

expressions of type Symbolic{<:Number} (which includes Sym{Real}) when they are created. It also does constant elimination (including rational numbers)

5 + 2w - 3z + α - (β + 5//3) + 3w - 2 + 3//2 * β
(4//3) + α + (1//2)*β + 5w - 3z

It's worth remembering that the expression may be transformed with respect to the input when it's created.

Function-like symbols

Symbols can be defined to behave like functions. Both the input and output types for the function can be specified. Any application to that function will only admit either values of those types or symbols of the same symtype.

using SymbolicUtils
@syms f(x) g(x::Real, y::Real)::Real

f(z) + g(1, α) + sin(w)
sin(w) + f(z) + g(1, α)

This does not work since z is a Number, not a Real.

g(1, z)
Tuple{Int64, Number} is not a subtype of Tuple{Real, Real}.

This works because g "returns" a Real.

g(2//5, g(1, β))
g(2//5, g(1, β))

Expression interface

Symbolic expressions are of type Term{T}, Add{T}, Mul{T}, Pow{T} or Div{T} and denote some function call where one or more arguments are themselves such expressions or Syms. See more about the representation here.

All the expression types support the following:

See more on the interface here

Term rewriting

SymbolicUtils contains a rule-based rewriting language for easy pattern matching and rewriting of expression. There is also a combinator library to combine rules to chain, branch and loop over rules.

Simplification

By default +, * and ^ operations apply the most basic simplification upon construction of the expression.

The rules with which the canonical form of Symbolic{<:Number} terms are constructed are the next (where x isa Symbolic and c isa Number)

Here is an example of this

2 * (w+w+α+β + sin(z)^2 + cos(z)^2 - 1)
2α + 2β + 2(cos(z)^2) + 2(sin(z)^2) + 4w - 2

The simplify function applies a built-in set of rules to rewrite expressions in order to simplify it.

simplify(2 * (w+w+α+β + sin(z)^2 + cos(z)^2 - 1))
2α + 2β + 2(cos(z)^2) + 2(sin(z)^2) + 4w - 2

The rules in the default simplify applies simple constant elimination and trigonometric identities.

If you read the previous section on the rules DSL, you should be able to read and understand the rules that are used by simplify.

Code generation

Experimental feature

It is common to want to generate executable code from symbolic expressions and blocks of them. We are working on experimental support for turning Symbolic expressions into executable functions with specific focus on array input and output and performance which is critical to the Differential Equations ecosystem which is making heavy use of this package.

See Code generation for more about this.

Learn more

If you have a package that you would like to utilize rule-based rewriting in, look at the suggestions in the Interfacing section to find out how you can do that without any fundamental changes to your package. Look at the API documentation for docstrings about specific functions or macros.

Head over to the github repository to ask questions and report problems! Join the Zulip stream to chat!