3.5. SymPy#
3.5.1. Motivation#
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
eq = (2*x+1)*3*x
solve(eq, x)
Output:
[-1/2, 0]
…or simply work with math symbols instead of boring Python code?
That is when SymPy comes in handy.
3.5.2. What is SymPy?#
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 *
3.5.3. Basic Operations#
Normally, when computing a square root, we get a decimal:
18 ** (1 / 2)
4.242640687119285
But with SymPy, we can get a simplified version of the square root instead:
sqrt(18)
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.
25 / 15
1.6666666666666667
frac = Rational(25, 15)
frac
3.5.4. Symbols#
The real power of SymPy is its ability to work with symbols. To create symbols, use the method symbols()
:
x, y = symbols("x y")
expr = 3 * x + y
expr
Cool! We can create an expression in terms of x and y. What happens if we add a number to this expression?
expr + 2
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.
3.5.5. Equations#
3.5.5.1. Expand, Factor, and Simplify#
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
:
expr = x * (3 * x + y)
expansion = expand(expr)
expansion
Cool! We can also factor our expression by using factor
:
factor(expansion)
Another cool thing we can do with SymPy is to simplify an equation using simplify
:
expr = (6 * x ** 2 + 3 * x) / (3 * x)
expr
Pretty nice, isn’t it?
3.5.5.2. Solve an Equation#
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
:
eq = (2 * x + 1) * 3 * x
eq
solve(eq, x)
[-1/2, 0]
3.5.5.3. Substitution#
If we substitute the equation below by 2, what do we get?
\(x(6x+3)\)
We can figure that out using eq.subs(x, 2)
:
eq.subs(x, 2)
We can also substitute x with another variable to get an expression like below:
eq.subs(x ** 2, 2)
3.5.6. Trigonometric#
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()
trigsimp(1 / sec(x))
trigsimp(sin(x) / cos(x))
trigsimp(sin(x) ** 2 + cos(x) ** 2)
trigsimp(1 + cot(x) ** 2)
3.5.7. Derivatives, Integrals, and Limit#
With SymPy, you can also do calculus! What is the derivative of the expression below?
expr = sin(x) ** 2 + 2 * x
expr
If you cannot figure it out, don’t worry. We can use SymPy to figure it out.
res = diff(expr)
res
Now let’s try to get back to the original expression by taking the integral of the derivate.
integrate(res)
We can also take the limit as x approaches infinity.
limit(
1 / (x ** 2),
x,
oo,
)
or 2:
limit(
1 / (x ** 2),
x,
2,
)
3.5.8. Special Functions#
SymPy also provides special functions to:
Find the factorial of a number
factorial(x)
factorial(3)
Rewrite the expression in terms of another
tan(x).rewrite(cos)
3.5.9. Print Latex#
If you like the output and want to get the LaTex form of an expression, use latex
:
print(latex(integrate(res)))
2 x + \sin^{2}{\left(x \right)}
You could paste this LaTex form to your notebook’s markdown and get a nice mathematical expression like below!
\(2 x + \sin^{2}{\left(x \right)}\)