Skip to main content

A Python implementation of Chebfun

Project description

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 DEVCONTAINER

Open in GitHub Codespaces

๐Ÿ”ฌ 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
  • ๐Ÿ“ 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

Using pip (recommended)

pip install chebpy

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.where(np.abs(x) < 0.5, 1.0, 0.0), [-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, reported as identically zero outside.

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()                           # โ‰ˆ โˆšฯ€

# 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']

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 ๐Ÿ™


Made with โค๏ธ by the ChebPy community

โญ If you find ChebPy useful, please consider giving it a star! โญ

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chebfun-0.9.0.tar.gz (699.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chebfun-0.9.0-py3-none-any.whl (85.3 kB view details)

Uploaded Python 3

File details

Details for the file chebfun-0.9.0.tar.gz.

File metadata

  • Download URL: chebfun-0.9.0.tar.gz
  • Upload date:
  • Size: 699.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for chebfun-0.9.0.tar.gz
Algorithm Hash digest
SHA256 697f98b3928f8ac00dd2fc0e2d230146bed4933c5d9711818a1e27d726876946
MD5 611441cab93eef80ccfa4f990e4b5c13
BLAKE2b-256 00da4608ea4705b3aa18701769ae39cdfdbc7eee233aaaba6c808470336b3658

See more details on using hashes here.

Provenance

The following attestation bundles were made for chebfun-0.9.0.tar.gz:

Publisher: rhiza_release.yml on chebpy/chebpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chebfun-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: chebfun-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 85.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for chebfun-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ccde0cc70f50e00b78179116cdc787830cf1fe7b4d96ec87d3c6115c8c2fbad
MD5 f59933f10c3866298a710ad532ff55a3
BLAKE2b-256 562e67f26fef74fa1370e06fdf960382ede18ef4e3a8545d2836493450ac89d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for chebfun-0.9.0-py3-none-any.whl:

Publisher: rhiza_release.yml on chebpy/chebpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page