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 Chebyshev series at points (x, y) with 3D array of coefficient in Python
To evaluate a 2-D Chebyshev series at points (x, y), use the polynomial.chebval2d() method in Python NumPy. The method returns the values of the two dimensional Chebyshev series at points formed from pairs of corresponding values from x and y.
The parameter c is an array of coefficients ordered so that the coefficient of the term of multidegree i,j is contained in c[i,j]. If c has dimension greater than 2, the remaining indices enumerate multiple sets of coefficients.
Syntax
numpy.polynomial.chebyshev.chebval2d(x, y, c)
Parameters
The parameters x and y represent evaluation points where x and y must have the same shape. If x or y is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged.
Creating a 3D Coefficient Array
Let's start by importing the required libraries and creating a 3D array of coefficients ?
import numpy as np
from numpy.polynomial import chebyshev as C
# Create a 3D array of coefficients
c = np.arange(24).reshape(2, 2, 6)
print("Coefficient Array:")
print(c)
print("\nShape:", c.shape)
print("Dimensions:", c.ndim)
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]]] Shape: (2, 2, 6) Dimensions: 3
Evaluating the 2-D Chebyshev Series
Now we'll evaluate the 2-D Chebyshev series at specific points using chebval2d() ?
import numpy as np
from numpy.polynomial import chebyshev as C
# Create a 3D array of coefficients
c = np.arange(24).reshape(2, 2, 6)
# Evaluate at points (1,1) and (2,2)
x_points = [1, 2]
y_points = [1, 2]
result = C.chebval2d(x_points, y_points, c)
print("Evaluation at points (1,1) and (2,2):")
print(result)
Evaluation at points (1,1) and (2,2): [[ 36. 108.] [ 40. 117.] [ 44. 126.] [ 48. 135.] [ 52. 144.] [ 56. 153.]]
How It Works
The function evaluates each 2D slice of the 3D coefficient array separately. Since our coefficient array has shape (2, 2, 6), it contains 6 different 2×2 coefficient matrices. Each evaluation point pair produces results for all 6 sets of coefficients.
Example with Different Points
Let's evaluate the series at more diverse points to see the variation ?
import numpy as np
from numpy.polynomial import chebyshev as C
# Create coefficient array
c = np.arange(12).reshape(2, 2, 3)
print("Simplified coefficient array shape:", c.shape)
# Evaluate at different points
x_vals = [0, 1, -1]
y_vals = [0, 1, -1]
result = C.chebval2d(x_vals, y_vals, c)
print("\nEvaluation results:")
print(result)
Simplified coefficient array shape: (2, 2, 3) Evaluation results: [[ 0. 3.] [ 6. 9.] [12. 15.]]
Conclusion
The chebval2d() function efficiently evaluates 2-D Chebyshev series at specified points. When using 3D coefficient arrays, it processes each 2D slice independently, making it useful for batch evaluations of multiple polynomial series.
