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 CASComputer Algebra System, modern and highly performant, geared towards symbolic computations, mathematics, modeling, and simulation.

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, x>0x>0 is an example of a truetrue symbolic assertion if x=33x = 33.

  • Symbolic statement: It is considered a particular case of symbolic assertions but without variables. For example, 2+3=62+3=6 is a falsefalse symbolic statement, and it is falsefalse because the arithmetic expression 2+32+3 evaluates to 55, not 66.

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 b
display(2b+a+b)

Let's go through the code widget above:

  • Line 1: We load the Symbolics.jl package.

  • Line 2: We declare two symbolic variables a and b using the @variables macro.

  • 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 y
ex1 = x + 2y ~ 10
ex2 = 2x ~ 8
vals = 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.jl package.

  • Line 2: We declare two symbolic variables x and y using the @variables macro.

  • 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 x and y.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved