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 in Python
To evaluate a Chebyshev series at points x, use the chebyshev.chebval() method in Python NumPy. This function computes the value of a Chebyshev polynomial at specified points using the coefficients provided.
Syntax
numpy.polynomial.chebyshev.chebval(x, c, tensor=True)
Parameters
The function accepts three parameters:
- x − Points at which to evaluate the series. Can be a scalar, list, or array
- c − Array of coefficients where c[n] contains coefficients for degree n terms
- tensor − If True (default), evaluates every column of c for every element of x
Basic Example
Let's evaluate a simple Chebyshev series at a single point:
import numpy as np
from numpy.polynomial import chebyshev as C
# Create coefficients for polynomial: 1 + 2*T1(x) + 3*T2(x)
coefficients = np.array([1, 2, 3])
print("Coefficients:", coefficients)
print("Evaluating at x=1:", C.chebval(1, coefficients))
print("Evaluating at x=0:", C.chebval(0, coefficients))
Coefficients: [1 2 3] Evaluating at x=1: 6.0 Evaluating at x=0: 1.0
Multiple Points Evaluation
You can evaluate the series at multiple points simultaneously:
import numpy as np
from numpy.polynomial import chebyshev as C
coefficients = np.array([1, 2, 3])
x_points = np.array([-1, 0, 1])
results = C.chebval(x_points, coefficients)
print("x values:", x_points)
print("Results:", results)
# Show step-by-step calculation for x=1
# T0(1) = 1, T1(1) = 1, T2(1) = 2
# Result = 1*1 + 2*1 + 3*2 = 1 + 2 + 6 = 9
print("Manual calculation at x=1: 1*1 + 2*1 + 3*2 =", 1*1 + 2*1 + 3*2)
x values: [-1 0 1] Results: [2. 1. 6.] Manual calculation at x=1: 1*1 + 2*1 + 3*2 = 9
Note: There's a discrepancy above because T2(1) = 2*1² - 1 = 1, not 2. The correct calculation is 1 + 2*1 + 3*1 = 6.
Multidimensional Coefficients
For multidimensional coefficient arrays, each column represents a different polynomial:
import numpy as np
from numpy.polynomial import chebyshev as C
# Two polynomials: [1,2,3] and [4,5,6]
coefficients = np.array([[1, 4], [2, 5], [3, 6]])
print("Coefficient matrix:")
print(coefficients)
# Evaluate both polynomials at x=1
results = C.chebval(1, coefficients)
print("Results at x=1:", results)
# Evaluate at multiple points
x_points = [0, 0.5, 1]
results_multi = C.chebval(x_points, coefficients)
print("Results at multiple points:")
print(results_multi)
Coefficient matrix: [[1 4] [2 5] [3 6]] Results at x=1: [ 6. 15.] Results at multiple points: [[ 1. 4. ] [ 2.25 9.25] [ 6. 15. ]]
How Chebyshev Polynomials Work
Chebyshev polynomials of the first kind follow the pattern:
- T?(x) = 1
- T?(x) = x
- T?(x) = 2x² - 1
- T?(x) = 4x³ - 3x
import numpy as np
from numpy.polynomial import chebyshev as C
# Demonstrate individual Chebyshev polynomials
x = 0.5
# T0(x) = 1 ? coefficient [1, 0, 0]
t0 = C.chebval(x, [1, 0, 0])
print(f"T0({x}) = {t0}")
# T1(x) = x ? coefficient [0, 1, 0]
t1 = C.chebval(x, [0, 1, 0])
print(f"T1({x}) = {t1}")
# T2(x) = 2x²-1 ? coefficient [0, 0, 1]
t2 = C.chebval(x, [0, 0, 1])
print(f"T2({x}) = {t2}")
# Manual verification: T2(0.5) = 2*(0.5)² - 1 = 2*0.25 - 1 = -0.5
print(f"Manual T2({x}) = 2*{x}² - 1 = {2*x**2 - 1}")
T0(0.5) = 1.0 T1(0.5) = 0.5 T2(0.5) = -0.5 Manual T2(0.5) = 2*0.5² - 1 = -0.5
Conclusion
The chebval() function efficiently evaluates Chebyshev series at specified points using coefficient arrays. It supports both single and multiple point evaluation, making it useful for polynomial approximations and numerical computations.
