Evaluate a polynomial and every column of coefficients in r is evaluated for every element of x in Python

The polyvalfromroots() method in NumPy evaluates polynomials specified by their roots at given points. When working with multidimensional arrays, the tensor parameter controls how evaluation is performed across columns.

Syntax

numpy.polynomial.polynomial.polyvalfromroots(x, r, tensor=True)

Parameters

x: Array of points where the polynomial is evaluated. Can be scalar, list, or array.

r: Array of roots. If multidimensional, first index is the root index, remaining indices enumerate multiple polynomials.

tensor: Boolean parameter controlling evaluation behavior ?

  • True (default): Every column of coefficients in r is evaluated for every element of x
  • False: x is broadcast over the columns of r for evaluation

Basic Example

Let's create a multidimensional array of roots and evaluate polynomials ?

from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np

# Create multidimensional array of roots
roots = np.arange(-2, 2).reshape(2, 2)
print("Roots array:")
print(roots)
print("\nShape:", roots.shape)
Roots array:
[[-2 -1]
 [ 0  1]]

Shape: (2, 2)

Evaluating with tensor=True

With tensor=True, each column of roots is evaluated for every point in x ?

from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np

roots = np.arange(-2, 2).reshape(2, 2)
points = [-2, 1]

# tensor=True: evaluate each column for each point
result_tensor = polyvalfromroots(points, roots, tensor=True)
print("With tensor=True:")
print(result_tensor)
print("Shape:", result_tensor.shape)
With tensor=True:
[[-0.  3.]
 [ 3.  0.]]
Shape: (2, 2)

Evaluating with tensor=False

With tensor=False, x is broadcast over the columns of r ?

from numpy.polynomial.polynomial import polyvalfromroots
import numpy as np

roots = np.arange(-2, 2).reshape(2, 2)
points = [-1, 2]

# tensor=False: broadcast x over columns
result_broadcast = polyvalfromroots(points, roots, tensor=False)
print("With tensor=False:")
print(result_broadcast)
print("Shape:", result_broadcast.shape)
With tensor=False:
[1. 6.]
Shape: (2,)

How It Works

The polynomial from roots [r?, r?, ..., r?] is constructed as (x - r?)(x - r?)...(x - r?). For our example with roots [-2, -1], the polynomial becomes (x - (-2))(x - (-1)) = (x + 2)(x + 1).

Comparison

Parameter Behavior Output Shape Use Case
tensor=True Each column evaluated for each x point (len(x), cols) Multiple polynomials, multiple points
tensor=False x broadcast over columns (cols,) Paired evaluation

Conclusion

Use polyvalfromroots() to evaluate polynomials from their roots. The tensor parameter controls whether each polynomial column is evaluated for all x points or paired with corresponding x values.

Updated on: 2026-03-26T19:38:23+05:30

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements