SymPy (http://www.sympy.org) is a Python library for symbolic math.

In symbolic math, symbols are used to represent mathematical expressions. An example of a symbolic math expression is below:

$$ x^{2} + y^{2} = z $$

In the expression above, we have the variables $x$, $y$ and $z$.

If we define a second symbolic math expression as:

$$ x = a + b $$

We can substitute in $a + b$ for $x$.

The resulting expression is:

$$ (a + b)^{2} + y^{2} = z $$

$$ a^{2} + 2ab + b^{2} + y^{2} = z $$

Solving for $y$ in terms of $a$,$b$ and $z$, results in:

$$ y = \sqrt{z - a^{2} - 2ab - b^{2}} $$

If we have numerical values for $z$, $a$ and $b$, we can use Python to calculate the value of $y$.

However, if we don't have numerical values for $z$, $a$ and $b$, Python can also be used to rearrange terms of the expression and solve for the variable $y$ in terms of the other variables $z$, $a$ and $b$. Working with mathematical symbols in a programmatic way, instead of working with numerical values in a programmatic way, is called symbolic math.

SymPy is a Python library for working with symbolic math. Before SymPy can be used, it needs to be installed. The installation of Sympy is accomplished using the Anaconda Prompt (or a terminal and pip) with the command:

> conda install sympy

SymPy is included in the Anaconda distribution of Python. If you have the full Anaconda distribution, you will be notified that the SymPy library is already installed.

Defining Variables in SymPy

To define symbolic math variables with SymPy, first import the symbols() function from the SymPy module:

In [1]:
from sympy import symbols

Symbolic math variables are declared using SymPy's symbols() function. Note, the arguments passed to the symbols() function (symbol names) are separated by a space, no comma, and surrounded by quotes. The output of the symbols() function are SymPy symbols objects. These output objects are separated by commas with no quotation marks.

In [2]:
x, y = symbols('x y')

Now that the symbols x and y are instantiated, a symbolic math expression using x and y can be created.

A symbolic math expression is a combination of symbolic math variables with numbers and mathematical operators, such as +,-,/ and *. The standard Python rules for working with numbers apply in SymPy symbolic math expressions.

In [3]:
expr = 2*x + y

Use the .subs() method to insert a numerical value into a symbolic math expression. The first argument of the .subs() method is the symbols object (the variable) and the second argument is the numerical value. In the expression above:

$$ 2x + y $$

If we substitute:

$$ x = 2 $$

The resulting expression is:

$$ 2(2) + y $$ $$ 4 +y $$

In [4]:
expr.subs(x, 2)
Out[4]:
y + 4

The .subs() method does not replace variables in place, .subs() only completes a one-time substitution. If we call expr after the .subs() method is applied, the original expr expression is returned.

In [5]:
expr
Out[5]:
2*x + y

In order to make the substitution permanent, a new expression object needs to be assigned to the output of the .subs() method.

In [6]:
expr = 2*x + y
expr2 = expr.subs(x, 2)
expr2
Out[6]:
y + 4

SymPy variables can also be substituted into SymPy expressions

In [7]:
x, y, z = symbols('x y z')
expr = 2*x + y
expr2 = expr.subs(x, z)
expr2
Out[7]:
y + 2*z

More complex substitutions can also be completed. Consider the following:

$$ 2x + y $$

Substitute in:

$$ y = 2x^2 + z^{-3} $$

Results in:

$$ 2x + 2x^2 + z^{-3} $$

In [8]:
x, y, z = symbols('x y z')
expr = 2*x + y
expr2 = expr.subs(y, 2*x**2 + z**(-3))
expr2
Out[8]:
2*x**2 + 2*x + z**(-3)

A more practical example could involve a large expression and several variable substitutions.

$$ n_0e^{-Q_v/RT} $$

$$ n_0 = 3.48 \times 10^{-6} $$

$$ Q_v = 12,700 $$

$$ R = 8.31 $$

$$ T = 1000 + 273 $$

In [9]:
from sympy import symbols, exp
n0, Qv, R, T = symbols('n0 Qv R T')
expr = n0*exp(-Qv/(R*T))

Multiple SymPy subs() methods can be chained together to substitue multiple variables in one line of code.

In [10]:
expr.subs(n0, 3.48e-6).subs(Qv,12700).subs(R, 8031).subs(T, 1000+273)
Out[10]:
3.48e-6*exp(-12700/10223463)

To evaluate an expression as a floating point number (get a numerical answer out), use Sympy's .evalf() method.

In [11]:
expr2 = expr.subs(n0, 3.48e-6).subs(Qv,12700).subs(R, 8031).subs(T, 1000+273)
expr2.evalf()
Out[11]:
3.47567968697765e-6

Defining Equations in Sympy

We can define equations in SymPy using symbolic math variables. Equations in SymPy are different than expressions in SymPy. An expression does not have equality. An equation has equality. An equation is equal to something.

In [1]:
from sympy import symbols, Eq, solve
In [2]:
x, y = symbols('x y')

SymPy equations are instantiated as an object of the Eq class. After SymPy symbols are created, the symbols can be passed into an equation object. Let's create the equation:

$$ 2x + y - 1 = 0 $$

In [3]:
eq1 = Eq(2*x - y - 1)

Now let's create a second equation:

$$ x + y - 5 = 0 $$

In [4]:
eq2 = Eq(x + y - 5)

To solve the two equations for the two variables x and y, we'll use SymPy's solve() function. The solve() function takes two arguments, a tuple of the equations (eq1, eq2) and a tuple of the variables to solve for (x, y).

In [5]:
sol = solve((eq1, eq2),(x, y))
sol
Out[5]:
{x: 2, y: 3}

The SymPy solution object is a Python dictionary. The keys are the SymPy variable objects and the values are the numerical values these variables correspond to.

In [6]:
print(f'The solution is x = {sol[x]}, y = {sol[y]}')
The solution is x = 2, y = 3

Summary

In this post, we looked at a Python package for symbolic math called SymPy. Using symbolic math, we can define expressions and equations exactly in terms of symbolic variables. We reviewed how to create a SymPy expression and substitue values and variables into the expression. Then we created to SymPy equation objects and solved two equations for two unknowns using SymPy's solve() function.