Skip to main content

Python wrapper for C++ numerical algorithms using CFFI for monotone splines

Project description

monspline


Python package for efficient spline interpolation using C++ (via CFFI),

with a strong focus on shape preservation.


Overview


This project implements and evaluates spline interpolation methods,

including:

  • Cubic spline interpolation

  • Monotone-preserving spline interpolation

  • Taut exponential spline interpolation

  • Tensor-product splines (multivariate, monotone)


Motivation


Spline interpolation is widely used in numerical analysis and machine

learning. However:

  • Cubic splines may introduce oscillations

  • Monotone splines improve stability

  • Real-world datasets often contain noise and discontinuities


[Methods and examples](https://gleb.cgps.ch/Assets/Documents/Shape_preserving_approximation_using_least_squares_splines.pdf, https://gleb.cgps.ch/Assets/Documents/Monotone_approximation_of_aggregation_operators_using_least_squares_splines.pdf)

Architecture


Python - CFFI - C wrapper - C++ classes

  • Python: User API and plotting

  • CFFI: Interface layer

  • C wrapper: Object management using handles

  • C++: Core spline algorithms


Features


Cubic Spline


  • Smooth interpolation (C^2 continuity)

  • Efficient C++ implementation

Monotone Spline


  • Preserves monotonicity

  • Prevents overshooting

  • Better for noisy / real-world data

  • Taut spline remove unnecessary inflection points

Tensor (Multivariate) Spline


  • Supports higher-dimensional approximation

  • Used for 3D surface modelling

  • Support monotonicity preservation


Build


python build.py

 Run Tests

python tests/testcs.py

python tests/test_tensor_spline.py

Observation: Cubic splines introduce oscillations, while monotone

splines preserve structure.

Observation: Cubic splines overshoot near discontinuities. Monotone

splines provide stable approximation.

3D Tensor Spline

Observation: Tensor splines approximate smooth surfaces effectively in

2D.

Key Insights


Cubic splines - smooth but unstable near discontinuities

Monotone splines - stable and shape-preserving

Taut splines - remove extraneous inflection points, no monotonicity

assumed

Tensor splines - extend interpolation to higher dimensions

References


  1. de Boor, A Practical Guide to Splines

Fritsch & Carlson, Monotone Piecewise Cubic Interpolation

G.Beliakov, (2000) Shape preserving approximation using least squares

splines. Analysis in theory and applications, 16(4), 80-98

G.Beliakov, (2002) Monotone approximation of aggregation operators using

least squares splines. International journal of uncertainty, fuzziness,

and knowledge-based systems, 10(6), 659-676.

Installation


To install type:

$ pip install monspline

Usage of monspline


from monspline import TensorSpline

from monspline import CubicSpline

 generate/read data tables

x = [0,1,2,3,4,5]

y = [0,0.1,0.2,0.3,0.4,0.5]

s = CubicSpline(x, y, type=1)

y=s.value(0.5)

s1= = TensorSpline(kind=0, dim=2, knots=knots, data=data, exactdata=exactdata)

y = s1.value([0.5, 0.5])

Usage of CubicSpline(x,y type, tau=0)


Construction of a cubic/monotone/taut univariate spline

Parameters


Input parameters:


x[]: NumPy array of size n, float

w[]: NumPy array of size n, float

type: int, can be 1 (cubic),2 (taut exponential, required tau>0.01) , 3

monotone increasing

tau: float, tension parameter in taut splines to straighten inflection

points

Output parameters


s: spline object

Usage of value(x)


Calculation of a cubic/monotone/taut spline value

Parameters


Input parameters:


t: float, the query point

Output


y: float, spline value at t

Usage of TensorSpline(kind, dim, knots, data, exactdata)


Construction of a tensor product spline of dimension dim

Parameters


Input parameters:


kind: int, type of spline, see below

dim: int, dimension

knots: array of size(dim,knotN), float, contains spline knots for each

variable

data[]: NumPy array of size (n,dim+1), float, contains n data (x and y)

to approximate

exactdata[]: NumPy array of size (m,dim+1), float, contains m data (x

and y) to interpolate (exact fit)

Output parameters


s: spline object

Interpretation of kind


For each variable: 0: dim-dimensional tensor product spline of order 2

(linear), no constraints

1: monotone increasing

2: monotone decreasing

When dim >1, concatenate the values for each variable starting from the

right.

Example: dim=2

12: means increasing in the second variable (1) and decreasing in the

first variable (2)

dim=3

110: increasing in the third and second variable unrestricted in the

first variable

When dim==1:

kind=11,12,… means spline of order 3,4,… (order 4 means cubic spline),

i.e., kind-8 = spline order otherwise order is forced to be 2

Usage of value(x)


Calculation of tensor spline value at x

Parameters


Input parameters:


x: float, when dim==1 and array of size dim otherwise, the query point

Output


y: float, spline value at x

Usage of value_der(x,var)


Calculation of tensor spline derivative value at x

Parameters


Input parameters:


x: float, when dim==1 and array of size dim otherwise, the query point

var: int, with respect to which variable, starting from 0 to dim-1

Output


y: float, spline partial derivative value at x

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

monspline-0.1.3.tar.gz (3.8 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

monspline-0.1.3-cp314-cp314-macosx_14_0_arm64.whl (114.7 kB view details)

Uploaded CPython 3.14macOS 14.0+ ARM64

monspline-0.1.3-cp313-cp313-win_amd64.whl (84.7 kB view details)

Uploaded CPython 3.13Windows x86-64

File details

Details for the file monspline-0.1.3.tar.gz.

File metadata

  • Download URL: monspline-0.1.3.tar.gz
  • Upload date:
  • Size: 3.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for monspline-0.1.3.tar.gz
Algorithm Hash digest
SHA256 39ecc882edd756af50fc8ec99d6b034cb0883aa126037bc0bd9c62dbce791fb2
MD5 4a622a42362e95db37f85943ad7af701
BLAKE2b-256 2a95c764915f2ade8bf67150556095d4c16e68da53249f3cd4a86a6d588265d9

See more details on using hashes here.

File details

Details for the file monspline-0.1.3-cp314-cp314-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for monspline-0.1.3-cp314-cp314-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 7de348c3efaeccd80d694b35e1678cde552a1f115d22e5bea30986edad1b1eaa
MD5 6baf259d72ad77a970662f24ad7680fe
BLAKE2b-256 a5caf632228680bcef1ed7e44fada4fe5d37b87d22aac9114d13248a86ef0151

See more details on using hashes here.

File details

Details for the file monspline-0.1.3-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: monspline-0.1.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 84.7 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for monspline-0.1.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1142e3f348d24beb13ea857a58ca457a423c22158cfccc3369bfb782d9197d3c
MD5 9d3ebebec597f874b57567856dcb8ee1
BLAKE2b-256 23a9b63a8ebe3063708521fab7f585cade46319774af5510d0453594286afa63

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