Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Evaluate a polynomial when coefficients are multi-dimensional in Python
To evaluate a polynomial at points x with multi-dimensional coefficients, use the numpy.polynomial.polynomial.polyval() method in Python. This method handles coefficient arrays where multiple polynomials can be stored in different columns.
Parameters
The polyval() method accepts three key parameters ?
- x ? The points at which to evaluate the polynomial. Can be a scalar, list, or array
-
c ? Array of coefficients where
c[n]contains coefficients for degree n terms. For multidimensional arrays, columns represent different polynomials -
tensor ? If
True(default), evaluates every column of coefficients for every element of x. IfFalse, broadcasts x over columns
Creating Multi-dimensional Coefficients
First, let's create a multi-dimensional coefficient array and examine its properties ?
import numpy as np
from numpy.polynomial.polynomial import polyval
# Create a multidimensional array of coefficients
c = np.arange(4).reshape(2,2)
# Display the array
print("Our Array...\n", c)
# Check the Dimensions
print("\nDimensions of our Array...\n", c.ndim)
# Get the Datatype
print("\nDatatype of our Array object...\n", c.dtype)
# Get the Shape
print("\nShape of our Array object...\n", c.shape)
Our Array... [[0 1] [2 3]] Dimensions of our Array... 2 Datatype of our Array object... int64 Shape of our Array object... (2, 2)
Evaluating the Polynomial
Now we'll evaluate the polynomial at points [1, 2] using the multi-dimensional coefficients ?
import numpy as np
from numpy.polynomial.polynomial import polyval
# Create coefficient array: [[0,1], [2,3]]
# This represents two polynomials:
# Column 0: 0 + 2x (coefficients [0,2])
# Column 1: 1 + 3x (coefficients [1,3])
c = np.arange(4).reshape(2,2)
# Evaluate at points x=[1,2] with tensor=True
result = polyval([1,2], c, tensor=True)
print("Result with tensor=True:\n", result)
# Each column of c is evaluated for each point in x
print("\nExplanation:")
print("For x=1: Column 0: 0+2(1)=2, Column 1: 1+3(1)=4")
print("For x=2: Column 0: 0+2(2)=4, Column 1: 1+3(2)=7")
Result with tensor=True: [[2. 4.] [4. 7.]] Explanation: For x=1: Column 0: 0+2(1)=2, Column 1: 1+3(1)=4 For x=2: Column 0: 0+2(2)=4, Column 1: 1+3(2)=7
Understanding the Output
The result matrix has shape (2, 2) where ?
- Rows correspond to evaluation points [1, 2]
- Columns correspond to the different polynomials
- Each element
result[i,j]is polynomial j evaluated at pointx[i]
Comparison: tensor=True vs tensor=False
import numpy as np
from numpy.polynomial.polynomial import polyval
c = np.arange(4).reshape(2,2)
# With tensor=True (default)
result_tensor = polyval([1,2], c, tensor=True)
print("tensor=True shape:", result_tensor.shape)
print("tensor=True result:\n", result_tensor)
# With tensor=False
result_broadcast = polyval([1,2], c, tensor=False)
print("\ntensor=False shape:", result_broadcast.shape)
print("tensor=False result:", result_broadcast)
tensor=True shape: (2, 2) tensor=True result: [[2. 4.] [4. 7.]] tensor=False shape: (2,) tensor=False result: [2. 7.]
Conclusion
Use polyval() with multi-dimensional coefficients to evaluate multiple polynomials simultaneously. The tensor parameter controls whether to evaluate all polynomials at all points (True) or broadcast for element-wise evaluation (False).
