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 3-D polynomial on the Cartesian product of x, y, z with 4d array of coefficient in Python
To evaluate a 3-D polynomial on the Cartesian product of x, y, and z, use the polynomial.polygrid3d() method in Python. This method returns the values of a three-dimensional polynomial at points in the Cartesian product of x, y, and z coordinates.
Syntax
numpy.polynomial.polynomial.polygrid3d(x, y, z, c)
Parameters
The function accepts the following parameters ?
- x, y, z ? Three-dimensional coordinates where the polynomial is evaluated at points in the Cartesian product. If any parameter is a list or tuple, it is converted to an ndarray. Scalars are treated as such.
-
c ? Array of coefficients ordered so that coefficients for terms of degree i, j, k are contained in
c[i,j,k]. If c has more than three dimensions, the remaining indices enumerate multiple sets of coefficients.
Example
Let's create a 4D array of coefficients and evaluate the polynomial on a Cartesian product ?
import numpy as np
from numpy.polynomial.polynomial import polygrid3d
# Create a 4D array of coefficients
c = np.arange(48).reshape(2, 2, 6, 2)
# Display the array
print("Coefficient Array:")
print(c)
# Check array properties
print(f"\nDimensions: {c.ndim}")
print(f"Datatype: {c.dtype}")
print(f"Shape: {c.shape}")
# Evaluate 3D polynomial on Cartesian product
x = [1, 2]
y = [1, 2]
z = [1, 2]
result = polygrid3d(x, y, z, c)
print(f"\nPolynomial evaluation result:")
print(result)
Coefficient 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]]] [[[24 25] [26 27] [28 29] [30 31] [32 33] [34 35]] [[36 37] [38 39] [40 41] [42 43] [44 45] [46 47]]]] Dimensions: 4 Datatype: int64 Shape: (2, 2, 6, 2) Polynomial evaluation result: [[[[ 552. 6600.] [ 900. 10656.]] [[ 972. 11412.] [1566. 18252.]]] [[[ 576. 6852.] [ 936. 11034.]] [[ 1008. 11790.] [ 1620. 18819.]]]]
How It Works
The result shape follows the formula: c.shape[3:] + x.shape + y.shape + z.shape. In our example ?
- Coefficient array shape: (2, 2, 6, 2)
- x, y, z arrays each have shape: (2,)
- Result shape: (2,) + (2,) + (2,) + (2,) = (2, 2, 2, 2)
Alternative Example with Different Coordinates
import numpy as np
from numpy.polynomial.polynomial import polygrid3d
# Create simpler coefficient array
c = np.ones((2, 2, 2))
# Different coordinate values
x = [0, 1]
y = [0, 1]
z = [0, 1]
result = polygrid3d(x, y, z, c)
print("Simple polynomial evaluation:")
print(result)
print(f"Result shape: {result.shape}")
Simple polynomial evaluation: [[[1. 2.] [2. 4.]] [[2. 4.] [4. 8.]]] Result shape: (2, 2, 2)
Conclusion
The polygrid3d() function efficiently evaluates 3D polynomials on Cartesian products of coordinate arrays. The result dimensions depend on both the coefficient array structure and input coordinate shapes, making it versatile for multi-dimensional polynomial computations.
