--- title: Sympy file_title: Sympy ---

SymPy is a symbolic mathematics library built on the Python programming language.

In this post we'll look at how to use some of the basic functions of the library.

In :
import sympy as sy


The equation that we want to solve is in the form of \begin{equation} a x^2 + bx + c = 0 \end{equation}

### Solving Algebraic Roots¶

Let's solve for the roots of the quadratic equation. First we have to define the relevant variables as sympy symbols. We can accomplish this in one line by using the symbols function, providing a list of variables, and setting them to the associated variables that we want to use. The order does matter [^1].

In :
a, b, c, x = sy.symbols('a, b, c, x')


Next, let's define the quadratic equation as a sympy equation using the Eq function. Note that the equation is assumed to be equal to zero.

In :
eq = sy.Eq(a*x**2 + b*x + c)


Let's make sure that our equation looks right by printing it:

In :
print(eq)

Eq(a*x**2 + b*x + c, 0)


This is pretty ugly. Luckily, we can print a much better version using tools built into sympy.

In :
sy.init_printing(use_latex='mathjax')
eq

Out:
$\displaystyle a x^{2} + b x + c = 0$

We can even print out the $\LaTeX{}$ used to display the equation we can be copied and pated into $\LaTeX{}$ documents.

In :
sy.print_latex(eq)

a x^{2} + b x + c = 0


Now, let's go ahead and solve the equation for $x$. All we need to do is call the sympy.solve function with the equation and the variable we want to find.

In :
solutions = sy.solve(eq, x)
solutions

Out:
$\displaystyle \left[ \frac{- b + \sqrt{- 4 a c + b^{2}}}{2 a}, \ - \frac{b + \sqrt{- 4 a c + b^{2}}}{2 a}\right]$

The function returned a list of solutions. The quadratic equation has two roots (which should come as no suprise). Using typical python syntax we can extract the first solution:

In :
solutions

Out:
$\displaystyle \frac{- b + \sqrt{- 4 a c + b^{2}}}{2 a}$

We can even substitute in values for the constant coefficients using the subs method on the first solution:

In :
solutions.subs({a:1,b:2,c:-1})

Out:
$\displaystyle -1 + \sqrt{2}$

### Conclusion¶

SymPy is a great stand-alone symbolic computing library. It has many features, and its integrations with Jupyter notebooks makes it great for interactive computational mathematics. Because Python is a free, open source langauge it can be extended for uses such as symbolic computations and retain the rich programming features that are built into the core of the language.