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

Uploaded Python 3

File details

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

File metadata

  • Download URL: ppapp-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 383aced2af3bcac53eef5cb0dfb58f1756e688e8fb5e0531d3d1a139a1763b23
MD5 34483340a1e1fc8314ff2ded524af06f
BLAKE2b-256 164bf6d017f01914c27db8bbf53e14a91fe46380fcd062c55e973ce57f5fc3a4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ppapp-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6747a06c4ffd7ab85879e2327f2169e4986bce7aa74feb979e562a3bfd9abc30
MD5 6ab87b7e39e27da0958b37b563bc74ae
BLAKE2b-256 570a9436e2130ad64205405d49002b346a707020211458c571cf7f8cfa1784d9

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