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 specified by its roots at points x in Python
To evaluate a polynomial specified by its roots at points x, use the polynomial.polyvalfromroots() method in Python NumPy. This function calculates the polynomial value at given points when you know the polynomial's roots rather than its coefficients.
Syntax
numpy.polynomial.polynomial.polyvalfromroots(x, r, tensor=True)
Parameters
x: Array of points where to evaluate the polynomial. If x is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar.
r: Array of roots. If r is multidimensional, the first index is the root index, while the remaining indices enumerate multiple polynomials.
tensor: If True (default), the shape of the roots array is extended with ones on the right, one for each dimension of x. If False, x is broadcast over the columns of r for the evaluation.
Example 1: Basic Usage
Let's evaluate a polynomial with roots [1, 2, 3] at point x = 1 ?
from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np
# Create an array of roots
roots = np.array([1, 2, 3])
print("Roots:", roots)
# Evaluate polynomial at x = 1
result = polyvalfromroots(1, roots)
print("Polynomial value at x=1:", result)
Roots: [1 2 3] Polynomial value at x=1: 0.0
Example 2: Multiple Evaluation Points
Evaluate the same polynomial at multiple points ?
from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np
# Roots of the polynomial
roots = np.array([1, 2, 3])
# Multiple evaluation points
x_values = np.array([0, 1, 2, 3, 4])
# Evaluate polynomial at all points
results = polyvalfromroots(x_values, roots)
print("Roots:", roots)
print("X values:", x_values)
print("Polynomial values:", results)
Roots: [1 2 3] X values: [0 1 2 3 4] Polynomial values: [-6. 0. 0. 0. 6.]
How It Works
For a polynomial with roots r?, r?, ..., r?, the polynomial is represented as:
P(x) = (x - r?) × (x - r?) × ... × (x - r?)
When x equals any root, the polynomial evaluates to zero. When x is not a root, the function multiplies all the differences (x - root) together.
Example 3: Complex Roots
The function also works with complex roots ?
from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np
# Complex roots
roots = np.array([1+1j, 1-1j])
# Evaluate at real points
x_values = np.array([0, 1, 2])
results = polyvalfromroots(x_values, roots)
print("Complex roots:", roots)
print("X values:", x_values)
print("Polynomial values:", results)
Complex roots: [1.+1.j 1.-1.j] X values: [0 1 2] Polynomial values: [2.+0.j 0.+0.j 2.+0.j]
Conclusion
The polyvalfromroots() function efficiently evaluates polynomials when you know their roots. It returns zero at root values and calculates the polynomial value by multiplying differences at other points. This is particularly useful in numerical analysis and root-finding algorithms.
