Skip to content

wrochira/function-tabulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Function Tabulator

A small library for building piecewise‑linear approximations to smooth functions.

AboutKey FeaturesQuick StartLearn More

About

function_tabulator provides a thin, well‑tested wrapper around pwlf and numpy for turning smooth mathematical functions into compact piecewise‑linear approximations ("tabulations"). You give it a Python callable, a domain, and a number of segments; it gives you breakpoints, intercepts, gradients, and a fast predict() method that behaves like your original function but is cheap to evaluate and easy to embed into other systems (e.g. hardware, simulators, or lookup tables).

The library grew out of work on hybrid analog–digital computing, where functions often need to be reduced to simple linear segments before being implemented in hardware or other constrained environments.

Key Features

  • Flexible breakpoint placement – Choose between:
    • regular: evenly‑spaced breakpoints across the domain
    • analytical: curvature‑aware placement based on the second derivative
    • minimise: breakpoints found by numerical optimisation
  • Multiple intercept strategies – Either sample the true function directly (direct) or minimise error numerically (minimise).
  • Interpolation control – Use true piecewise‑linear segments (interpolate=True) or treat each segment as a flat bin (interpolate=False).
  • Caching of fits – Expensive optimisation results are cached on disk in a per‑user cache directory so repeated fits with identical parameters are effectively free.
  • Numpy‑friendly API – Works cleanly with numpy arrays and ArrayLike inputs, and returns scalar‑like outputs for scalar inputs.
  • Well‑tested – Includes a reasonably thorough test suite covering different function families, configuration combinations, caching behaviour, and edge cases.

Quick Start

Install the dependencies:

pip install numpy scipy pwlf platformdirs pytest matplotlib

Create and fit a simple tabulation:

import numpy as np
from function_tabulator import FunctionTabulator


def cubic(x: np.ndarray, a: float) -> np.ndarray:
    return a * x**3


tab = FunctionTabulator(
    function=cubic,
    domain=(0.0, 1.0),
    n_segments=8,
    function_params={'a': 2.0},
    interpolate=True,
    breakpoint_method='regular',
    intercept_method='minimise',
)
tab.fit(resolution=1_000)

x = np.linspace(0.0, 1.0, 1_000)
y_true = cubic(x, a=2.0)
y_approx = tab.predict(x)

For a slightly more complete example (including plotting and an error check), see example.py:

python example.py

Learn More

For a detailed overview of breakpoint strategies, intercept options, caching, and the full API, see DOCUMENTATION.md.

Status & License

This library is small but stable and is not under active development, aside from occasional refinements.

This project is licensed under the CC BY-NC-SA 4.0 Licence. You can freely use, modify, and share this work for non-commercial purposes. You must give appropriate credit. Any modifications must be shared under the same licence. You cannot use this work for commercial purposes without permission.

CC BY-NC-SA 4.0

About

A small library for building piecewise‑linear approximations to smooth functions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages