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 2-D polynomial on the Cartesian product of x and y in Python
To evaluate a 2-D polynomial on the Cartesian product of x and y, use the polynomial.polygrid2d(x, y, c) method in Python. The method returns the values of the two dimensional polynomial at points in the Cartesian product of x and y.
The first parameter, x and y, are two dimensional series evaluated at the points in the Cartesian product of x and y. 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 second parameter, c is an array of coefficients ordered so that the coefficients for terms of degree i,j are 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. The shape of the result will be c.shape[2:] + x.shape + y.shape.
Syntax
numpy.polynomial.polynomial.polygrid2d(x, y, c)
Parameters
- x, y ? Array-like coordinates at which to evaluate the polynomial
- c ? Array of coefficients for the 2-D polynomial
Example
Let's create a 2D coefficient array and evaluate the polynomial on different coordinate points ?
import numpy as np
from numpy.polynomial.polynomial import polygrid2d
# Create a 2D array of coefficients
c = np.arange(4).reshape(2, 2)
# Display the coefficient array
print("Coefficient Array:")
print(c)
# Check array properties
print("\nDimensions:", c.ndim)
print("Datatype:", c.dtype)
print("Shape:", c.shape)
# Evaluate 2-D polynomial on Cartesian product
result = polygrid2d([1, 2], [1, 2], c)
print("\nPolynomial evaluation result:")
print(result)
Coefficient Array: [[0 1] [2 3]] Dimensions: 2 Datatype: int64 Shape: (2, 2) Polynomial evaluation result: [[ 6. 10.] [11. 18.]]
How It Works
The function evaluates the polynomial p(x,y) = c[0,0] + c[0,1]*y + c[1,0]*x + c[1,1]*x*y for each combination of x and y values. With our coefficient array:
import numpy as np
from numpy.polynomial.polynomial import polygrid2d
# Coefficient array
c = np.array([[0, 1], [2, 3]])
# For x=[1,2] and y=[1,2], the polynomial becomes:
# p(x,y) = 0 + 1*y + 2*x + 3*x*y
# Calculate manually for verification
x_vals = [1, 2]
y_vals = [1, 2]
print("Manual calculation:")
for i, x in enumerate(x_vals):
row = []
for j, y in enumerate(y_vals):
value = 0 + 1*y + 2*x + 3*x*y
print(f"p({x},{y}) = 0 + 1*{y} + 2*{x} + 3*{x}*{y} = {value}")
row.append(value)
# Compare with polygrid2d result
print(f"\nUsing polygrid2d:")
print(polygrid2d([1, 2], [1, 2], c))
Manual calculation: p(1,1) = 0 + 1*1 + 2*1 + 3*1*1 = 6 p(1,2) = 0 + 1*2 + 2*1 + 3*1*2 = 10 p(2,1) = 0 + 1*1 + 2*2 + 3*2*1 = 11 p(2,2) = 0 + 1*2 + 2*2 + 3*2*2 = 18 Using polygrid2d: [[ 6. 10.] [11. 18.]]
Different Input Types
The function accepts various input types for x and y coordinates ?
import numpy as np
from numpy.polynomial.polynomial import polygrid2d
# Coefficient array
c = np.array([[1, 2], [3, 4]])
# Using lists
print("Using lists:")
print(polygrid2d([0, 1], [0, 1], c))
# Using numpy arrays
print("\nUsing numpy arrays:")
x = np.array([0, 1])
y = np.array([0, 1])
print(polygrid2d(x, y, c))
# Using scalar values
print("\nUsing scalars:")
print(polygrid2d(1, 1, c))
Using lists: [[ 1. 3.] [ 4. 10.]] Using numpy arrays: [[ 1. 3.] [ 4. 10.]] Using scalars: 10.0
Conclusion
The polygrid2d() function efficiently evaluates 2-D polynomials on the Cartesian product of coordinate arrays. It's particularly useful for scientific computing applications requiring polynomial interpolation and surface fitting in two dimensions.
