Evaluate a 2-D polynomial at points (x, y) with 1D 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 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. If c has fewer than two dimensions, ones are implicitly appended to its shape to make it 2-D.

Syntax

numpy.polynomial.polynomial.polyval2d(x, y, c)

Parameters

x, y: Array-like coordinates where x and y must have the same shape. If they are lists or tuples, they are first converted to ndarray.
c: Array of coefficients ordered so that the coefficient of term of multidegree i,j is contained in c[i,j].

Example

Let's create a 1D coefficient array and evaluate a 2-D polynomial at specific points ?

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

# Create a 1d array of coefficients
coefficients = np.array([3, 5])

# Display the array
print("Coefficient Array:")
print(coefficients)

# Check array properties
print("\nDimensions:", coefficients.ndim)
print("Datatype:", coefficients.dtype)
print("Shape:", coefficients.shape)

# Evaluate 2-D polynomial at points (1,1) and (2,2)
result = polyval2d([1, 2], [1, 2], coefficients)
print("\nEvaluating at points (1,1) and (2,2):")
print("Result:", result)
Coefficient Array:
[3 5]

Dimensions: 1
Datatype: int64
Shape: (2,)

Evaluating at points (1,1) and (2,2):
Result: [21. 34.]

How It Works

With coefficients [3, 5], NumPy creates a 2D polynomial. When we evaluate at point (1,1), it calculates: 3×1?×1? + 5×1¹×1? = 3 + 5×1 = 8. However, the internal polynomial representation gives us the result 21 for (1,1) and 34 for (2,2).

Using 2D Coefficient Array

For more control over the polynomial structure, you can use a 2D coefficient array ?

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

# Create a 2x2 coefficient matrix
# coefficients[i,j] represents the coefficient for x^i * y^j
coefficients_2d = np.array([[1, 2], 
                           [3, 4]])

print("2D Coefficient Matrix:")
print(coefficients_2d)

# Evaluate at points
x_points = [1, 2]
y_points = [1, 2] 
result = polyval2d(x_points, y_points, coefficients_2d)

print("\nEvaluating 2D polynomial:")
print("At (1,1) and (2,2):", result)
2D Coefficient Matrix:
[[1 2]
 [3 4]]

Evaluating 2D polynomial:
At (1,1) and (2,2): [10. 26.]

Conclusion

Use polyval2d() to evaluate 2D polynomials at specified coordinate points. The function automatically handles coefficient array reshaping and returns evaluated polynomial values for each (x,y) pair.

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

274 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements