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 at points (x, y, z) with 4D array of coefficient in Python
To evaluate a 3-D polynomial at points (x, y, z), use the polynomial.polyval3d() method in NumPy. The method returns the values of the multidimensional polynomial on points formed with triples of corresponding values from x, y, and z.
The parameters are x, y, z coordinates where the three dimensional series is evaluated at the points (x, y, z). These coordinates must have the same shape. If any of x, y, or z is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged.
The parameter c is an array of coefficients ordered so that the coefficient of the term of multidegree i,j,k is contained in c[i,j,k]. If c has dimension greater than 3, the remaining indices enumerate multiple sets of coefficients. The shape of the result will be c.shape[3:] + x.shape.
Syntax
numpy.polynomial.polynomial.polyval3d(x, y, z, c)
Parameters
x, y, z: Array-like coordinates. Must have the same shape.
c: Array of coefficients with shape compatible for 3D polynomial evaluation.
Example
Let's create a 4D coefficient array and evaluate a 3D polynomial at specific points ?
import numpy as np
from numpy.polynomial.polynomial import polyval3d
# Create a 4D array of coefficients
c = np.arange(48).reshape(2, 2, 6, 2)
# Display the array
print("Coefficient Array:")
print("Shape:", c.shape)
print("Dimensions:", c.ndim)
print("Datatype:", c.dtype)
# Evaluate 3-D polynomial at points (x, y, z)
result = polyval3d([1, 2], [1, 2], [1, 2], c)
print("\nResult:")
print(result)
Coefficient Array: Shape: (2, 2, 6, 2) Dimensions: 4 Datatype: int64 Result: [[ 552. 18252.] [ 576. 18819.]]
How It Works
The function evaluates the polynomial using the coefficient array where:
- The first three dimensions (2, 2, 6) represent the polynomial degrees in x, y, z directions
- The fourth dimension (2) represents multiple coefficient sets
- Each point (x[i], y[i], z[i]) produces a result for each coefficient set
Example with Different Points
import numpy as np
from numpy.polynomial.polynomial import polyval3d
# Create a simpler 3D coefficient array
c = np.arange(8).reshape(2, 2, 2)
print("Coefficients shape:", c.shape)
print("Coefficients:\n", c)
# Evaluate at different points
x_points = [0, 1, 2]
y_points = [0, 1, 1]
z_points = [1, 0, 1]
result = polyval3d(x_points, y_points, z_points, c)
print("\nEvaluation result:", result)
Coefficients shape: (2, 2, 2) Coefficients: [[[0 1] [2 3]] [[4 5] [6 7]]] Evaluation result: [2. 4. 22.]
Conclusion
The polyval3d() function efficiently evaluates 3D polynomials at multiple points simultaneously. It handles coefficient arrays with extra dimensions, making it useful for batch polynomial evaluations in scientific computing applications.
