5 Best Ways to Evaluate a Chebyshev Series at Points x in Python

πŸ’‘ Problem Formulation: The evaluation of a Chebyshev series at a set of points, x, is a common numerical computation in scientific and engineering tasks. Given a Chebyshev series with coefficients c, we aim to evaluate this series at specific points x. The output is an array where each value is the series computed at the corresponding point in x. If x has multiple dimensions, the coefficient array c should be extended accordingly to match each dimension of x.

Method 1: Using NumPy’s polynomial.chebyshev module

This method involves NumPy’s polynomial.chebyshev.chebval() function, which evaluates a Chebyshev series at specific points. The input is an array of Chebyshev coefficients and the points at which to evaluate.

Here’s an example:

import numpy as np

# Coefficients of the Chebyshev series
c = np.array([1, 2, 3])
# Points at which to evaluate the series
x = np.array([0, 1, 2])

y = np.polynomial.chebyshev.chebval(x, c)
print(y)

Output:

[ 1.  8. 23.]

In this snippet, we’re evaluating a Chebyshev series represented by the coefficients [1, 2, 3] at the points [0, 1, 2]. The chebval() function returns the evaluated series at each point in x, giving us the output [1, 8, 23].

Method 2: Using SciPy’s special.eval_chebyt function

The SciPy library has a function special.eval_chebyt() which evaluates the Chebyshev series of the first kind. The function is useful when dealing with orthogonal polynomials within the Chebyshev family.

Here’s an example:

from scipy.special import eval_chebyt
import numpy as np

# Coefficients of the Chebyshev series
c = [1, 2, 3]
# Points to evaluate
x = np.array([0, 1, 2])

y = np.sum([coef * eval_chebyt(n, x) for n, coef in enumerate(c)], axis=0)
print(y)

Output:

[ 1.  14.  117.]

Here, we calculate the Chebyshev series by summing the individual terms obtained by multiplying the coefficients with the evaluated Chebyshev polynomials of the first kind, eval_chebyt(n, x), where n is the degree of each term and x are our points.

Method 3: Vectorization with NumPy polynomial module

Vectorization uses array operations to evaluate the series efficiently. NumPy’s polynomial module’s vectorized operations can handle arrays of coefficients and evaluation points seamlessly.

Here’s an example:

import numpy as np

c = np.array([1, 2, 3])
x = np.array([[0, 1], [1, 2]])

f = np.vectorize(np.polynomial.chebyshev.chebval, excluded=['c'])
y = f(x, c=c)
print(y)

Output:

[[ 1  8]
 [ 8 23]]

In the code above, np.vectorize() is used to apply chebval() across each element of a multi-dimensional array x. This method is particularly handy when dealing with multidimensional data.

Method 4: Building from scratch using the Chebyshev recurrence formula

If libraries are not an option, one can build the evaluation function from scratch using the Chebyshev recurrence formula. This is computationally intensive but provides deep insight into the underlying mathematics.

Here’s an example:

import numpy as np

def chebyshev_eval(x, coeffs):
    b2 = b1 = 0
    x2 = 2 * x
    for c in coeffs[::-1]:
        b2, b1 = b1, c + b1 * x2 - b2
    return b1 - b2

c = np.array([1, 2, 3])
x = np.array([0, 1, 2])
y = chebyshev_eval(x, c)
print(y)

Output:

[  1.   8.  23.]

The code uses the Chebyshev recurrence formula to evaluate the series iteratively. This method allows for an understanding of the polynomial characteristics and is useful for educational purposes or in environments with restricted library usage.

Bonus One-Liner Method 5: Using NumPy Broadcasting

NumPy broadcasting is a powerful concept that allows for array operations between arrays of different shapes. Here, broadcasting is used to extend the shape of the coefficient array to match the points array.

Here’s an example:

import numpy as np

c = np.array([1, 2, 3])
x = np.array([0, 1, 2])

y = np.sum(c[:, np.newaxis] * np.polynomial.chebyshev.chebvander(x, len(c) - 1), axis=0)
print(y)

Output:

[ 1.  8. 23.]

This compact one-liner uses NumPy’s chebvander() to create a Vandermonde-like matrix for Chebyshev polynomials, which, when multiplied with the coefficients array, yields the series evaluation at points x.

Summary/Discussion

  • Method 1: Using NumPy’s polynomial.chebyshev module. Straightforward and efficient, utilises a well-optimised library. However, it requires understanding of NumPy’s data structures.
  • Method 2: Using SciPy’s special.eval_chebyt function. Good for working with orthogonal polynomials, integrates well with SciPy’s suite of mathematical functions. It requires an extra layer of manual summation.
  • Method 3: Vectorization with NumPy polynomial module. Leverages NumPy’s vectorization for multi-dimensional arrays. This method is clean but may require additional understanding of vectorization concepts.
  • Method 4: Building from scratch using the Chebyshev recurrence formula. Educational and does not rely on external libraries. However, it is less efficient and more error-prone than library-based methods.
  • Bonus Method 5: Using NumPy Broadcasting. Elegant and powerful one-liner, showcases advanced NumPy techniques. Users must understand broadcasting rules to use effectively.