Skip to main content

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

Release files for monspline 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for monspline 0.1.3
File Size Uploaded
monspline-0.1.3.tar.gz 3.8 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for monspline 0.1.3
File Interpreter ABI Platform
monspline-0.1.3-cp314-cp314-macosx_14_0_arm64.whl CPython 3.14 CPython 3.14 macOS 14.0+ ARM64 Details
monspline-0.1.3-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details

Total release size: 4.0 MB

Release files / monspline-0.1.3.tar.gz

Download URL monspline-0.1.3.tar.gz
Size 3.8 MB
Tags Source
SHA-256 checksum
How to use checksums
39ecc882edd756af50fc8ec99d6b034cb0883aa126037bc0bd9c62dbce791fb2
BLAKE2b-256 checksum
How to use checksums
2a95c764915f2ade8bf67150556095d4c16e68da53249f3cd4a86a6d588265d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release files / monspline-0.1.3-cp314-cp314-macosx_14_0_arm64.whl

Download URL monspline-0.1.3-cp314-cp314-macosx_14_0_arm64.whl
Size 114.7 kB
Tags CPython 3.14 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
7de348c3efaeccd80d694b35e1678cde552a1f115d22e5bea30986edad1b1eaa
BLAKE2b-256 checksum
How to use checksums
a5caf632228680bcef1ed7e44fada4fe5d37b87d22aac9114d13248a86ef0151
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release files / monspline-0.1.3-cp313-cp313-win_amd64.whl

Download URL monspline-0.1.3-cp313-cp313-win_amd64.whl
Size 84.7 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
1142e3f348d24beb13ea857a58ca457a423c22158cfccc3369bfb782d9197d3c
BLAKE2b-256 checksum
How to use checksums
23a9b63a8ebe3063708521fab7f585cade46319774af5510d0453594286afa63
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release history Release notifications | RSS feed

This release

0.1.3 This release

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page