What is symbolic math in Julia?
Symbolic math provides a simple, intuitive, and comprehensive environment for interactive learning and applying math operations such as calculus, algebra, and differential equations.
Julia's ecosystem leverages several libraries like Symbolics, Symata, SymPy, and many others that provide symbolic math interfaces.
Symbolics is a pure Julia
Before moving forward, let's define some important terms to get a clearer picture.
What are symbolic expressions?
A symbolic language is composed of expressions similar to the ones written by mathematicians. In symbolic expressions, symbols are arranged according to specific rules. These expressions are divided into two main categories:
Symbolic assertion: It is a complete statement that acts like an assertion that may include variables. It is possible for a symbolic assertion to be true in some cases and false in others, depending on the values of the underlying variables. For example,
is an example of a symbolic assertion if . Symbolic statement: It is considered a particular case of symbolic assertions but without variables. For example,
is a symbolic statement, and it is because the arithmetic expression evaluates to , not .
Now after this introduction, let's delve into the world of symbolic numeric programming in Julia:
Code example 1
This example shows how to generate a symbolic expression in Julia.
using Symbolics@variables a bdisplay(2b+a+b)
Let's go through the code widget above:
Line 1: We load the
Symbolics.jlpackage.Line 2: We declare two symbolic variables
aandbusing the@variablesmacro.Line 3: We generate and display a symbolic equation out of the following expression:
2b+a+b
Code example 2
This example shows how to solve symbolic equations in Julia.
using Symbolics@variables x yex1 = x + 2y ~ 10ex2 = 2x ~ 8vals = Symbolics.solve_for([ex1,ex2],[x,y])println("x = ", vals[1])println("y = ", vals[2])
Let's go through the code widget above:
Line 1: We load the
Symbolics.jlpackage.Line 2: We declare two symbolic variables
xandyusing the@variablesmacro.Lines 3–4: We define two symbolic equations. Remember that the
~sign in symbolic equations represents the=operator.Line 5: We invoke the
solve_for()function to solve the linear equations provided as arguments and the previously defined symbolic variables.Lines 6–7: We print out the computed values of the symbolic variables
xandy.
Free Resources