
- 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 - Polynomial Representation
Polynomial Representation in NumPy
Polynomial representation in NumPy refers to how polynomials are expressed and manipulated using arrays. A polynomial is a mathematical expression involving a sum of powers of a variable, such as ax2 + bx + c, where a, b, and c are constants and x is the variable.
In NumPy, polynomials are represented as arrays, where each element corresponds to a coefficient of the polynomial.
For example, the polynomial 3x2 + 2x + 1 would be represented as the array [3, 2, 1], where the first element represents the coefficient of x2, the second represents the coefficient of x, and the third represents the constant term.
NumPy provides the numpy.polynomial module to create and work with polynomial objects, which allow you to perform operations like evaluation, differentiation, and integration on polynomials.
Creating Polynomials
Polynomials in NumPy can be created using the numpy.polynomial.Polynomial class, which represents polynomials in terms of their coefficients.
Example: Creating a Polynomial
In the following example, we create a polynomial 2x3 + 3x2 + x + 5 using NumPy −
import numpy as np from numpy.polynomial import Polynomial # Define the coefficients of the polynomial coefficients = [5, 1, 3, 2] # Create the polynomial p = Polynomial(coefficients) print("Polynomial:", p)
The result shows the created polynomial −
Polynomial: 5.0 + 1.0x + 3.0x + 2.0x
Evaluating Polynomials
Polynomials can be evaluated at specific points using the __call__ method of the Polynomial class or the numpy.polyval() function, which is another method for evaluating polynomials.
Example: Evaluating a Polynomial
In the following example, we evaluate the polynomial 2x3 + 3x2 + x + 5 at x = 2 using both the __call__ method and the polyval() function −
import numpy as np from numpy.polynomial import Polynomial # Define the polynomial # 2x^3 + 3x^2 + x + 5 p = Polynomial([5, 1, 3, 2]) # Evaluate the polynomial at x = 2 using the __call__ method value_call = p(2) # Evaluate the polynomial at x = 2 using numpy.polyval value_evaluate = np.polyval(p.coef, 2) print("Value of the polynomial at x = 2 using __call__:", value_call) print("Value of the polynomial at x = 2 using numpy.polyval:", value_evaluate)
The result shows the value of the polynomial at x = 2 using both methods −
Value of the polynomial at x = 2 using __call__: 35.0 Value of the polynomial at x = 2 using numpy.polyval: 52.0
Performing Operations on Polynomials
NumPy allows various operations on polynomials, such as addition, subtraction, multiplication, and division.
You can create polynomial objects by instantiating the Polynomial class and passing the coefficients of the polynomial. NumPy automatically handles the necessary calculations on the polynomial coefficients.
Example: Polynomial Addition
In the following example, we add two polynomials 2x2 + 3x + 1 and x2 + 4x + 2 using the + operator −
import numpy as np from numpy.polynomial import Polynomial # Define the polynomials # 2x^2 + 3x + 1 p1 = Polynomial([1, 3, 2]) # x^2 + 4x + 2 p2 = Polynomial([2, 4, 1]) # Add the polynomials result_add = p1 + p2 print("Result of polynomial addition:", result_add)
The result shows the sum of the two polynomials −
Result of polynomial addition: 3.0 + 7.0x + 3.0x
Example: Polynomial Multiplication
Here, we are multiplying two polynomials 2x + 1 and x + 2 using the * operator −
import numpy as np from numpy.polynomial import Polynomial # Define the polynomials # 2x + 1 p1 = Polynomial([1, 2]) # x + 2 p2 = Polynomial([2, 1]) # Multiply the polynomials result_multiply = p1 * p2 print("Result of polynomial multiplication:", result_multiply)
The result shows the product of the two polynomials −
Result of polynomial multiplication: 2.0 + 5.0x + 2.0x
Example: Polynomial Differentiation
In this example, we find the derivative of the polynomial 2x3 + 3x2 + x + 5 −
import numpy as np from numpy.polynomial import Polynomial # Define the polynomial # 2x^3 + 3x^2 + x + 5 p = Polynomial([5, 1, 3, 2]) # Differentiate the polynomial result_diff = p.deriv() print("Result of polynomial differentiation:", result_diff)
The result shows the first derivative of the original polynomial −
Result of polynomial differentiation: 1.0 + 6.0x + 6.0x
Fitting Polynomials to Data
Fitting polynomials to data involves finding the polynomial that best matches a given set of data points. In NumPy, this is done using the numpy.fit() function, which fits a polynomial of a specified degree to the data by minimizing the error.
The result is a polynomial that approximates the data, useful for tasks like curve fitting or trend analysis.
Example: Fitting a Polynomial to Data
In the following example, we fit a polynomial of degree 2 to a set of data points −
import numpy as np from numpy.polynomial import Polynomial # Define data points x = np.array([0, 1, 2, 3, 4]) y = np.array([1, 2, 0, 2, 1]) # Fit a polynomial of degree 2 to the data p_fit = Polynomial.fit(x, y, deg=2) print("Fitted polynomial:", p_fit)
The result shows the fitted polynomial −
Fitted polynomial: 1.2 - (7.02166694e-17)x + (1.22883961e-16)x