Skip to main content

ChebPy

A Python implementation of Chebfun

License: BSD-3-Clause Python versions PyPI - Version

Github Linux MAC OS Code style: ruff uv Hatch project CodeFactor

CI MARIMO

🔬 Numerical computing with Chebyshev series approximations

Symbolic-numeric computation with functions

ChebPy is a Python implementation of Chebfun, bringing the power of Chebyshev polynomial approximations to Python. It allows you to work with functions as first-class objects, performing operations like differentiation, integration, and root-finding with machine precision accuracy.


Table of Contents


✨ Features

Work with functions as easily as numbers

  • 🔢 Function Approximation: Automatic Chebyshev polynomial approximation of smooth functions
  • 🌊 Periodic Functions: Fourier-based approximation via trigfun for smooth periodic functions
  • ♾️ Infinite Intervals: Functions on $[a, \infty)$, $(-\infty, b]$ or the full real line via CompactFun
  • 📍 Endpoint Singularities: Resolve $\sqrt{x}$, $\sqrt{x(1-x)}$ and similar branch-type endpoints to spectral accuracy
  • 📐 Calculus Operations: Differentiation, integration, and root-finding with machine precision
  • 📊 Plotting: Beautiful function visualizations with matplotlib integration
  • 🧮 Arithmetic: Add, subtract, multiply, and compose functions naturally
  • 🎯 Adaptive: Automatically determines optimal polynomial degree for given tolerance
  • 🔁 Convolution: Convolve two Chebfuns to produce a new function
  • 📏 Quasimatrices: Continuous linear algebra via QR, SVD, and least-squares
  • 🎲 Gaussian Process Regression: GP posteriors returned as Chebfun objects
  • 🔗 Interoperability: Works seamlessly with NumPy and SciPy ecosystems

📥 Installation

pip install chebfun

From source (development)

git clone https://github.com/chebpy/chebpy.git
cd chebpy
pip install -e .

Note: Use -e flag for editable installation during development

🛠️ Development

For contributors and advanced users

ChebPy uses modern Python development tools for a smooth developer experience:

# 📦 Install development dependencies
make install

# 🧪 Run tests with coverage
make test

# ✨ Format and lint code
make fmt
make lint

# 📓 Start interactive notebooks
make marimo

# 🔍 View test coverage report
make coverage

Development Tools

  • Testing: pytest with coverage reporting
  • Formatting: ruff for code formatting and linting
  • Notebooks: marimo for interactive development
  • Task Management: Taskfile for build automation

Quick Start

ChebPy Example

This figure was generated with the following simple ChebPy code:

import numpy as np
from chebpy import chebfun

# Create functions as chebfuns on interval [0, 10]
f = chebfun(lambda x: np.sin(x**2) + np.sin(x) ** 2, [0, 10])
g = chebfun(lambda x: np.exp(-((x - 5) ** 2) / 10), [0, 10])

# Find intersection points
roots = (f - g).roots()

# Plot both functions and mark intersections
ax = f.plot(label="f(x) = sin(x²) + sin²(x)")
g.plot(ax=ax, label="g(x) = exp(-(x-5)²/10)")
ax.plot(roots, f(roots), "ro", markersize=8, label="Intersections")
ax.legend()
ax.grid(True, alpha=0.3)

More Examples

# Differentiation and integration
f = chebfun(lambda x: np.exp(x) * np.sin(x), [-1, 1])
df_dx = f.diff()  # Derivative
integral = f.sum()  # Definite integral

# Root finding
g = chebfun(lambda x: x**3 - 2 * x - 5, [-3, 3])
roots = g.roots()  # All roots in the domain

Convolution

Convolve two functions to produce a new Chebfun on the summed domain:

from chebpy import chebfun
import numpy as np

f = chebfun(lambda x: np.exp(-(x**2)), [-1, 1])
g = chebfun(lambda x: np.cos(np.pi * x), [-1, 1])

h = f.conv(g)  # h(x) = ∫ f(t) g(x−t) dt, a Chebfun on [−2, 2]
h.plot()

Quasimatrices

Stack functions as columns of an ∞×n matrix and use continuous linear algebra — QR, SVD, least-squares:

from chebpy import Quasimatrix, chebfun

x = chebfun("x")
A = Quasimatrix([1, x, x**2, x**3, x**4, x**5])

Q, R = A.qr()  # QR factorisation → Legendre polynomials
U, S, V = A.svd()  # Singular value decomposition

f = chebfun(lambda t: np.exp(t) * np.sin(6 * t), [-1, 1])
c = A.solve(f)  # Least-squares polynomial fit
f_approx = A @ c  # Reconstruct as a Chebfun

Gaussian Process Regression

Fit a GP to scattered data and get the posterior mean and variance back as Chebfuns — ready for differentiation, integration, and root-finding:

from chebpy import gpr
import numpy as np

rng = np.random.default_rng(1)
x_obs = np.sort(-2 + 4 * rng.random(10))
y_obs = np.sin(np.exp(x_obs))

f_mean, f_var = gpr(x_obs, y_obs, domain=[-2, 2])

f_mean.plot()  # Posterior mean (a Chebfun)
extrema = f_mean.diff().roots()  # Local extrema via calculus
integral = f_mean.sum()  # Definite integral

