Open In App

Evaluate a 2-D polynomial at points (x, y) in Python

Last Updated : 25 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

We use the polynomial.polyval2d() which is a numpy’s function in Python to assess a 2-D polynomial at the positions (x, y). The technique returns the values of the given two-dimensional polynomial at places produced by x and y pairs. x and y are parameters. At the points (x, y), the two-dimensional series is assessed, where x and y must have the same form. If x or y is a list or tuple, it is first transformed to an ndarray, otherwise, it is kept unmodified and is regarded as a scalar if it isn’t a ndarray.

The parameter ‘Arr’ is an Array of coefficients sorted so that Arr[i,j] contains the coefficient of the term of multidegree i,j. The remaining indices enumerate numerous sets of coefficients if ‘Arr’ has a dimension higher than two. If the form of ‘Arr’ has less than two dimensions, ones are implicitly added to make it two-dimensional. Arr .shape[2:] + x.shape is the shape of the result.

Below are the steps to evaluate 2-D polynomial at point (x,y) :

Step 1: Import NumPy and polyval2d libraries as shown below :

Python3




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


Step 2: Now we have to create a multidimensional array ‘Arr’ of coefficients as shown below :

Python3




Arr = np.arange(4).reshape(2,2)


Step 3: Define x and y (the points at which we are evaluating 2-D polynomial) as shown below :

Python3




x = [1,2]
y = [2,3]


Step 3: Use the polynomial.polyval2d() function to assess a 2-D polynomial at locations (x, y) as shown below :

Python3




print("Result : \n",polyval2d(x,y, Arr))


Example 1 :

Python3




# importing numpy and polyval2d
import numpy as np
from numpy.polynomial.polynomial import polyval2d
 
# Create a multidimensional array of
# coefficients or a matrix
Arr = np.matrix([[4, 2], [6, 3]])
 
# Defining x and y
x = [2, 3]
y = [1, 2]
 
# in order to evaluate a 2-D polynomial
# at points (x, y), we are using the
# polynomial.polyval2d() method in Python
# Numpy
print(polyval2d(x, y, Arr))


Output :

[24. 44.]

Example 2 :

Python3




# import polyval2d from numpy
from numpy.polynomial.polynomial import polyval2d
 
# create a 2 dimensional array
# of coefficient or a matrix
Arr = [[4,2], [6,3]]
 
# evaluate the 2D polynomial at
# point (x,y) using polyval2d
# function and print the result
print(polyval2d([2,3],[1,2],Arr))


Output :

[24. 44.]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads