Skip to content

# SymPy

## SymPy

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

The expression contains the symbols $x$, $y$, and $z$.

If we define a second symbolic math expression:

x = a + b

then we can substitue in $a + b$ for $x$.

The result is the expression:

(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}}

In the symbolic math substitution above, symbolic math variables were rearranged, grouped and inserted. None of the variables were equal to a specific number, like 5 or 0.001, but you can still solve for one variable in terms on the other variables. If we have numerical values for $z$, $a$ and $b$, we can use Python to calculate the value of $y$.

But if we don't have numerical values for $z$, $a$ and $b$, Python and the SymPy package can be used to rearrange terms and solve for one variable in terms of the other.

Working with mathematical symbols in a programmatic way instead of working with numerical values in a programmatic way is called symbolic math. To work with symbolic math in Python, the SymPy library needs to be installed. SymPy comes pre-installed with the Anaconda distribution of Python. If you are not using the Anaconda distribution of Python, SymPy can be installed with the Anaconda Prompt. Use the command:

> conda install sympy