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.2.tar.gz (188.4 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.2-py3-none-any.whl (189.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ppapp-1.0.2.tar.gz
  • Upload date:
  • Size: 188.4 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.2.tar.gz
Algorithm Hash digest
SHA256 c09f415295ef506942b1825602a16347bfec75b4a14e5e9da7e506a389330ccd
MD5 7001254892b78422161ebe9e8172f34c
BLAKE2b-256 289d6b0eba6024f8ebf77bee67a3351e230d41c2ff22d2a640edfdaab09c1051

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ppapp-1.0.2-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.12.4

File hashes

Hashes for ppapp-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ae7737d8587d4cdfd60fc7c1fd06a623b21071ba6d50c3da893045c4d776ca7a
MD5 06b81f9b7eff963043af14d657c539cb
BLAKE2b-256 159ab36b5759a8fe6dfe0b5b91f2b65473eb2cab55cd360d094e677d80b0b072

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