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 Distribution

splinefit-0.4.0.tar.gz (75.2 kB view details)

Uploaded Source

Built Distribution

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

splinefit-0.4.0-cp313-cp313-macosx_11_0_arm64.whl (297.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

File details

Details for the file splinefit-0.4.0.tar.gz.

File metadata

  • Download URL: splinefit-0.4.0.tar.gz
  • Upload date:
  • Size: 75.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for splinefit-0.4.0.tar.gz
Algorithm Hash digest
SHA256 52e47fe755d93b1109d5629e3e313059bab52b249690f939471b7cb495b18539
MD5 19205e68a6c2f685255f8c9a13905b53
BLAKE2b-256 c0498900b6f707ebfac19a1535c02ab165bd3740ad630deed540bb88e1f2f63b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for splinefit-0.4.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 86a5b377029cb75592fdda73a2c2b5fb4ea5e362091774feb07ed0d2dc659c89
MD5 84c67ee84abe81c106edc6839e15788c
BLAKE2b-256 80f52f1ca5460b0cb8963bb9eb9977261c4b49b1c30402c5694d470e7a9edc3e

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