Skip to main content

Piecewise polynomial approximation: code generator for Chebyshev approximation

Project description

ppapp - Piecewise Polynomial Approximation

Code generator for piecewise Chebyshev approximation of mathematical functions.

Overview

This package generates C source code containing polynomial coefficients that approximate a given function f(x) over a specified domain. The approximation uses piecewise Chebyshev polynomials with octave-based domain subdivision for optimal accuracy.

Reference: Joachim Wuttke and Alexander Kleinsorge, "Algorithm 1XXX: Code generation for piecewise Chebyshev approximation"

Installation

pip install ppapp

Requirements

  • Python 3.9+
  • python-flint (for arbitrary-precision arithmetic)

Quick Start

Command Line

# Compute a function value
ppapp v ppapp.demo_functions.imwofx 1.0

# Compute error bound for given M and N
ppapp e ppapp.demo_functions.imwofx 5 10

# Generate C source code
ppapp s ppapp.demo_functions.imwofx 5 10 > coefficients.c

Using Demo Functions

The package includes several demo functions:

# Im w(x) = exp(-x^2) * erfi(x)
ppapp e ppapp.demo_functions.imwofx 5 10

# erfcx(x) = exp(x^2) * erfc(x)
ppapp e ppapp.demo_functions.erfcx 5 10

# Polynomial x^3 - x^2 + x - 1
ppapp e ppapp.demo_functions.polynomial 8 15

Python API

from ppapp import (
    set_reference_function,
    compute_subdomains,
    cheb_pm,
    analyse_row,
    error_bound,
)
from ppapp.demo_functions.imwofx import my_arb_f, my_domain

# Set the reference function
set_reference_function(my_arb_f)

# Compute subdomains
a, b = my_domain
j0, l0, D = compute_subdomains(a, b, M=5)

# Compute coefficients for each subdomain
for d in D:
    d.coeffs = cheb_pm(d.a, d.b, N=10)
    d.s, d.r = analyse_row(d.coeffs)
    err = error_bound(d, N=10)
    print(f"Subdomain [{d.a}, {d.b}): error = {err:.2f} epsilon")

Creating Custom Functions

Create a Python module with:

from flint import arb

# Domain [a, b)
my_domain: tuple[float, float] = (0.5, 12.0)

def my_arb_f(X: arb, prec: int) -> arb:
    """Evaluate f(x) with given precision."""
    # Your implementation here
    return result

# Test cases: (x, expected_value, tolerance)
my_testcases: list[tuple[float, float, float]] = [
    (1.0, 0.123456, 1e-5),
]

Then use it:

ppapp s path/to/my_function.py 5 10 > output.c

Modes

Mode Command Description
v ppapp v <f> <x> Compute f(x)
n ppapp n <f> <M> <Nmax> <E> Find minimal N for error ≤ E
e ppapp e <f> <M> <N> Compute error bound
c ppapp c <f> <M> <N> [E] Print Chebyshev coefficients
p ppapp p <f> <M> <N> [E] Print monomial coefficients
s ppapp s <f> <M> <N> [E] Generate C source code
t ppapp t <f> <M> <Nxo> <E> Generate test cases

Documentation

The user manual is included in the package:

from ppapp import get_user_manual_path
print(get_user_manual_path())  # Path to userManual.pdf

License

GNU General Public License v3 or later (GPLv3+)

Authors

  • Joachim Wuttke (Forschungszentrum Jülich GmbH)
  • Alexander Kleinsorge (Technische Hochschule Wildau)

Links

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

ppapp-1.0.0.tar.gz (188.2 kB view details)

Uploaded Source

Built Distribution

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

ppapp-1.0.0-py3-none-any.whl (189.2 kB view details)

Uploaded Python 3

File details

Details for the file ppapp-1.0.0.tar.gz.

File metadata

  • Download URL: ppapp-1.0.0.tar.gz
  • Upload date:
  • Size: 188.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for ppapp-1.0.0.tar.gz
Algorithm Hash digest
SHA256 86de539eef3e711d239724bf66fcea9453ff105c812da72a0289d59561147aba
MD5 1c3466b91851b6cf99175edf1551af43
BLAKE2b-256 d6af5d53707a458b4569776c618843761cb9283638383597b79c541b0b657cff

See more details on using hashes here.

File details

Details for the file ppapp-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ppapp-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 189.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for ppapp-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6f00c4e5fb898d8c3905525e1832409bdd2b495232821e92da47e9d2feff6ee
MD5 800b78381fe42f63fef8845d3977d7c8
BLAKE2b-256 b37a3700e7fbd0388bc54ddf0cb991de1fccc0289f813ace42490efc5aaf1d69

See more details on using hashes here.

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