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 2d array of coefficient in Python
To evaluate a 3-D polynomial on the Cartesian product of x, y, z coordinates, use the numpy.polynomial.polynomial.polygrid3d() method in Python. This method computes polynomial values at all combinations of the input coordinate arrays.
Syntax
numpy.polynomial.polynomial.polygrid3d(x, y, z, c)
Parameters
The function takes the following parameters ?
- x, y, z ? One-dimensional arrays of coordinates. The polynomial is evaluated at points in the Cartesian product of x, y, and z. If any parameter is a list or tuple, it is converted to an ndarray.
- 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 fewer than three dimensions, ones are implicitly appended to make it 3-D.
Example
Let's create a 2D coefficient array and evaluate a 3-D polynomial ?
import numpy as np
from numpy.polynomial.polynomial import polygrid3d
# Create a 2d array of coefficients
c = np.arange(4).reshape(2,2)
# Display the array
print("Our Array...")
print(c)
# Check the Dimensions
print("\nDimensions of our Array...")
print(c.ndim)
# Get the Datatype
print("\nDatatype of our Array object...")
print(c.dtype)
# Get the Shape
print("\nShape of our Array object...")
print(c.shape)
# Evaluate 3-D polynomial on Cartesian product
print("\nResult...")
print(polygrid3d([1,2],[1,2],[1,2],c))
Our Array... [[0 1] [2 3]] Dimensions of our Array... 2 Datatype of our Array object... int64 Shape of our Array object... (2, 2) Result... [[17. 28.] [28. 46.]]
How It Works
The function evaluates the polynomial at each point (x[i], y[j], z[k]) for all combinations of indices. Since our coefficient array is 2D with shape (2,2), it gets expanded to 3D internally by appending ones. The result shape is determined by the Cartesian product of the input arrays.
Example with Different Coordinates
import numpy as np
from numpy.polynomial.polynomial import polygrid3d
# Create coefficient array
c = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
# Different coordinate arrays
x = [0, 1]
y = [0, 1]
z = [0, 1]
print("Coefficient array shape:", c.shape)
print("\nEvaluating polynomial...")
result = polygrid3d(x, y, z, c)
print("Result shape:", result.shape)
print("Result:")
print(result)
Coefficient array shape: (2, 2, 2) Evaluating polynomial... Result shape: (2, 2, 2) Result: [[[1. 2.] [3. 4.]] [[5. 6.] [7. 8.]]]
Conclusion
The polygrid3d() function efficiently evaluates 3-D polynomials on Cartesian coordinate products. Use it when you need to compute polynomial values across a 3D grid of points with given coefficient arrays.
