
- 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 - Evaluating Polynomials
Evaluating Polynomials in NumPy
Evaluating polynomials in NumPy means calculating the value of the polynomial at a specific point. You can do this using the numpy.polyval() function in NumPy.
The polynomial is defined by its coefficients, starting with the highest degree term and ending with the constant term. The function evaluates the polynomial at the given value of x, and can handle multiple values of x at once, returning the corresponding results.
The numpy.polyval() Function
The numpy.polyval() function evaluates a polynomial for a given value (or array of values) of x. It takes two arguments: the coefficients of the polynomial and the value(s) at which the polynomial should be evaluated.
The polynomial is expressed in terms of its coefficients, starting with the highest power term and ending with the constant term.
Example: Evaluating Polynomial at a Single Point
Let us consider the polynomial f(x) = 2x - 3x + 4. We can evaluate this polynomial at x = 2 −
import numpy as np # Define the coefficients of the polynomial 2x - 3x + 4 coefficients = np.array([2, -3, 4]) # Evaluate the polynomial at x = 2 using numpy.polyval x_value = 2 result = np.polyval(coefficients, x_value) print(f"Value of the polynomial at x = {x_value}: {result}")
The result of evaluating the polynomial at x = 2 is −
Value of the polynomial at x = 2: 6
Evaluating Polynomials at Multiple Points
In addition to evaluating the polynomial at a single point, the numpy.polyval() function can also evaluate the polynomial at multiple points at once. The function accepts an array of x values and returns an array of corresponding results.
Example
Let us evaluate the same polynomial f(x) = 2x - 3x + 4 at multiple values of x −
import numpy as np # Define the coefficients of the polynomial 2x - 3x + 4 coefficients = np.array([2, -3, 4]) # Define the x values for evaluation x_values = np.array([-1, 0, 1, 2]) # Evaluate the polynomial at multiple points using numpy.polyval results = np.polyval(coefficients, x_values) print("Values of the polynomial at x = [-1, 0, 1, 2]:", results)
The result of evaluating the polynomial at the given points is as shown below −
Values of the polynomial at x = [-1, 0, 1, 2]: [9 4 3 6]
Evaluating Polynomials with Complex Roots
If a polynomial has complex roots or coefficients, the numpy.polyval() function can still evaluate the polynomial correctly. The function will return complex numbers if the result involves complex arithmetic.
Example
Consider the polynomial f(x) = (2 + 3i)x + (1 - 2i)x + (4 + i). We can evaluate this polynomial at x = 1 + i as shown below −
import numpy as np # Define the coefficients of the polynomial with complex numbers coefficients_complex = np.array([2 + 3j, 1 - 2j, 4 + 1j]) # Define the x value for evaluation x_value_complex = 1 + 1j # Evaluate the polynomial at x = 1 + 1j using numpy.polyval result_complex = np.polyval(coefficients_complex, x_value_complex) print(f"Value of the complex polynomial at x = {x_value_complex}: {result_complex}")
The result of evaluating the polynomial with complex coefficients is −
Value of the complex polynomial at x = (1+1j): (1+4j)
Polynomial Evaluation for Curve Plotting
Evaluating polynomials at a range of x values is commonly used in curve plotting, such as when plotting the graph of a polynomial function. This can be done by passing an array of x values to the numpy.polyval() function and plotting the resulting values.
Example: Plotting a Polynomial Curve
Let us plot the polynomial f(x) = 2x - 3x + 4 over the range of x = -5 to x = 5 −
import numpy as np import matplotlib.pyplot as plt # Define the coefficients of the polynomial 2x - 3x + 4 coefficients = np.array([2, -3, 4]) # Define the range of x values x_values_plot = np.linspace(-5, 5, 100) # Evaluate the polynomial at the x values using numpy.polyval y_values_plot = np.polyval(coefficients, x_values_plot) # Plot the polynomial curve plt.plot(x_values_plot, y_values_plot, label='f(x) = 2x - 3x + 4') plt.xlabel('x') plt.ylabel('f(x)') plt.title('Polynomial Curve Plot') plt.legend() plt.grid(True) plt.show()
The output displayed is as shown below −
