Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | sympy.series() method
Next article icon

Python | Getting started with SymPy module

Last Updated : 21 Apr, 2022
Comments
Improve
Suggest changes
Like Article
Like
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)]

 



Next Article
Python | sympy.series() method
author
ankthon
Improve
Article Tags :
  • Python
  • python-modules
Practice Tags :
  • python

Similar Reads

  • Getting Started with Python Programming
    Python is a versatile, interpreted programming language celebrated for its simplicity and readability. This guide will walk us through installing Python, running first program and exploring interactive coding—all essential steps for beginners. Install PythonBefore starting this Python course first,
    3 min read
  • Getting Started with Pytest
    Python Pytest is a framework based on Python. It is mainly used to write API test cases. It helps you write better programs. In the present days of REST services, Pytest is mainly used for API testing even though we can use Pytest to write simple to complex test cases, i.e., we can write codes to te
    5 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 w
    8 min read
  • Python | sympy.sqrt() method
    With the help of sympy.sqrt() method, we can find the square root of any number by using sympy.sqrt() method. Syntax : sympy.sqrt(number) Return : Return square root of any number. Example #1 : In this example we can see that by using sympy.sqrt() method, we can get the square root of any number. #
    1 min read
  • Python | sympy.series() method
    With the help of sympy.series() method, we can find the series of some mathematical functions and trigonometric expressions by using sympy.series() method. Syntax : sympy.series() Return : Return a series of functions. Example #1 : In this example we can see that by using sympy.series() method, we a
    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 t
    3 min read
  • Python | sympy.sec() method
    With the help of sympy.sec() method, we are able to find the value of sec theta using sympy.sec() function. Syntax : sympy.sec() Return : Return value of sec theta. Example #1 : In this example we can see that by using sympy.sec() method, we can find the value of sec theta. # import sympy from sympy
    1 min read
  • Python for Game Development: Getting Started with Pygame
    For a variety of uses, including web development, data research, automation, and, more and more, game creation, Python has grown to be an immensely popular language. Python allows both novice and seasoned developers to implement all the processes by initiating a very easy and robust approach to crea
    5 min read
  • Python | Sympy equation() method
    In Simpy, the function equation() is used to make equation of a given circle. Syntax : equation(x='x', y='y') Parameters: x : str or Symbol, optional y : str or Symbol, optional Returns : SymPy expression Example #1: # import sympy, Point and Circle from sympy import Point, Circle # using Circle() c
    1 min read
  • Python | sympy.S() method
    With the help of sympy.S() method, we can make a single instance of an object by using sympy.S() method, where S denotes to singleton class. Syntax : sympy.S() Return : Return the single instance of an object. Example #1 : In this example we can see that by using sympy.S() method, we are able to cre
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences