Skip to main content

B-spline curve fitting — pure-Rust Dierckx engine with Python bindings

Project description

splinefit

B-spline curve fitting for Python, powered by a native Rust engine. Fit smoothing, interpolating, or cardinal cubic splines to data and evaluate, integrate, or find roots -- with the same numerical accuracy as SciPy's FITPACK, at native speed.

Built from a pure-Rust translation of Paul Dierckx' classic FITPACK library (the same Fortran engine behind SciPy's splrep/splev).

Installation

pip install splinefit

Requires Python >= 3.9 and NumPy.

Quick start

import numpy as np
from splinefit import CubicSpline

# Sample data: sin(x) on [0, 2pi]
x = np.linspace(0, 2 * np.pi, 50)
y = np.sin(x)

# Fit a smoothing spline (rms = 0.05)
spline = CubicSpline.smoothing(x, y, rms=0.05)
print(spline)  # CubicSpline(num_knots=11, domain=[0.000000, 6.283185])

# Evaluate at 200 points
x_new = np.linspace(0, 2 * np.pi, 200)
y_fit = spline.evaluate(x_new)

API

Constructors

All constructors take NumPy arrays (or array-like inputs) for x and y. x must be strictly increasing with at least 4 points.

CubicSpline.smoothing(x, y, rms)

Fit a smoothing spline. rms is the target root-mean-square residual in the same units as y. Smaller values produce more knots and a tighter fit.

spline = CubicSpline.smoothing(x, y, rms=0.05)

CubicSpline.interpolating(x, y)

Fit an interpolating spline that passes exactly through every data point.

spline = CubicSpline.interpolating(x, y)

CubicSpline.cardinal(x, y, dt)

Fit a spline on a fixed equidistant knot grid with spacing dt.

spline = CubicSpline.cardinal(x, y, dt=0.5)

Methods

spline.evaluate(x) -> numpy.ndarray

Evaluate the spline at each point in x.

y_fit = spline.evaluate(np.array([0.5, 1.0, 1.5]))

spline.integral(a, b) -> float

Compute the definite integral of the spline over [a, b].

area = spline.integral(0, np.pi)  # integral of sin(x) from 0 to pi ~ 2.0

spline.roots() -> numpy.ndarray

Find all interior zeros of the spline, returned in ascending order. Zeros at the domain boundaries may not be included.

zeros = spline.roots()  # e.g. array([3.14159...])

spline.knots() -> numpy.ndarray

Return the knot vector.

spline.coefficients() -> numpy.ndarray

Return the B-spline coefficients.

spline.num_knots -> int

Number of knots (property).

String representation

>>> spline
CubicSpline(num_knots=11, domain=[0.000000, 6.283185])

Comparison with SciPy

splinefit uses the same FITPACK algorithms as SciPy but compiled as a native Rust extension rather than wrapping Fortran via f2py. The API is simpler:

splinefit SciPy equivalent
CubicSpline.smoothing(x, y, rms) splrep(x, y, s=m*rms**2)
CubicSpline.interpolating(x, y) splrep(x, y, s=0)
spline.evaluate(x) splev(x, tck)
spline.integral(a, b) splint(a, b, tck)
spline.roots() sproot(tck)

How it works

This package is compiled from the Rust crate splinefit using PyO3 and maturin. It runs the same numerical algorithms as SciPy's FITPACK, translated line-by-line from Paul Dierckx' original Fortran into Rust.

License

Apache-2.0 OR MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

splinefit-0.4.1-cp313-cp313-macosx_11_0_arm64.whl (336.9 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

File details

Details for the file splinefit-0.4.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for splinefit-0.4.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 624f44730b18ec73adfac6e310d7b3766c8ea837e32096552b8db03ca6c053c4
MD5 7d0554303644a7acce678998a89d5cc2
BLAKE2b-256 59ad0043dfeff0ae1bf4be616c71f4b6d485f008be3536e81edab1fea2b61fa5

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