Cubic Spline Examples¶
Example Code¶
Representative script:
"""IMSL CSINT example with CSVAL/CS1GD mappings and plot."""
from __future__ import annotations
from pathlib import Path
from typing import Any, Dict
import sys
import matplotlib.pyplot as plt
import numpy as np
REPO_ROOT = Path(__file__).resolve().parent.parent
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))
from interpolation import (
cubic_spline_evaluate,
cubic_spline_evaluate_grid,
cubic_spline_not_a_knot,
)
def run_demo_imsl_csint() -> Dict[str, Any]:
"""Run CSINT website example and return parity data."""
ndata = 11
x_data = np.linspace(0.0, 1.0, ndata)
f_data = np.sin(15.0 * x_data)
pp = cubic_spline_not_a_knot(x_data, f_data)
x_vec = np.linspace(0.0, 1.0, 2 * ndata - 1)
values = cubic_spline_evaluate_grid(pp, x_vec)
exact = np.sin(15.0 * x_vec)
error = exact - values
csval_point = float(cubic_spline_evaluate(pp, 0.55))
cs1gd_values = values.copy()
out_dir = Path("test_output")
out_dir.mkdir(parents=True, exist_ok=True)
out_path = out_dir / "demo_imsl_csint_interpolant.svg"
x_dense = np.linspace(0.0, 1.0, 500)
y_dense = cubic_spline_evaluate_grid(pp, x_dense)
y_true_dense = np.sin(15.0 * x_dense)
fig, axes = plt.subplots(2, 1, figsize=(9, 7), sharex=True)
axes[0].plot(x_dense, y_true_dense, "k--", linewidth=1.2, label="Exact sin(15x)")
axes[0].plot(x_dense, y_dense, color="#1f77b4", linewidth=2.0, label="CSINT spline")
axes[0].scatter(x_data, f_data, color="#d62728", s=24, zorder=3)
axes[0].set_ylabel("f(x)")
axes[0].set_title("IMSL CSINT / CSVAL / CS1GD")
axes[0].grid(True, alpha=0.3)
axes[0].legend(loc="best")
axes[1].plot(x_vec, error, color="#ff7f0e", marker="o", markersize=3.2, linewidth=1.1)
axes[1].axhline(0.0, color="black", linewidth=0.9)
axes[1].set_xlabel("x")
axes[1].set_ylabel("Error")
axes[1].grid(True, alpha=0.3)
fig.tight_layout()
fig.savefig(out_path, format="svg")
plt.close(fig)
return {
"x_vec": x_vec,
"interpolant": values,
"exact": exact,
"error": error,
"csval_at_055": csval_point,
"cs1gd_values": cs1gd_values,
"plot_path": str(out_path),
}
if __name__ == "__main__":
run_demo_imsl_csint()
Input (Console)¶
Run the cubic-spline family scripts from the package root:
python examples/example_imsl_csakm.py
python examples/example_imsl_cscon.py
python examples/example_imsl_csder.py
python examples/example_imsl_csitg.py
python examples/example_imsl_csper.py
python examples/example_imsl_csher.py
python examples/example_imsl_csdec.py
python examples/example_imsl_csiez.py
python examples/example_imsl_csint.py
Plot Output¶
Generated SVG plots:
Output Console¶
Summary console output:
script input setup representative numeric outputs
example_imsl_csakm.py 11 samples on [0,1] interpolant head = [0, 0.8176270179768701, 0.9974949866040544]; max abs error = 0.1634867058267827
example_imsl_cscon.py 9 samples, 600 query points constrained head = [0, 0.0221059625971212, 0.04417637248313012]; standard = [0, 0.02200175027382867, 0.04376660042753504]
example_imsl_csder.py 10 samples, 20 eval points value head = [0, 0.7434266905889403, 0.9974015968324504]; d1 = [18.77489907247145, 9.47531516990828, 0.1757312673451068]; max err = 0.1367715836928539
example_imsl_csitg.py intervals = [[0.0, 0.5], [0.0, 2.0]] computed = [0.04166666666666667, 2.666666666666667]; exact = [0.04166666666666666, 2.666666666666667]
example_imsl_csper.py 21 periodic points interpolant head = [0, 0.3079247504143486, 0.5877852522924731]; max abs error = 0.003534575704511433
example_imsl_csher.py 11 Hermite nodes interpolant head = [0, 0.672984267989333, 0.9974949866040544]; max abs error = 0.01261585974675983
example_imsl_csdec.py 11 decomposition nodes interpolant head = [0, 0.6753071884832167, 0.9974949866040544]; max abs error = 0.02181067268567627
example_imsl_csiez.py 11 easy-interface samples interpolant head = [0, 0.7304812389455574, 0.9974949866040544]; max abs error = 0.09516293416486565
example_imsl_csint.py 11 interpolant samples interpolant head = [0, 0.7304812389455574, 0.9974949866040544]; CSVAL(0.55) = 0.8318860885199404; max err = 0.09516293416486565
Detailed integration result tables:
CSAKM parity output (full parameter rows)
x interpolant exact error
0 0 0 0
0.05 0.8176270179768701 0.6816387600233341 0.135988257953536
0.1 0.9974949866040544 0.9974949866040544 0
0.15 0.6145864910611383 0.778073196887921 -0.1634867058267827
0.2 0.1411200080598672 0.1411200080598672 0
0.25 -0.4781855199918669 -0.5715613187423437 0.09337579875047686
0.3 -0.9775301176650972 -0.9775301176650972 0
0.35 -0.8124875355610804 -0.8589344934265916 0.04644695786551112
0.4 -0.2794154981989259 -0.2794154981989259 0
0.45 0.3855534935682086 0.4500440737806176 -0.06449058021240905
0.5 0.9379999767747389 0.9379999767747389 0
0.55 0.8543298794768504 0.9226042102393402 -0.0682743307624899
0.6000000000000001 0.4121184852417549 0.4121184852417549 0
0.65 -0.2762319176794184 -0.3195191936222737 0.04328727594285525
0.7000000000000001 -0.8796957599716709 -0.8796957599716709 0
0.75 -0.888860502909416 -0.9678079975112615 0.07894749460184547
0.8 -0.5365729180004349 -0.5365729180004349 0
0.8500000000000001 0.1488416474475482 0.1825991346311358 -0.03375748718358751
0.9 0.803784426551621 0.803784426551621 0
0.9500000000000001 0.9323811462163794 0.9936411011327624 -0.06125995491638303
1 0.6502878401571169 0.6502878401571168 1.110223024625157e-16
CSINT parity output (full parameter rows)
x interpolant exact error
0 0 0 0
0.05 0.7304812389455574 0.6816387600233341 -0.04884247892222326
0.1 0.9974949866040544 0.9974949866040544 0
0.15 0.7015665668525244 0.778073196887921 0.07650663003539659
0.2 0.1411200080598672 0.1411200080598672 0
0.25 -0.5153606546782621 -0.5715613187423437 -0.05620066406408164
0.3 -0.9775301176650972 -0.9775301176650972 0
0.35 -0.7744769079756757 -0.8589344934265916 -0.08445758545091586
0.4 -0.2794154981989259 -0.2794154981989259 0
0.45 0.4057919962253537 0.4500440737806176 0.04425207755526395
0.5 0.9379999767747389 0.9379999767747389 0
0.55 0.8318860885199404 0.9226042102393402 0.09071812171939986
0.6000000000000001 0.4121184852417549 0.4121184852417549 0
0.65 -0.2881014082089707 -0.3195191936222737 -0.03141778541330292
0.7000000000000001 -0.8796957599716709 -0.8796957599716709 0
0.75 -0.8726450633463958 -0.9678079975112615 -0.09516293416486565
0.8 -0.5365729180004349 -0.5365729180004349 0
0.8500000000000001 0.1646444685484527 0.1825991346311358 0.01795466608268304
0.9 0.803784426551621 0.803784426551621 0
0.9500000000000001 0.9137678747226888 0.9936411011327624 0.07987322641007355
1 0.650287840157117 0.6502878401571168 -2.220446049250313e-16