Installation
Install ds_utils directly from the repository using pip. No external dependencies are required beyond the standard data science stack.
$pip install git+https://github.com/your-org/ds_utils.git
Each module can be imported independently once the package is installed:
from num_analysis import interpolate_curve, smooth_curve, lagrange, aaa
from num_analysis import floater_hormann, chebyshev_pts, plot_lebesgue
Dependencies: numpy, scipy, matplotlib. All are available via pip and conda.
Numerical Analysis
The Numerical Analysis module provides algorithms for function approximation, rational interpolation, and parametric curve fitting — with a focus on numerical stability and applicability to real-world geometric data such as transport network maps.
Barycentric interpolation in Lagrange and Floater-Hormann forms, the AAA greedy rational algorithm, Chebyshev node generation, and Lebesgue function visualisation. All interpolants are returned as callable BarycentricRational objects that can be evaluated at arbitrary points.
lagrange(xinter, fnc)Constructs the Lagrange interpolating polynomial through the given nodes in barycentric form.
The barycentric weight for node xᵢ is wᵢ = 1 / ∏_{j≠i}(xᵢ − xⱼ), giving the numerically
stable evaluation r(x) = (Σ wᵢ fᵢ/(x−xᵢ)) / (Σ wᵢ/(x−xᵢ)) that avoids explicit polynomial
construction and catastrophic cancellation.
from num_analysis import lagrange
| Parameter | Type | Description |
|---|
xinter | array-like | Interpolation nodes x₀, …, xₙ |
fnc | callable or array-like | Function f or pre-computed values f(xᵢ) |
Returns: BarycentricRational — callable r(x) that interpolates f at every node.
import numpy as np
from num_analysis import lagrange, chebyshev_pts
x = chebyshev_pts(10) # 10 Chebyshev nodes on [-1, 1]
r = lagrange(x, np.sin) # build interpolant of sin
print(r(0.5)) # evaluate at x = 0.5
floater_hormann(d, xinter, fnc)Floater-Hormann rational interpolant with blending parameter d (window size).
Unlike polynomial Lagrange interpolation, the rational form does not suffer exponential
Lebesgue-constant growth for equidistant nodes. The weights are
wᵢ = (-1)^(i-d) Σ_{k∈Jᵢ} ∏_{j=k,j≠i}^{k+d} 1/|xᵢ−xⱼ|
where Jᵢ = max(0, i-d) … min(i, n-d). Setting d = 0 recovers the nearest-neighbour
step function; d = n recovers Lagrange.
from num_analysis import floater_hormann
| Parameter | Type | Description |
|---|
d | int | Blending parameter 0 ≤ d ≤ n; higher d → closer to Lagrange |
xinter | array-like | Interpolation nodes |
fnc | callable or array-like | Function or pre-computed values |
Returns: BarycentricRational — rational interpolant in barycentric form.
from num_analysis import floater_hormann
import numpy as np
x = np.linspace(-1, 1, 20) # equidistant nodes
r = floater_hormann(3, x, np.abs) # d=3 blending
print(r(0.0))
aaa(xinter, fnc, tol=1e-13, mmax=100, return_errors=False)Adaptive Antoulas-Anderson greedy rational approximation.
At each step the algorithm selects the sample point with largest residual
|f(x) − r(x)| as the next support point, then solves a 1×1 least-squares
problem via SVD to find the optimal weight. Convergence is superlinear for
meromorphic functions and the algorithm terminates as soon as the ℓ∞ residual
falls below tol × ‖f‖∞. The result is a near-minimax rational approximant
requiring far fewer nodes than polynomial interpolation for the same accuracy.
from num_analysis import aaa
| Parameter | Type | Description |
|---|
xinter | array-like | Dense sample grid (not the interpolation nodes) |
fnc | callable or array-like | Function or values to approximate |
tol | float | Relative ℓ∞ convergence tolerance (default 1e-13) |
mmax | int | Maximum number of support points (default 100) |
return_errors | bool | If True, also return list of per-step ℓ∞ errors |
Returns: BarycentricRational, or (BarycentricRational, list[float]) if return_errors=True.
from num_analysis import aaa
import numpy as np
x = np.linspace(-1, 1, 500)
r, errs = aaa(x, np.exp, tol=1e-10, return_errors=True)
print(f"Support points: {len(r.nodes)}, final error: {errs[-1]:.2e}") chebyshev_pts(n)Returns the n Chebyshev nodes of the first kind on [−1, 1]:
xₖ = cos(k π / (n−1)) for k = 0, 1, …, n−1.
Using Chebyshev nodes minimises the Runge oscillation that afflicts polynomial
interpolation on equidistant grids. The Lebesgue constant grows only as O(log n)
for Chebyshev nodes, compared to O(2ⁿ / (n log n)) for equidistant nodes.
from num_analysis import chebyshev_pts
| Parameter | Type | Description |
|---|
n | int | Number of Chebyshev nodes |
Returns: numpy.ndarray of shape (n,) with values in [−1, 1].
from num_analysis import chebyshev_pts, lagrange
import numpy as np
x_cheb = chebyshev_pts(15)
r = lagrange(x_cheb, lambda x: 1/(1 + 25*x**2)) # Runge function
print("Interpolant at 0:", r(0.0)) plot_lebesgue(xinter)Plots the Lebesgue function Λ(x) = Σₖ |Lₖ(x)| where Lₖ are the
Lagrange basis polynomials for the given node set. The Lebesgue constant
max Λ(x) bounds the interpolation error amplification: ‖f − r‖ ≤ (1 + Λ) ‖f − p*‖
where p* is the best polynomial approximation. Visualising Λ(x) reveals which
parts of the interval are most sensitive to function-value perturbations.
from num_analysis import plot_lebesgue, chebyshev_pts
| Parameter | Type | Description |
|---|
xinter | array-like | Interpolation nodes whose Lebesgue function to plot |
Returns: None — displays a matplotlib figure.
Low-level B-spline primitives: Cox-de Boor basis evaluation and the 2-D LSPIA iteration step. These building blocks are used internally by curve_splines.py and can also be used standalone to implement custom approximation schemes.
High-level N-dimensional parametric curve library. Provides both exact interpolation through all anchor points (cubic spline) and smooth approximation with controllable detail (LSPIA / least-squares B-spline). Used to generate the smoothed tube lines in the London Transport map.
Both functions share a chord-length parametrisation step: each input point is assigned a parameter tᵢ proportional to its cumulative Euclidean distance from the first point, mapped to [0, 1]. This ensures the spline advances at a rate consistent with the actual spatial density of the data, avoiding bunching at clusters of nearby points.
Patterns Mining coming soon
Frequent itemset mining, sequence pattern discovery, and graph-based pattern extraction. Documentation will be available once the module reaches stable release.
Linguistic Processing coming soon
NLP utilities: tokenisation, n-gram models, semantic similarity, and text vectorisation wrappers. Documentation in progress.
Learning Algorithms coming soon
Supervised and unsupervised learning implementations with emphasis on interpretability. Documentation in progress.
Model Tuning coming soon
Hyperparameter optimisation utilities including grid search, Bayesian optimisation, and cross-validation helpers. Documentation in progress.
Assessment & Metrics coming soon
Evaluation metrics beyond standard sklearn: disparity indices, GloSS, and domain-specific transport network KPIs. Documentation in progress.