Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
Find the roots of the polynomials using NumPy
Next article icon

Find the roots of the polynomials using NumPy

Last Updated : 05 Sep, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, let's discuss how to find the roots of a polynomial of a NumPy array. It can be found using various methods, let's see them in detail.

Method 1: Using np.roots()

This function returns the roots of a polynomial with coefficients given in p. The coefficients of the polynomial are to be put in an array in the respective order. 

For example, if the polynomial is x2 +3x + 1, then the array will be [1, 3, 1]

Syntax : numpy.roots(p)

Parameters :
p : [array_like] Rank-1 array of polynomial coefficients.

Return : [ndarray] An array containing the roots of the polynomial.

Let's see some examples:

Example 1: Find the roots of polynomial x2 +2x + 1

Python3
# import numpy library import numpy as np   # Enter the coefficients of the poly in the array coeff = [1, 2, 1] print(np.roots(coeff)) 

Output:

[-1. -1.]

Example 2: Find the roots of the polynomial x3 +3 x2   + 2x +1

Python3
# import numpy library import numpy as np   # Enter the coefficients of the poly  # in the array coeff = [1, 3, 2, 1] print(np.roots(coeff)) 

Output:

[-2.32471796+0.j         -0.33764102+0.56227951j -0.33764102-0.56227951j]

Method 2: Using np.poly1D()

This function helps to define a polynomial function. It makes it easy to apply “natural operations” on polynomials. The coefficients of the polynomial are to be put in an array in the respective order.

For example, for the polynomial x2 +3x + 1, the array will be [1, 3, 1]

Approach:

  • Apply function np.poly1D() on the array and store it in a variable.
  • Find the roots by multiplying the variable by roots or r(in-built keyword) and print the result to get the roots of the given polynomial

Syntax: numpy.poly1d(arr, root, var):

Let's see some examples:

Example 1: Find the roots of polynomial x2 +2x + 1

Python3
# import numpy library import numpy as np   # enter the coefficients of poly  # in the array p = np.poly1d([1, 2, 1])  # multiplying by r(or roots) to  # get the roots root_of_poly = p.r print(root_of_poly) 

Output:

[-1. -1.]

Example 2: Find the roots of the polynomial x3 +3 x2   + 2x +1

Python3
# import numpy library import numpy as np   # enter the coefficients of poly # in the array p = np.poly1d([1, 3, 2, 1])  # multiplying by r(or roots) to get  # the roots root_of_poly = p.r print(root_of_poly) 

Output:

[-2.32471796+0.j         -0.33764102+0.56227951j -0.33764102-0.56227951j]


Next Article
Find the roots of the polynomials using NumPy

M

mayanktyagi1709
Improve
Article Tags :
  • Python
  • Python-numpy
  • Python numpy-polynomials
Practice Tags :
  • python

Similar Reads

    How to add one polynomial to another using NumPy in Python?
    In this article, Let's see how to add one polynomial to another. Two polynomials are given as input and the result is the addition of two polynomials. The polynomial p(x) = C3 x2 + C2 x + C1  is represented in NumPy as : ( C1, C2, C3 ) { the coefficients (constants)}. Let take two polynomials p(x) a
    2 min read
    How to subtract one polynomial to another using NumPy in Python?
    In this article, let's discuss how to subtract one polynomial to another. Two polynomials are given as input and the result is the subtraction of two polynomials. The polynomial p(x) = C3 x2 + C2 x + C1  is represented in NumPy as : ( C1, C2, C3 ) { the coefficients (constants)}.Let take two polynom
    2 min read
    Compute the roots of a Chebyshev series using NumPy in Python
    This article will discuss how to compute the roots of a Chebyshev series in NumPy using Python. Chebyshev polynomials are important in approximation theory these are never formally generated. Only the coefficients are required for all calculations. If we want to compute the roots of a polynomial we
    2 min read
    Convert a Polynomial to Hermite Series Using NumPy
    In this article, we will discuss how to convert a Polynomial to Hermite series using NumPy. Example: Polynomial: [4 2 3 6] Coefficients of the converted equivalent Hermite series : [5.5  5.5  0.75 0.75] To convert a polynomial to Hermite series we need to use numpy methods hermite.poly2herm(). We ca
    2 min read
    How to divide a polynomial to another using NumPy in Python?
    In this article, we will make a NumPy program to divide one polynomial to another. Two polynomials are given as input and the result is the quotient and remainder of the division. The polynomial p(x) = C3 x2 + C2 x + C1  is represented in NumPy as : ( C1, C2, C3 ) { the coefficients (constants)}.Let
    2 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