Periodic Functions

Use trigfun for smooth periodic functions — the same API as chebfun, but backed by a Fourier (Trigtech) representation that is far more compact for periodic targets:

from chebpy import trigfun
import numpy as np

f = trigfun(lambda x: np.exp(np.sin(np.pi * x)), [-1, 1])
len(f)  # number of Fourier modes
f.diff()  # spectral differentiation in Fourier space
f.sum()  # ≈ 2 · I₀(1)

The gpr interface accepts trig=True for a periodic GP posterior, also returned as a Trigtech-backed Chebfun.

Infinite Intervals

Pass np.inf or -np.inf as a domain endpoint to construct a Chebfun on a (semi-)infinite interval. Pieces with infinite endpoints are automatically built as CompactFun objects: a Chebyshev expansion on the discovered numerical-support window, with optional non-zero tail constants (tail_left, tail_right) recovered automatically for sigmoid-like inputs (tanh, logistic, …).

from chebpy import chebfun
import numpy as np

# Doubly-infinite Gaussian — sum is √π
h = chebfun(lambda x: np.exp(-(x**2)), [-np.inf, np.inf])
h.sum()  # ≈ √π

# Sigmoid-like: tail constants are detected automatically
t = chebfun(np.tanh, [-np.inf, np.inf])
t.funs[0].tail_left, t.funs[0].tail_right  # (-1.0, 1.0)
t(1e10)  # 1.0

# Mixed: finite breakpoints with infinite endpoints
p = chebfun(lambda x: np.exp(-(x**2)), [-np.inf, -2.0, 0.0, 3.0, np.inf])
[type(piece).__name__ for piece in p.funs]
# ['CompactFun', 'Bndfun', 'Bndfun', 'CompactFun']

Endpoint Singularities

Functions with branch-type singularities at one or both endpoints — such as $\sqrt{x}$ on $[0, 1]$ — cannot be resolved by ordinary Chebyshev interpolation. Pass sing="left", "right", or "both" to switch the boundary pieces to Singfun, which uses an exponential clustering map to recover spectral accuracy.

from chebpy import chebfun
import numpy as np

f = chebfun(np.sqrt, [0.0, 1.0], sing="left")
f.sum()  # 2/3, to machine precision

g = chebfun(lambda x: np.sqrt(x * (1 - x)), [0.0, 1.0], sing="both")
g.sum()  # pi/8, to machine precision

Documentation

  • 🚀 Codespaces: Try ChebPy in your browser
  • 📚 Documentation: Full documentation, user guide, and API reference

📄 License

ChebPy is licensed under the 3-Clause BSD License.

📜 See the full license in the LICENSE.rst file.


👥 Contributing

We welcome contributions! 🎉

Whether you're fixing bugs, adding features, or improving documentation, your help makes ChebPy better for everyone.

Quick Start for Contributors

  1. 🍴 Fork the repository
  2. 🌿 Create your feature branch
    git checkout -b feature/amazing-feature
    
  3. ✨ Make your changes and add tests
  4. 🧪 Test your changes
    make test
    
  5. 📝 Commit your changes
    git commit -m 'Add amazing feature'
    
  6. 🚀 Push to your branch
    git push origin feature/amazing-feature
    
  7. 🎯 Open a Pull Request

Resources

Acknowledgments 🙏

ChebPy stands on the shoulders of the Chebfun project led by Nick Trefethen and the Chebfun development team at the University of Oxford. The mathematical design, algorithmic ideas, and naming conventions in this library are direct adaptations of their work — most notably:

We are grateful for their decades of open scholarship, which made this Python port possible. Any errors in translation or adaptation are ours alone.

📜 See the About page for a fuller timeline, key contributors, and foundational publications.

Project tooling:


⭐ If you find ChebPy useful, please consider giving it a star! ⭐

Metadata

Release files for chebfun 0.11.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chebfun 0.11.0
File Size Uploaded Provenance Actions
chebfun-0.11.0.tar.gz 807.6 kB With provenance Details for chebfun-0.11.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for chebfun 0.11.0
File Interpreter ABI Platform Actions
chebfun-0.11.0-py3-none-any.whl Python 3 none any Details for chebfun-0.11.0-py3-none-any.whl

Total release size: 920.0 kB

Release files / chebfun-0.11.0.tar.gz

Download URL chebfun-0.11.0.tar.gz
Size 807.6 kB
Tags Source
SHA-256 checksum
How to use checksums
808dee444f08ded16f623785b954048ee18df750b17d2aa676c8f649c4ad45be
BLAKE2b-256 checksum
How to use checksums
534aa3ebf6c8e722ba1bf7316509898f7fa108fd11a9e832efd35e1eb40cc08b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

Release files / chebfun-0.11.0-py3-none-any.whl

Download URL chebfun-0.11.0-py3-none-any.whl
Size 112.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
173faf7ab273488bbfe3829c565fc416c299b107ab481b422133af181729cf13
BLAKE2b-256 checksum
How to use checksums
228746d383c91bb3d2991c63e451b716087141380b8c387d4619fa683d536f64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

Security for chebfun 0.11.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for chebfun-0.11.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page