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 with 3d array of coefficient 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 the coordinate arrays evaluated at points in the Cartesian product. If x or y is a list or tuple, it is first converted to an ndarray. The second parameter, c, is an array of coefficients where 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.
Syntax
numpy.polynomial.polynomial.polygrid2d(x, y, c)
Parameters
x, y: Array-like coordinates for evaluation
c: Array of coefficients with shape (M, N, ...) where M and N represent polynomial degrees
Understanding 3D Coefficient Arrays
When c has more than 2 dimensions, the shape of the result will be c.shape[2:] + x.shape + y.shape. This allows evaluation of multiple polynomial sets simultaneously ?
import numpy as np
from numpy.polynomial.polynomial import polygrid2d
# Create a 3D array of coefficients (2x2x6)
c = np.arange(24).reshape(2, 2, 6)
print("Coefficient Array Shape:", c.shape)
print("First coefficient set:\n", c[:, :, 0])
Coefficient Array Shape: (2, 2, 6) First coefficient set: [[ 0 6] [12 18]]
Example: Evaluating Multiple Polynomials
Here's a complete example showing how to evaluate multiple 2D polynomials simultaneously ?
import numpy as np
from numpy.polynomial.polynomial import polygrid2d
# Create a 3D array of coefficients
c = np.arange(24).reshape(2, 2, 6)
print("Our Array...")
print(c)
print("\nDimensions of our Array:", c.ndim)
print("Datatype of our Array:", c.dtype)
print("Shape of our Array:", c.shape)
# Evaluate on Cartesian product of [1,2] x [1,2]
result = polygrid2d([1, 2], [1, 2], c)
print("\nResult shape:", result.shape)
print("Result...")
print(result)
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: int64 Shape of our Array: (2, 2, 6) Result shape: (6, 2, 2) Result... [[[ 36. 60.] [ 66. 108.]] [[ 40. 66.] [ 72. 117.]] [[ 44. 72.] [ 78. 126.]] [[ 48. 78.] [ 84. 135.]] [[ 52. 84.] [ 90. 144.]] [[ 56. 90.] [ 96. 153.]]]
How the Result is Structured
The result has shape (6, 2, 2) because:
- 6 comes from the third dimension of coefficients (multiple polynomial sets)
- 2, 2 comes from the Cartesian product of [1,2] × [1,2]
Each 2×2 matrix represents the evaluation of one polynomial set at the four coordinate pairs: (1,1), (1,2), (2,1), and (2,2).
Conclusion
The polygrid2d() function efficiently evaluates multiple 2D polynomials simultaneously using 3D coefficient arrays. The result shape follows the pattern c.shape[2:] + x.shape + y.shape, making it useful for batch polynomial evaluations.
