SymPy: Symbolic Computation in Python | by Khuyen Tran | Towards Data Science

Have you ever wished to solve a math equation in Python? Wouldn’t it be nice if we could solve an algebraic equation like below in one line of code
[-1/2, 0]
...or simply work with math symbols instead of boring Python code?
That is when SymPy comes in handy.
SymPy is a Python library that allows you to compute mathematical objects symbolically.
To install SymPy, type:
pip install sympy
Now let’s go over some of the amazing things that SymPy can do!
Start with importing all methods provided by SymPy
from sympy import *
Normally, when computing a square root, we get a decimal:
But with SymPy, we can get a simplified version of the square root instead:
It is because SymPy tries to represent mathematical objects exactly instead of approximately.
Thus, we will get a fraction instead of a decimal when dividing 2 numbers using SymPy.
The real power of SymPy is its ability to work with symbols. To create symbols, use the method symbols() :
Cool! We can create an expression in terms of x and y. What happens if we add a number to this expression?
Aha! + 2 is added to the expression and the expression remains unevaluated.
Why would working with symbols be useful? Because we can now use all kinds of math tricks we learn in school such as expanding, factoring and simplifying an equation to make our life easier.
We know that the expansion of the expression on the left is equal to the expression on the right.
Can this be done with SymPy? Yes! SymPy allows us to expand an equation using expand :
Cool! We can also factor our expression by using factor :
Another cool thing we can do with SymPy is to simplify an equation using simplify :
Pretty nice, isn’t it?
One of the most common questions we see when dealing with mathematical symbols is to solve an equation. Luckily, this can also be done with SymPy.
To solve an equation, use solve :
If we substitute the equation below by 2, what do we get?
We can figure that out using eq.subs(x, 2) :
We can also substitute x with another variable to get an expression like below:
Nice!
Remember all the fun trigonometric identities we learn in high school?
Instead of looking up these identities, wouldn’t it be nice if we can use SymPy to figure this out for us?
To simplify expressions using trigonometric identities, use trigsimp()
With SymPy, you can also do calculus! What is the derivative of the expression below?
If you cannot figure it out, don’t worry. We can use SymPy to figure it out.
Now let’s try to get back to the original expression by taking the integral of the derivate.
We can also take the limit as x approaches infinity.
or 2:
SymPy also provides special functions to:
Find the factorial of a number
Rewrite the expression in terms of another
If you like the output and want to get the LaTex form of an expression, use latex :
You could paste this LaTex form to your notebook’s markdown and get a nice mathematical expression like below!
Congratulation! You have just learned how to compute mathematical objects symbolically in Python using SymPy. The next time you solve math, try using SymPy to make your life easier.
There are so many other useful methods SymPy provides that I could not cover here. I encourage you to check out this documentation for more inspiration.
Feel free to fork and play with the code for this article in this Github repo:
github.com
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: