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 Chebyshev series at points x and the shape of the coefficient array extended for each dimension of x in Python
To evaluate a Chebyshev series at points x, use the chebyshev.chebval() method in Python NumPy. This function evaluates Chebyshev polynomials at specified points and handles multidimensional coefficient arrays efficiently.
Parameters
The chebval() method takes three main parameters:
- x: The points where the series is evaluated. Can be a scalar, list, tuple, or ndarray
-
c: Array of coefficients where
c[n]contains coefficients for terms of degree n - tensor: Boolean flag controlling shape extension behavior (default: True)
Understanding the Tensor Parameter
When tensor=True, the coefficient array shape is extended with ones on the right for each dimension of x. This means every column of coefficients is evaluated for every element of x.
Example
Let's create a multidimensional coefficient array and evaluate the Chebyshev series ?
import numpy as np
from numpy.polynomial import chebyshev as C
# Create a multidimensional array of coefficients
c = np.arange(6).reshape(2, 3)
# 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 Chebyshev series at points x=[1,2] with tensor=True
print("\nResult (chebval)...")
print(C.chebval([1, 2], c, tensor=True))
Our Array... [[0 1 2] [3 4 5]] Dimensions of our Array... 2 Datatype of our Array object... int64 Shape of our Array object... (2, 3) Result (chebval)... [[ 3. 6.] [ 5. 9.] [ 7. 12.]]
How It Works
In this example, the coefficient array c has shape (2, 3), representing 3 polynomials with 2 coefficients each. When evaluated at points [1, 2] with tensor=True, each polynomial is evaluated at both points, resulting in a 3×2 output array.
Comparison: Tensor True vs False
import numpy as np
from numpy.polynomial import chebyshev as C
c = np.arange(6).reshape(2, 3)
print("With tensor=True:")
result_true = C.chebval([1, 2], c, tensor=True)
print(f"Shape: {result_true.shape}")
print(result_true)
print("\nWith tensor=False:")
result_false = C.chebval([1, 2], c, tensor=False)
print(f"Shape: {result_false.shape}")
print(result_false)
With tensor=True: Shape: (3, 2) [[ 3. 6.] [ 5. 9.] [ 7. 12.]] With tensor=False: Shape: (3,) [ 3. 9. 12.]
Conclusion
The chebval() method efficiently evaluates Chebyshev series at multiple points. Use tensor=True to evaluate all polynomials at all points, or tensor=False to broadcast x over coefficient columns for element-wise evaluation.
