
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Finding Roots of Polynomials
Finding Roots of Polynomials in NumPy
Finding the roots of a polynomial means determining the values of x where the polynomial equals zero. In NumPy, you can find the roots of a polynomial using the numpy.roots() function.
Roots are fundamental in various mathematical applications, such as solving equations, optimization problems, and analyzing the behavior of polynomials in signal processing or numerical analysis.
The numpy.roots() Function
The numpy.roots() function takes the coefficients of a polynomial as an input and returns the roots of the polynomial.
The polynomial is expressed in terms of its coefficients, starting with the highest power and ending with the constant term. The function finds all the roots of the polynomial, including real and complex roots.
Example
In this example, we find the roots of a polynomial defined by the equation using the roots() function in NumPy −
import numpy as np # Define the coefficients of the polynomial 1x - 6x + 11x - 6 coefficients = np.array([1, -6, 11, -6]) # Find the roots of the polynomial using numpy.roots roots = np.roots(coefficients) print("Roots of the polynomial:", roots)
The result of finding the roots for the given polynomial is as follows −
Roots of the polynomial: [3. 2. 1.]
Real and Complex Roots
NumPy handles both real and complex roots. If the polynomial has complex roots, numpy.roots() will return them as complex numbers. For example, the roots of a quadratic polynomial with no real roots will appear as complex numbers.
Example: Finding Complex Roots
In this example, we find the roots of a quadratic polynomial using the numpy.roots() function −
import numpy as np # Define the coefficients of the polynomial x + 1 coefficients_complex = np.array([1, 0, 1]) # Find the roots of the polynomial using numpy.roots roots_complex = np.roots(coefficients_complex) print("Roots of the complex polynomial:", roots_complex)
The result of finding the roots of the complex polynomial is as shown below −
Roots of the complex polynomial: [-0.+1.j 0.-1.j]
Verifying the Roots
After finding the roots, it is often useful to verify that they satisfy the original polynomial equation. This can be done by substituting the roots back into the polynomial and checking if the result is close to zero.
Example
In this example, we verify the roots of a polynomial by evaluating the polynomial at the roots −
import numpy as np # Define the polynomial coefficients coefficients = np.array([1, -6, 11, -6]) # Find the roots of the polynomial roots = np.roots(coefficients) # Verify the roots by evaluating the polynomial at the roots verification = np.polyval(coefficients, roots) print("Verification results:", verification)
The verification results (should be close to zero for all roots) is −
Verification results: [3.55271368e-15 2.66453526e-15 0.00000000e+00]
Handling Higher-Degree Polynomials
The NumPy roots() function is capable of finding the roots of polynomials of any degree. As the degree of the polynomial increases, the number of roots increases as well. The function will return all roots, real and complex, of the given polynomial.
Example
In this example, we find the roots of a polynomial of degree 5 using the roots() function −
import numpy as np # Define the coefficients of the polynomial 1x - 3x + 5x - 7x + 11x - 13 coefficients_high_degree = np.array([1, -3, 5, -7, 11, -13]) # Find the roots of the polynomial using numpy.roots roots_high_degree = np.roots(coefficients_high_degree) print("Roots of the higher-degree polynomial:", roots_high_degree)
The result of finding the roots for the higher-degree polynomial is as follows −
Roots of the higher-degree polynomial: [-0.56466348+1.42799478j -0.56466348-1.42799478j 1.18589358+1.31548183j 1.18589358-1.31548183j 1.7575398 +0.j ]