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.3.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.3-py3-none-any.whl (189.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ppapp-1.0.3.tar.gz
Algorithm Hash digest
SHA256 d0094e33d241dfd76bfa81176240cd8b9c83b99ee35a6828b9b1d23c5f907081
MD5 a9617e10538a2389f268a0b83213dc7c
BLAKE2b-256 bf80f1076022fd19aeaddfed8db608c6c796a0af0c612998fd06aaf6c025540d

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ppapp-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3b7c967b29f8c1f46b399d6a591de977a32c7545dc7fe7be08b64a8902191190
MD5 7df48b4082d9237d38c2e02dd625a229
BLAKE2b-256 02d4f40601fdb7a57c8240abd2af7ef2232b98212d65ff57755edd3193b7485c

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