Skip to content

Function Approximation

ChebPy automatically approximates smooth functions with Chebyshev polynomials to machine precision.

Adaptive Construction

Pass any callable to chebfun and ChebPy determines the optimal polynomial degree:

import numpy as np
from chebpy import chebfun

f = chebfun(lambda x: np.exp(np.sin(x)), [-5, 5])
print(len(f))  # polynomial degree chosen automatically

Fixed-Length Construction

Specify the number of points explicitly with the n parameter:

f = chebfun(lambda x: np.sin(x), [-np.pi, np.pi], n=32)

Equispaced Sample Data

Use equifun when you already have one-dimensional values sampled on an equispaced grid that includes both interval endpoints:

import matplotlib.pyplot as plt
import numpy as np
from chebpy import equifun

nodes = np.linspace(0.0, 2.0 * np.pi, 17)
values = np.sin(nodes) + 0.25 * np.cos(3.0 * nodes)
f = equifun(values, [0.0, 2.0 * np.pi])

xx = np.linspace(0.0, 2.0 * np.pi, 500)
plt.plot(xx, f(xx), label="equifun")
plt.plot(nodes, values, "o", label="samples")
plt.legend()
plt.savefig("docs/assets/equifun-examples.png", dpi=180)

For equispaced data, ChebPy first builds a Floater-Hormann rational interpolant through the samples and then adaptively represents it as a Chebfun. This is often more stable than high-degree polynomial interpolation on equispaced nodes, including Runge-style data:

Equifun examples

Special Constructors

# Identity function
x = chebfun('x')

# Constant function
c = chebfun(3.14)

# Piecewise-constant function
from chebpy import pwc
f = pwc(domain=[-2, -1, 0, 1, 2], values=[-1, 0, 1, 2])

Multi-Interval Functions

ChebPy can represent functions with breakpoints as piecewise Chebyshev expansions:

f = chebfun(lambda x: np.abs(x), [-1, 0, 1])

Chebyshev Points

Use chebpts to get the Chebyshev interpolation points and barycentric weights:

from chebpy import chebpts

pts, wts = chebpts(16)              # 16 points on [-1, 1]
pts, wts = chebpts(16, [0, 3])      # 16 points on [0, 3]

References