The Wayback Machine - https://web.archive.org/web/20240724024932/https://www.geeksforgeeks.org/python-getting-started-with-sympy-module/
Open In App

Python | Getting started with SymPy module

Last Updated : 21 Apr, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy is written entirely in Python. 
SymPy only depends on mpmath, a pure Python library for arbitrary floating point arithmetic, making it easy to use. 
Installing sympy module: 
 

 pip install sympy 

 

SymPy as a calculator:

SymPy defines following numerical types: Rational and Integer. The Rational class represents a rational number as a pair of two Integers, numerator and denominator, so Rational(1, 2) represents 1/2, Rational(5, 2) 5/2 and so on. The Integer class represents Integer number.
Example #1 : 
 

Python3




# import everything from sympy module
from sympy import *
 
a = Rational(5, 8)
print("value of a is :" + str(a))
 
b = Integer(3.579)
print("value of b is :" + str(b))


Output: 
 

 
value of a is :5/8
value of b is :3

SymPy uses mpmath in the background, which makes it possible to perform computations using arbitrary-precision arithmetic. That way, some special constants, like exp, pi, oo (Infinity), are treated as symbols and can be evaluated with arbitrary precision.
Example #2 : 
 

Python3




# import everything from sympy module
from sympy import *
 
# you can't get any numerical value
p = pi**3
print("value of p is :" + str(p))
 
# evalf method evaluates the expression to a floating-point number
q = pi.evalf()
print("value of q is :" + str(q))
 
# equivalent to e ^ 1 or e ** 1
r = exp(1).evalf()
print("value of r is :" + str(r))
 
s = (pi + exp(1)).evalf()
print("value of s is :" + str(s))
 
rslt = oo + 10000
print("value of rslt is :" + str(rslt))
 
if oo > 9999999 :
    print("True")
else:
    print("False")


Output: 
 

value of p is :pi^3
value of q is :3.14159265358979
value of r is :2.71828182845905
value of s is :5.85987448204884
value of rslt is :oo
True

 
In contrast to other Computer Algebra Systems, in SymPy you have to declare symbolic variables explicitly using Symbol() method.
Example #3 : 
 

Python3




# import everything from sympy module
from sympy import * x = Symbol('x')
y = Symbol('y')
 
z = (x + y) + (x-y)
print("value of z is :" + str(z))


Output: 
 

value of z is :2*x 

  
 

Calculus:

The real power of a symbolic computation system such as SymPy is the ability to do all sorts of computations symbolically. SymPy can simplify expressions, compute derivatives, integrals, and limits, solve equations, work with matrices, and much, much more, and do it all symbolically. Here is a small sampling of the sort of symbolic power SymPy is capable of, to whet your appetite.
Example #4 : Find derivative, integration, limits, quadratic equation.
 

Python3




# import everything from sympy module
from sympy import *
 
# make a symbol
x = Symbol('x')
 
# make the derivative of sin(x)*e ^ x
ans1 = diff(sin(x)*exp(x), x)
print("derivative of sin(x)*e ^ x : ", ans1)
 
# Compute (e ^ x * sin(x)+ e ^ x * cos(x))dx
ans2 = integrate(exp(x)*sin(x) + exp(x)*cos(x), x)
print("indefinite integration is : ", ans2)
 
# Compute definite integral of sin(x ^ 2)dx
# in b / w interval of ? and ?? .
ans3 = integrate(sin(x**2), (x, -oo, oo))
print("definite integration is : ", ans3)
 
# Find the limit of sin(x) / x given x tends to 0
ans4 = limit(sin(x)/x, x, 0)
print("limit is : ", ans4)
 
# Solve quadratic equation like, example : x ^ 2?2 = 0
ans5 = solve(x**2 - 2, x)
print("roots are : ", ans5)


Output : 
 

derivative of sin(x)*e^x :  exp(x)*sin(x) + exp(x)*cos(x)
indefinite integration is :  exp(x)*sin(x)
definite integration is :  sqrt(2)*sqrt(pi)/2
limit is :  1
roots are :  [-sqrt(2), sqrt(2)]

 



Similar Reads

Getting Started with OpenCV CUDA Module
OpenCV is an well known Open Source Computer Vision library, which is widely recognized for computer vision and image processing projects. The OpenCV CUDA (Compute Unified Device Architecture ) module introduced by NVIDIA in 2006, is a parallel computing platform with an application programming interface (API) that allows computers to use a variety
8 min read
Python | Getting started with psycopg2-PostGreSQL
PostgreSQL is a powerful, open source object-relational database system. PostgreSQL runs on all major operating systems. PostgreSQL follows ACID property of DataBase system and has the support of triggers, updatable views and materialized views, foreign keys. To connect PostgreSQL we use psycopg2 . It is the best and most friendly Database adapter
1 min read
Getting started with Python for Automated Trading
Automated Trading is the terminology given to trade entries and exits that are processed and executed via a computer. Automated trading has certain advantages: Minimizes human intervention: Automated trading systems eliminate emotions during trading. Traders usually have an easier time sticking to the strategy by holding emotions in check. Backtest
3 min read
Getting Started with Plotly-Python
The Plotly Python library is an interactive open-source library. This can be a very helpful tool for data visualization and understanding the data simply and easily. plotly graph objects are a high-level interface to plotly which are easy to use. It can plot various types of graphs and charts like scatter plots, line charts, bar charts, box plots,
4 min read
Getting Started with Python OpenCV
Computer Vision is one of the techniques from which we can understand images and videos and can extract information from them. It is a subset of artificial intelligence that collects information from digital images or videos. Python OpenCV is the most popular computer vision library. By using it, one can process images and videos to identify object
15+ min read
Getting Started on Heroku with Python
Heroku is a cloud platform as a service supporting several programming languages where a user can deploy, manage and scale their applications. It is widely used to deploy server-based web applications, APIs, discord bots, and more. So today in this tutorial, we'll be deploying a simple flask app to Heroku from start, and learn how it works. Ways to
3 min read
Getting Started with Competitive Programming in Python
Python is a great option for programming in Competitive Programming. First off, its easy-to-understand and concise grammar enables quicker development and simpler debugging. The huge standard library of Python offers a wide range of modules and functions that can be used to effectively address programming difficulties. Python also offers dynamic ty
11 min read
Getting Started With ImageIO Library in Python
Imageio is a Python library that provides an easy interface to read and write a wide range of image and video data, including animated images, volumetric data, and scientific formats. It is cross-platform. Installation: This module does not come built-in with Python. To install it type the below command in the terminal. pip install imageioRequireme
3 min read
Getting Started With Nose Testing in Python
Testing is essential for software development, ensuring that programs work reliably. Python offers various testing frameworks, but Nose stands out for being simple, flexible, and easy to extend. This article will explore Nose testing, covering its fundamentals, fixtures, and advanced features. Table of Content What is Nose?Why Choose Nose in Python
4 min read
Getting Started With Unit Testing in Python
In Python, unit tests are the segments of codes that are written to test other modules and files that we refer to as a unit. Python Unit Testing is a very important part of the software development process that helps us to ensure that the code works properly without any errors. In this article, we will learn about Python Testing with the help of ex
8 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg