Evaluate a 2-D polynomial at points (x, y) with 3D array of coefficient in Python

To evaluate a 2-D polynomial at points (x, y), use the polynomial.polyval2d() method in Python NumPy. The method returns the values of the two-dimensional polynomial at points formed with pairs of corresponding values from x and y.

The two-dimensional series is evaluated at the points (x, y), where x and y must have the same shape. If x or y is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar.

The parameter c is an array of coefficients ordered so that the coefficient of the term of multidegree i,j is contained in c[i,j]. If c has dimension greater than two, the remaining indices enumerate multiple sets of coefficients. The shape of the result will be c.shape[2:] + x.shape.

Syntax

polynomial.polyval2d(x, y, c)

Parameters

x, y: Array-like points at which to evaluate the polynomial. Must have the same shape.

c: Array of coefficients ordered so that the coefficient of the term of multidegree i,j is contained in c[i,j].

Example

Let's create a 3D array of coefficients and evaluate the 2D polynomial at specific points ?

import numpy as np
from numpy.polynomial.polynomial import polyval2d

# Create a 3D array of coefficients
c = np.arange(24).reshape(2, 2, 6)

# 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)

# To evaluate a 2-D polynomial at points (x, y), use the polynomial.polyval2d() method
print("\nResult...\n", polyval2d([1, 2], [1, 2], c))
Our Array...
[[[ 0  1  2  3  4  5]
  [ 6  7  8  9 10 11]]

 [[12 13 14 15 16 17]
  [18 19 20 21 22 23]]]

Dimensions of our Array...
3

Datatype of our Array object...
int64

Shape of our Array object...
(2, 2, 6)

Result...
[[ 36. 108.]
 [ 40. 117.]
 [ 44. 126.]
 [ 48. 135.]
 [ 52. 144.]
 [ 56. 153.]]

How It Works

The coefficient array has shape (2, 2, 6), where the first two dimensions represent the polynomial degrees and the third dimension contains 6 different coefficient sets. When we evaluate at points [1, 2] for both x and y, the function computes 6 different polynomial evaluations (one for each coefficient set), resulting in a 6×2 output array.

Example with Different Points

Let's evaluate the same polynomial at different coordinate points ?

import numpy as np
from numpy.polynomial.polynomial import polyval2d

# Create a simpler 2D coefficient array
c = np.array([[1, 2], [3, 4]])
print("Coefficient array:\n", c)

# Evaluate at single point (1, 1)
result1 = polyval2d(1, 1, c)
print("\nEvaluation at (1, 1):", result1)

# Evaluate at multiple points
x_points = [0, 1, 2]
y_points = [0, 1, 2]
result2 = polyval2d(x_points, y_points, c)
print("\nEvaluation at multiple points:", result2)
Coefficient array:
[[1 2]
 [3 4]]

Evaluation at (1, 1): 10.0

Evaluation at multiple points: [ 1. 10. 49.]

Conclusion

The polyval2d() function efficiently evaluates 2D polynomials at given points using coefficient arrays. For 3D coefficient arrays, it processes multiple polynomial sets simultaneously, making it useful for batch polynomial evaluations.

Updated on: 2026-03-26T19:36:03+05:30

293 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements