Chebyshev approximation in Python

Chebyshev approximation is covered in Chapter 5.8 of Numerical Recipes in C.

Here's a similar implementation in Python:

import math

class Chebyshev:
    """
    Chebyshev(a, b, n, func)
    Given a function func, lower and upper limits of the interval [a,b],
    and maximum degree n, this class computes a Chebyshev approximation
    of the function.
    Method eval(x) yields the approximated function value.
    """
    def __init__(self, a, b, n, func):
        self.a = a
        self.b = b
        self.func = func

        bma = 0.5 * (b - a)
        bpa = 0.5 * (b + a)
        f = [func(math.cos(math.pi * (k + 0.5) / n) * bma + bpa) for k in range(n)]
        fac = 2.0 / n
        self.c = [fac * sum([f[k] * math.cos(math.pi * j * (k + 0.5) / n)
                  for k in range(n)]) for j in range(n)]

    def eval(self, x):
        a,b = self.a, self.b
        assert(a <= x <= b)
        y = (2.0 * x - a - b) * (1.0 / (b - a))
        y2 = 2.0 * y
        (d, dd) = (self.c[-1], 0)             # Special case first step for efficiency
        for cj in self.c[-2:0:-1]:            # Clenshaw's recurrence
            (d, dd) = (y2 * d - dd + cj, d)
        return y * d - dd + 0.5 * self.c[0]   # Last step is different

For example, to make an 8th-degree approximation to sinx, where 0 ≤ x ≤ (π)/(12), and use it to compute sin0.1:

>>> import math
>>> from chebyshev import Chebyshev
>>> ch = Chebyshev(0, math.pi / 12, 8, math.sin)
>>> ch.eval(0.1)
0.099833416646828876
>>> math.sin(0.1)
0.099833416646828155