Equana

Back to Tutorials

Symbolic Mathematics

Symbolic algebra, calculus, and equation solving

Run AllReset

Equana includes a powerful symbolic mathematics engine powered by SymEngine via WebAssembly. This tutorial covers how to create symbolic variables, manipulate expressions, solve equations, and perform calculus operations.

We'll work through examples interactively, with each code cell building on the previous. Run cells in sequence to see how variables persist across the notebook.

Creating Symbolic Variables

Symbolic variables are the building blocks of symbolic math. Unlike numeric variables, they represent abstract mathematical entities that can be manipulated algebraically.

FunctionDescriptionExample
sym("x")Create symbolic variablex = sym("x")
sym("x^2 + 1")Parse symbolic expressionf = sym("x^2 + 1")
sym(n)Convert number to symbolicsym(1)
Code [1]Run
# Create symbolic variables
x = sym("x")
y = sym("y")
z = sym("z")

# Display the variables
println(x)
println(y)
println(z)

Building Symbolic Expressions

Once you have symbolic variables, combine them using standard mathematical operators to build expressions. You can also parse expressions directly from strings.

Supported operators: +, -, *, /, ^

Supported functions: sin, cos, tan, exp, log, sqrt, and more.

Code [2]Run
x = sym("x")
y = sym("y")

# Build expressions using operators
f = x^2 + 2*x + 1
g = x*y + sin(x)

# Parse an expression from a string
h = sym("x^3 - 3*x + 2")

# Display the expressions
println(f)
println(g)
println(h)

Expanding Expressions

The expand function distributes products and expands powers of sums. This is useful for simplifying expressions into polynomial form.

Code [3]Run
x = sym("x")
y = sym("y")

# Expand (x + 1)^2
f = (x + 1)^2
println(expand(f))

# Expand (x + y)*(x - y)
g = (x + y)*(x - y)
println(expand(g))

# Expand a more complex expression
h = (x + 1)^3
println(expand(h))

Simplifying Expressions

The simplify function applies various algebraic rules to reduce expressions to simpler forms. It combines like terms, cancels common factors, and applies trigonometric identities.

Code [4]Run
x = sym("x")

# Simplify a rational expression
f = x^2 / x
println(simplify(f))

# Simplify trigonometric identity
g = sin(x)^2 + cos(x)^2
println(simplify(g))

# Simplify power expressions
h = sqrt(x^2)
println(simplify(h))

Manipulation Functions Reference

FunctionDescriptionExample
expand(f)Expand products and powersexpand((x+1)^2)
simplify(f)Simplify using algebraic rulessimplify(x^2/x)
diff(f, x, n)Differentiate n timesdiff(x^3, x)
taylor(f, x, n)Taylor/Maclaurin series (n terms)taylor(exp(x), x, 4)

Substitution

The subs function replaces variables or subexpressions with new values. This is useful for:

  • Evaluating expressions at specific points
  • Transforming expressions by replacing terms
  • Converting between variables
Code [5]Run
x = sym("x")
y = sym("y")

# Create an expression
f = x^2 + 2*x + 1

# Substitute a numeric value
println(subs(f, x, 3))

# Substitute with another expression
println(subs(f, x, y + 1))

# Substitute in a multi-variable expression
g = x + y
println(subs(g, x, y^2))

Solving Equations

The solve function finds values of a variable that make an expression equal to zero. For polynomial equations, it returns all roots (real and complex).

Note: The solve function treats expressions as equations equal to zero. So solve(x^2 - 4, x) solves x² - 4 = 0.

Code [6]Run
x = sym("x")

# Solve a quadratic equation x^2 - 4 = 0
println(solve(x^2 - 4, x))

# Solve x^2 + x - 6 = 0
println(solve(x^2 + x - 6, x))

# Solve a linear equation 2x + 5 = 0
println(solve(2*x + 5, x))

Symbolic Differentiation

The diff function computes symbolic derivatives. It handles all elementary functions including trigonometric, exponential, and logarithmic functions.

Syntax:

  • diff(f, x) - first derivative with respect to x
  • diff(f, x, n) - nth derivative with respect to x
Code [7]Run
x = sym("x")

# First derivative of x^3
println(diff(x^3, x))

# Derivative of trigonometric function
println(diff(sin(x), x))

# Derivative of exponential
println(diff(exp(x), x))

# Second derivative
println(diff(x^4, x, 2))

Taylor Series

The taylor function computes Maclaurin series expansions (Taylor series around x=0), which approximate functions as polynomials.

Syntax: taylor(f, x, n)

  • f - expression to expand
  • x - expansion variable
  • n - number of terms (default: 6)
Code [8]Run
x = sym("x")

# Taylor series of e^x, 4 terms
println(taylor(exp(x), x, 4))

# Taylor series of sin(x), 5 terms
println(taylor(sin(x), x, 5))

# Taylor series of cos(x), 5 terms
println(taylor(cos(x), x, 5))

Exact Symbolic Numbers

Use sym to create exact symbolic representations of numbers. This avoids floating-point approximation errors and preserves exact fractions.

Code [9]Run
# Create symbolic fractions (exact representation)
half = sym(1) / sym(2)
third = sym(1) / sym(3)

# Arithmetic with symbolic numbers preserves exactness
println(half + third)

# Compare with numeric (floating-point approximation)
println(0.5 + 0.333333333)

Extended Examples

Polynomial Manipulation

Work with polynomials: expand, find roots, and differentiate.

Code [10]Run
x = sym("x")

# Expand a polynomial
p = (x - 1)*(x - 2)*(x + 3)
println(expand(p))

# Find the roots
println(solve(p, x))

# Derivative of polynomial
println(diff(p, x))

Complete Calculus Example

Find the critical points of a cubic function by differentiating and solving.

Code [11]Run
x = sym("x")

# Define a function
f = x^3 - 3*x^2 + 2*x

# Find the derivative
df = diff(f, x)
println(df)

# Find critical points (where derivative = 0)
critical = solve(df, x)
println(critical)

# Second derivative for concavity
d2f = diff(f, x, 2)
println(d2f)

Summary

This tutorial covered the essentials of symbolic mathematics in Equana:

  • Creating symbols with sym("name")
  • Building expressions with operators and functions
  • Expanding products and powers with expand
  • Simplifying expressions with simplify
  • Substitution with subs for evaluation and transformation
  • Solving equations with solve
  • Differentiation with diff
  • Taylor series with taylor
  • Exact numbers with symbolic representation

Next Steps

  • Explore the Symbolic Package Reference for the complete API
  • Try the Working with Matrices tutorial for numeric computations
  • Check the Plotting tutorial to visualize symbolic functions

Workbench

Clear
No variables in workbench

Next Steps