chiku is an open-source Python library for efficient polynomial function approximation. It takes an arbitrary continuous function and returns the coefficients of a polynomial that approximates it, using a unified API across seven different methods. It is available on PyPI and is particularly useful for evaluating non-linear functions (such as sigmoid or tanh) under Fully Homomorphic Encryption, where only additions and multiplications are available and functions must be replaced by polynomials.
Installation
pip install chiku
To enable the optional TensorFlow-based ANN approximator (TensorFlow currently needs Python 3.11):
pip install chiku[ann]
What it does
Complex non-linear functions can be approximated by polynomials so they can be computed in restricted settings such as encrypted (FHE) domains. Deterministic methods like Taylor, Pade, Chebyshev, Remez, and Fourier always yield the same polynomial for the same function. chiku adds two data-driven approaches on top: an Artificial Neural Network (ANN) approximator that derives the polynomial probabilistically from the network's weights, and linear-regression (LR) fitting in the monomial or Chebyshev basis. Unlike libraries such as mpmath, chiku needs no datatype conversion and works directly on ordinary Python callables.
Every approximator shares the same interface: construct with (f, degree, frange), with fitting done in the constructor, then read coefficients with get_coeffs() or evaluate with predict().
Quick start
import numpy as np
from chiku import taylor, pade, chebyshev, fourier, remez, sk_lr
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Taylor expansion at the midpoint of frange
t = taylor.taylor(sigmoid, degree=5, frange=(-1, 1))
print(t.get_coeffs())
# Pade rational approximation, built directly from f
p = pade.pade(sigmoid, pd=3, qd=3, frange=(-1, 1))
# Chebyshev series
c = chebyshev.chebyshev(sigmoid, degree=8, frange=(-1, 1))
# Truncated Fourier series
f = fourier.fourier(lambda x: x*x, degree=70, frange=(-1, 5))
# Iterative Remez minimax
r = remez.RemezSolver(sigmoid, degree=4, frange=(-1, 1))
print(r.get_coeffs(), r.get_error())
# Polynomial regression
m = sk_lr.sk_lr(sigmoid, degree=[1, 2, 3, 4, 5], frange=(-1, 1))
For arbitrary-precision work, the chiku.mpmath subpackage provides mpTaylor, mpRemez, and others that operate directly on mpmath callables.
How chiku compares
Where numpy offers Chebyshev fitting and scipy and mpmath offer a subset of Taylor, Pade, Fourier, and Chebyshev, chiku covers all of those plus Remez minimax, the ANN approximator, and linear-regression fitting, behind one consistent API and with no datatype juggling.
Links
- Source: github.com/devharsh/chiku
- Package: pypi.org/project/chiku
- License: AGPL-3.0-or-later
If you use chiku in academic work, citation entries are provided in the project's README.
