Arguably the Most Handy Python Library- Sympy

Sympy- The Library hiding under the hood

For the longest time, I had no clue this library was out there, and when I finally found it,It just blew my mind, I couldn’t stop thinking, “Why didn’t I know about this sooner?” But its just no use crying over spilled milk. Now let’s get straight to the good stuff and explore the magic of Sympy.

Sympy stands for Symbolic Python. It was developed 17 years ago. Be it ODE’s or algebraic equations or solving the diophantine equations algebraically, look no further-Sympy has got you covered.

What’s so special about this?

As most of you might be aware of the python library Numpy, which enables you to perform numerical calculations on your data. But Sympy is a bit more abstract in that sense, it enables you to perform symbolic computation(Using symbols)

Simply put, we can perform all those nightmare inducing, pain staking and laborious mathematical operations like solving ODEs,substitution, differentiation, integration and even writing the equation in LaTex using sympy.

Let me show you the beauty of it. It goes without saying that to use the library you need to first pip it :-}

from sympy import *
f,x=symbols('f,x')

#Lets get creative about the function 
f=exp(x**3)**0.5

#Interested in the functional value of the above function at x=2? Fret not dear
f.subs([(x,2)])
# Output 54.5981500331442

# Lets differentiate this Monster 
f.diff(x)
# 1.5*x**2*exp(x**3)**0.5

That’s just a short premise into how useful this library can be.

Here is another snippet which performs the following daunting mathematical operation easily

\[\int_0^{3\pi}x^{12}*sin(33x)\hspace{1mm}dx\]
f=symbols('f',cls=Function)
x=symbols('x')
#defining the function
f=x**12*sin(33*x)

f.integrate(x)
#Output: -x**12*cos(33*x)/33 + 4*x**11*sin(33*x)/363 + 4*x**10*cos(33*x)/1089 - 40*x**9*sin(33*x)/35937 - 40*x**8*cos(33*x)/131769 + 320*x**7*sin(33*x)/4348377 + 2240*x**6*cos(33*x)/143496441 - 4480*x**5*sin(33*x)/1578460851 - 22400*x**4*cos(33*x)/52089208083 + 89600*x**3*sin(33*x)/1718943866739 + 89600*x**2*cos(33*x)/18908382534129 - 179200*x*sin(33*x)/623976623626257 - 179200*cos(33*x)/20591228579666481 

integral= float(f.integrate(x).subs([(x,3*pi)])-f.integrate(x).subs([(x,0)]))
print(integral)
# Output: 14864390294.783045

Another cool method which i found to be very useful was print_latex, I’ll use that method trying to solve the following ODE.

\[12*y^{\prime \prime }(x)+3y^{\prime}(x)-6*y(x)=0\]
y=symbols('y',cls=Function)
result = dsolve(12*Derivative(y(x), x, x) + 3*Derivative(y(x),x)-6*y(x), y(x))
# Output:
# Eq(y(x), C1*exp(x*(-1 + sqrt(33))/8) + C2*exp(-x*(1 + sqrt(33))/8))
checkodesol(12*Derivative(y(x), x, x) + 3*Derivative(y(x),x)-6*y(x), result)
# Output: (True, 0)
print_latex(result)
# Output: y{\left(x \right)} = C_{1} e^{\frac{x \left(-1 + \sqrt{33}\right)}{8}} + C_{2} e^{- \frac{x \left(1 + \sqrt{33}\right)}{8}}

Wondering what the solution, Lo and Behold here it is, \(y{\left(x \right)} = C_{1} e^{\frac{x \left(-1 + \sqrt{33}\right)}{8}} + C_{2} e^{- \frac{x \left(1 + \sqrt{33}\right)}{8}}\)

Well here i have only mentioned a few instances where sympy can come in handy. Gloss over the Documentation, you will find many more interesting applications.

Happy Learning!




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • 7 Pandas Methods everybody should know
  • Gradient Descent from Scratch
  • Stack and Queue Implementation
  • What If?