Contents

# A primer on symbolic math with Python

July 29th, 2018 Sympy is a library for doing symbolic math with Python. Python is a very flexible language, but it doesn't come built in with symbolic math functions. Beyond manipulating numbers, you may want to do operations, factorize, or even take the derivative or integral of a function.

This tutorial is to get your feet wet with the syntax and notation of Sympy.

If you want to take this tutorial farther or need a refrence, the docs for sympy can be found at http://docs.sympy.org/latest/tutorial/index.html#tutorial

There will be tutorials later where I will dive deeper into using sympy for calculus. I'll start with a quick demonstration of declaring variables in plain python vs sympy to see the difference:

### Declaring a variable

In vanilla python you would see something like so:

``````
x = 1
x + x + 1
``````

I don't think anybody would be surprised to see we get the following:

``````
3
``````

### Declaring a variable with Sympy

If we wanted to do the above equation using symbolic python we would use Sympy like this:
``````
from sympy import Symbol
x = Symbol('x')
x = x + x + 1
``````

Our result from above would be:

``````
2*x + 1
``````

Above is our equation expressed algebraically.

### Defining multiple symbols at once

You can pass in multiple symbols at once:

``````
from sympy import symbols
x, y, z = symbols('x,y,z')
h = 2*x*y + 2*x*y
``````

Would yield

``````
4*x*y
``````

### Factoring

``````
from sympy import factor
expression = x**3 - z**3
factor(expression)
``````

Would give us:

``````
(x - z)*(x**2 + x*z + z**2)
``````

### Expanding a factorized expression

``````
factor = (x + z)**2
expand(factor)
``````

Would return:

``````
x**2 + 2*x*z + z**2
``````

### Pretty printing

Sympy can attempt to use superscripts to return to the console the equation in a more human friendly view of the equation. Here will use the pprint method on the previous equation.

``````
factor = (x + z)**2
pprint(expand(factor))
``````

Which would give us the same equation back, but like you are use to seeing it in a text book.

``````
2            2
x  + 2⋅x⋅z + z
``````

### Solving Equations

Here is where we get to the meat and potatoes of sympy's functionality.

``````
from sympy import Symbol,solve
x = Symbol('x')
expression = x - 5 - 7
solve(expression)
``````

Would return:

``````
12
``````

``````
x = Symbol('x')
expression = x**2 +10*x + 5
solve(expression, dict=True)
``````

And our list of results are:

``````
{x: -5 - 2*sqrt(5)}, {x: -5 + 2*sqrt(5)}
``````

Let's put it all together:

``````
from sympy import *
x,a,b,c = symbols('x, a,b,c')
expression = a*x**2 + b*x+c
pprint(solve(expression,x,dict=True))
``````

Our result:

``````
⎡⎧           _____________⎫  ⎧    ⎛       _____________⎞ ⎫⎤
⎢⎪          ╱           2 ⎪  ⎪    ⎜      ╱           2 ⎟ ⎪⎥
⎢⎨   -b + ╲╱  -4⋅a⋅c + b  ⎬  ⎨   -⎝b + ╲╱  -4⋅a⋅c + b  ⎠ ⎬⎥
⎢⎪x: ─────────────────────⎪, ⎪x: ────────────────────────⎪⎥
⎣⎩            2⋅a         ⎭  ⎩             2⋅a           ⎭⎦
``````

This is just a sampling of what you can do with sympy. You can also solve linear equations and even do calculus with this open source library.

Back