Metadata-Version: 2.4
Name: ppapp
Version: 1.0.1
Summary: Piecewise polynomial approximation: code generator for Chebyshev approximation
Author-email: Joachim Wuttke <j.wuttke@fz-juelich.de>, Alexander Kleinsorge <alkl9873@th-wildau.de>
License: GPL-3.0-or-later
Project-URL: Repository, https://jugit.fz-juelich.de/mlz/ppapp
Project-URL: Documentation, https://jugit.fz-juelich.de/mlz/ppapp/-/blob/main/py/R/userManual/userManual.pdf
Keywords: chebyshev,approximation,polynomial,code-generation,numerical
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: python-flint>=0.4.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: ruff>=0.1.0; extra == "dev"
Requires-Dist: mypy>=1.0; extra == "dev"
Dynamic: license-file

# 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

```bash
pip install ppapp
```

### Requirements

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

## Quick Start

### Command Line

```bash
# 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:

```bash
# 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

```python
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:

```python
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:

```bash
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:

```python
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

- [Repository](https://jugit.fz-juelich.de/mlz/ppapp)
- [Documentation (PDF)](https://jugit.fz-juelich.de/mlz/ppapp/-/blob/main/py/R/userManual/userManual.pdf)
