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:
In the expression above, we have the variables $x$, $y$ and $z$.
If we define a second symbolic math expression as:
We can substitute in $a + b$ for $x$.
The resulting expression is:
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:
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.
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.
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:
If we substitute:
$$ x = 2 $$The resulting expression is:
$$ 2(2) + y $$$$ 4 +y $$expr.subs(x, 2)
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.
expr
In order to make the substitution permanent, a new expression object needs to be assigned to the output of the .subs()
method.
expr = 2*x + y
expr2 = expr.subs(x, 2)
expr2
SymPy variables can also be substituted into SymPy expressions
x, y, z = symbols('x y z')
expr = 2*x + y
expr2 = expr.subs(x, z)
expr2
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} $$x, y, z = symbols('x y z')
expr = 2*x + y
expr2 = expr.subs(y, 2*x**2 + z**(-3))
expr2
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 $$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.
expr.subs(n0, 3.48e-6).subs(Qv,12700).subs(R, 8031).subs(T, 1000+273)
To evaluate an expression as a floating point number (get a numerical answer out), use Sympy's .evalf()
method.
expr2 = expr.subs(n0, 3.48e-6).subs(Qv,12700).subs(R, 8031).subs(T, 1000+273)
expr2.evalf()
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.
from sympy import symbols, Eq, solve
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:
eq1 = Eq(2*x - y - 1)
Now let's create a second equation:
$$ x + y - 5 = 0 $$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)
.
sol = solve((eq1, eq2),(x, y))
sol
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.
print(f'The solution is x = {sol[x]}, y = {sol[y]}')
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.