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 2D array of coefficient in Python
To evaluate a 3-D polynomial at points (x, y, z), use the polynomial.polyval3d() method in Python NumPy. The method returns the values of the multidimensional polynomial on points formed with triples of corresponding values from x, y, and z.
Syntax
numpy.polynomial.polynomial.polyval3d(x, y, z, c)
Parameters
- x, y, z ? The three dimensional series is evaluated at the points (x, y, z), where x, y, and z 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 and if it isn't an ndarray it is treated as a scalar.
- c ? An array of coefficients ordered so that the coefficient of the term of multi-degree 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. If c has fewer than 3 dimensions, ones are implicitly appended to its shape to make it 3-D.
Return Value
Returns the evaluated 3-D polynomial values. The shape of the result will be c.shape[3:] + x.shape.
Example
Let's create a 2D array of coefficients and evaluate the 3-D polynomial at specific points ?
import numpy as np
from numpy.polynomial.polynomial import polyval3d
# 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:", c.ndim)
# Get the datatype
print("Datatype of our Array object:", c.dtype)
# Get the shape
print("Shape of our Array object:", c.shape)
# Evaluate 3-D polynomial at points (x, y, z)
result = polyval3d([1, 2], [1, 2], [1, 2], c)
print("\nResult:")
print(result)
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: [24. 42.]
How It Works
The 3-D polynomial evaluation works as follows ?
- The coefficient array
crepresents polynomial terms wherec[i,j,k]is the coefficient for the termx^i * y^j * z^k - Since our array is 2D (2×2), it's treated as having shape (2,2,1) for 3D evaluation
- For points (1,1,1) and (2,2,2), the polynomial is evaluated using all coefficient terms
- The result is an array containing the polynomial values at each point
Multiple Points Example
You can evaluate the polynomial at multiple coordinate points simultaneously ?
import numpy as np
from numpy.polynomial.polynomial import polyval3d
# Create coefficient array
coefficients = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
# Define coordinate points
x_points = [0, 1, 2]
y_points = [0, 1, 2]
z_points = [0, 1, 2]
# Evaluate 3-D polynomial
result = polyval3d(x_points, y_points, z_points, coefficients)
print("Coefficient array shape:", coefficients.shape)
print("Result:", result)
Coefficient array shape: (2, 2, 2) Result: [ 1. 48. 179.]
Conclusion
The polyval3d() function efficiently evaluates 3-D polynomials at given coordinate points using coefficient arrays. It handles coordinate broadcasting and supports multiple evaluation points simultaneously, making it useful for scientific computing and mathematical applications.
