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 at points x and x is broadcast over the columns of r for the evaluation in Python
To evaluate a polynomial specified by its roots at points x, use the polynomial.polyvalfromroots() method in Python NumPy. This method allows you to evaluate polynomials defined by their roots rather than coefficients, with flexible broadcasting options for multidimensional arrays.
Parameters
The polyvalfromroots() method accepts three parameters ?
- x − The evaluation points. Can be a scalar, list, or array
- r − Array of roots. For multidimensional arrays, the first index represents root index, remaining indices enumerate multiple polynomials
- tensor − Boolean flag controlling broadcasting behavior. Default is True
Understanding the tensor Parameter
The tensor parameter controls how x is broadcast over multidimensional root arrays ?
- tensor=True − Extends root array shape with ones for each dimension of x
- tensor=False − Broadcasts x over columns of r for evaluation
Example with tensor=False
Here's how to evaluate polynomials with broadcasting over columns ?
from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np
# Create a 2D array of roots
roots = np.arange(-2, 2).reshape(2, 2)
print("Root array:")
print(roots)
# Evaluation points
x_points = [-2, 1]
print(f"\nEvaluation points: {x_points}")
# Evaluate with tensor=False (broadcast over columns)
result = polyvalfromroots(x_points, roots, tensor=False)
print(f"\nResult with tensor=False:")
print(result)
Root array: [[-2 -1] [ 0 1]] Evaluation points: [-2, 1] Result with tensor=False: [-0. 0.]
Comparison: tensor=True vs tensor=False
Let's compare both broadcasting modes ?
from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np
# Create roots array
roots = np.array([[-2, -1], [0, 1]])
x_points = [1, 2]
# With tensor=True (default)
result_true = polyvalfromroots(x_points, roots, tensor=True)
print("With tensor=True:")
print(f"Shape: {result_true.shape}")
print(result_true)
# With tensor=False
result_false = polyvalfromroots(x_points, roots, tensor=False)
print(f"\nWith tensor=False:")
print(f"Shape: {result_false.shape}")
print(result_false)
With tensor=True: Shape: (2, 2) [[ 6. 3.] [12. 8.]] With tensor=False: Shape: (2,) [6. 8.]
How It Works
The polynomial defined by roots r?, r?, ..., r? has the form ?
P(x) = (x - r?)(x - r?)...(x - r?)
When tensor=False, each column of the root array defines a separate polynomial, and x is evaluated against each column independently.
Comparison
| Parameter | Behavior | Output Shape | Use Case |
|---|---|---|---|
tensor=True |
Extends root shape for each x dimension | Higher dimensional | Full tensor evaluation |
tensor=False |
Broadcasts x over columns | Matches x dimensions | Column-wise polynomial evaluation |
Conclusion
Use polyvalfromroots() with tensor=False when you want to evaluate multiple polynomials (stored as columns) at the same x points. This approach provides efficient broadcasting and is ideal for batch polynomial evaluation.
