1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use crate::core::scalar::ControlScalar;
/// Natural cubic spline through N waypoints.
///
/// Given N time/value pairs (t_i, y_i), fits a piecewise cubic polynomial
/// satisfying:
/// - Interpolation: s(t_i) = y_i
/// - C² continuity at interior knots
/// - Natural BC: s''(t_0) = s''(t_{N-1}) = 0
///
/// For each segment i ∈ [0, N-2]:
/// s_i(t) = y[i] + b[i]*h + c[i]*h² + d[i]*h³ where h = t - t[i]
///
/// N must be ≥ 2.
#[derive(Debug, Clone, Copy)]
pub struct CubicSpline<S: ControlScalar, const N: usize> {
t: [S; N],
y: [S; N],
/// Coefficient of h¹ (first derivative at left knot of each segment).
b: [S; N],
/// Coefficient of h².
c: [S; N],
/// Coefficient of h³.
d: [S; N],
}
impl<S: ControlScalar, const N: usize> CubicSpline<S, N> {
/// Fit a natural cubic spline to the given data points.
///
/// `t` must be strictly increasing (t[0] < t[1] < ... < t[N-1]).
/// Returns `None` if N < 2 or if any interval width is non-positive.
pub fn natural(t: [S; N], y: [S; N]) -> Option<Self> {
if N < 2 {
return None;
}
// h[i] = t[i+1] - t[i]
let mut h = [S::ZERO; N];
for i in 0..(N - 1) {
h[i] = t[i + 1] - t[i];
if h[i] <= S::ZERO {
return None;
}
}
// Second derivatives m[0..N]: natural BC → m[0]=m[N-1]=0
// Interior: solve tridiagonal system for m[1..N-2]
let mut m = [S::ZERO; N];
if N > 2 {
// Build tridiagonal system (size N-2 interior points)
// Lower diagonal: l[i] = h[i]
// Main diagonal: diag[i] = 2*(h[i]+h[i+1])
// Upper diagonal: u[i] = h[i+1]
// RHS: r[i] = 6*((y[i+2]-y[i+1])/h[i+1] - (y[i+1]-y[i])/h[i])
// For i = 1..N-2 (0-indexed interior points)
// Thomas algorithm: forward sweep then back substitution
let n_int = N - 2; // number of interior knots
let mut diag = [S::ZERO; N];
let mut rhs = [S::ZERO; N];
let mut lower = [S::ZERO; N];
let mut upper = [S::ZERO; N];
for i in 1..=(n_int) {
let idx = i - 1; // 0-based interior index
lower[idx] = h[i - 1];
diag[idx] = S::TWO * (h[i - 1] + h[i]);
upper[idx] = h[i];
rhs[idx] =
S::from_f64(6.0) * ((y[i + 1] - y[i]) / h[i] - (y[i] - y[i - 1]) / h[i - 1]);
}
// Forward elimination (Thomas)
let mut c_prime = [S::ZERO; N];
let mut d_prime = [S::ZERO; N];
if diag[0].abs() <= S::EPSILON {
return None;
}
c_prime[0] = upper[0] / diag[0];
d_prime[0] = rhs[0] / diag[0];
for i in 1..n_int {
let denom = diag[i] - lower[i] * c_prime[i - 1];
if denom.abs() <= S::EPSILON {
return None;
}
c_prime[i] = upper[i] / denom;
d_prime[i] = (rhs[i] - lower[i] * d_prime[i - 1]) / denom;
}
// Back substitution
if n_int > 0 {
m[n_int] = d_prime[n_int - 1];
for i in (0..n_int.saturating_sub(1)).rev() {
m[i + 1] = d_prime[i] - c_prime[i] * m[i + 2];
}
}
}
// Compute b, c, d coefficients for each segment
let mut b = [S::ZERO; N];
let mut c = [S::ZERO; N];
let mut d_coeff = [S::ZERO; N];
for i in 0..(N - 1) {
let hi = h[i];
c[i] = m[i] * S::HALF;
d_coeff[i] = (m[i + 1] - m[i]) / (S::from_f64(6.0) * hi);
b[i] = (y[i + 1] - y[i]) / hi - hi * (S::TWO * m[i] + m[i + 1]) / S::from_f64(6.0);
}
Some(Self {
t,
y,
b,
c,
d: d_coeff,
})
}
/// Find the segment index for time `t_query`.
fn segment(&self, t_query: S) -> usize {
if t_query <= self.t[0] {
return 0;
}
if t_query >= self.t[N - 1] {
return N - 2;
}
// Binary search
let mut lo = 0usize;
let mut hi = N - 1;
while hi - lo > 1 {
let mid = (lo + hi) / 2;
if t_query >= self.t[mid] {
lo = mid;
} else {
hi = mid;
}
}
lo
}
/// Evaluate the spline at `t_query`.
///
/// Clamps to endpoint values for queries outside [t[0], t[N-1]].
pub fn evaluate(&self, t_query: S) -> S {
if t_query <= self.t[0] {
return self.y[0];
}
if t_query >= self.t[N - 1] {
return self.y[N - 1];
}
let i = self.segment(t_query);
let h = t_query - self.t[i];
self.y[i] + h * (self.b[i] + h * (self.c[i] + h * self.d[i]))
}
/// Evaluate the first derivative at `t_query`.
pub fn velocity(&self, t_query: S) -> S {
let i = self.segment(t_query);
let h = t_query - self.t[i];
self.b[i] + h * (S::TWO * self.c[i] + S::from_f64(3.0) * h * self.d[i])
}
/// Evaluate the second derivative at `t_query`.
pub fn acceleration(&self, t_query: S) -> S {
let i = self.segment(t_query);
let h = t_query - self.t[i];
S::TWO * self.c[i] + S::from_f64(6.0) * h * self.d[i]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn interpolates_knots() {
let t = [0.0_f64, 1.0, 2.0, 3.0];
let y = [0.0_f64, 1.0, 0.0, 1.0];
let s = CubicSpline::natural(t, y).unwrap();
for i in 0..4 {
assert!((s.evaluate(t[i]) - y[i]).abs() < 1e-10, "knot {}", i);
}
}
#[test]
fn straight_line_exact() {
// Spline through collinear points should be exactly linear
let t = [0.0_f64, 1.0, 2.0];
let y = [0.0_f64, 1.0, 2.0];
let s = CubicSpline::natural(t, y).unwrap();
for step in 0..=20 {
let ti = step as f64 * 0.1;
assert!((s.evaluate(ti) - ti).abs() < 1e-10, "t={}", ti);
}
}
#[test]
fn clamps_at_endpoints() {
let t = [0.0_f64, 1.0, 2.0];
let y = [0.0_f64, 1.0, 0.0];
let s = CubicSpline::natural(t, y).unwrap();
assert!((s.evaluate(-1.0) - 0.0).abs() < 1e-10);
assert!((s.evaluate(3.0) - 0.0).abs() < 1e-10);
}
#[test]
fn velocity_at_midpoint() {
let t = [0.0_f64, 1.0, 2.0];
let y = [0.0_f64, 1.0, 2.0]; // linear
let s = CubicSpline::natural(t, y).unwrap();
// Linear spline should have velocity 1.0 everywhere
assert!((s.velocity(0.5) - 1.0).abs() < 1e-8);
assert!((s.velocity(1.5) - 1.0).abs() < 1e-8);
}
#[test]
fn natural_bc_zero_second_derivative() {
let t = [0.0_f64, 1.0, 2.0, 3.0];
let y = [1.0_f64, 2.0, 1.5, 3.0];
let s = CubicSpline::natural(t, y).unwrap();
// Natural boundary conditions: second derivative = 0 at endpoints
assert!(
s.acceleration(0.0).abs() < 1e-8,
"acc at t=0: {}",
s.acceleration(0.0)
);
assert!(
s.acceleration(3.0).abs() < 1e-8,
"acc at t=3: {}",
s.acceleration(3.0)
);
}
#[test]
fn two_point_spline() {
let t = [0.0_f64, 1.0];
let y = [0.0_f64, 1.0];
let s = CubicSpline::natural(t, y).unwrap();
assert!((s.evaluate(0.5) - 0.5).abs() < 1e-10);
}
#[test]
fn non_increasing_times_returns_none() {
let t = [0.0_f64, 1.0, 1.0]; // duplicate t
let y = [0.0_f64, 1.0, 2.0];
assert!(CubicSpline::natural(t, y).is_none());
}
}