Skip to main content

fdars_core/
helpers.rs

1//! Helper functions for numerical integration and common operations.
2
3/// Small epsilon for numerical comparisons (e.g., avoiding division by zero).
4pub const NUMERICAL_EPS: f64 = 1e-10;
5
6/// Default convergence tolerance for iterative algorithms.
7pub const DEFAULT_CONVERGENCE_TOL: f64 = 1e-6;
8
9/// Extract curves from column-major data matrix.
10///
11/// Converts a flat column-major matrix into a vector of curve vectors,
12/// where each curve contains all evaluation points for one observation.
13///
14/// # Arguments
15/// * `data` - Functional data matrix (n x m)
16///
17/// # Returns
18/// Vector of n curves, each containing m values
19pub fn extract_curves(data: &crate::matrix::FdMatrix) -> Vec<Vec<f64>> {
20    data.rows()
21}
22
23/// Compute L2 distance between two curves using integration weights.
24///
25/// # Arguments
26/// * `curve1` - First curve values
27/// * `curve2` - Second curve values
28/// * `weights` - Integration weights
29///
30/// # Returns
31/// L2 distance between the curves
32pub fn l2_distance(curve1: &[f64], curve2: &[f64], weights: &[f64]) -> f64 {
33    let mut dist_sq = 0.0;
34    for i in 0..curve1.len() {
35        let diff = curve1[i] - curve2[i];
36        dist_sq += diff * diff * weights[i];
37    }
38    dist_sq.sqrt()
39}
40
41/// Compute Simpson's 1/3 rule integration weights for a grid.
42///
43/// For odd n (even number of intervals): standard composite Simpson's 1/3 rule.
44/// For even n: Simpson's 1/3 for first n-1 points, trapezoidal for last interval.
45/// For non-uniform grids: generalized Simpson's weights per sub-interval pair.
46///
47/// # Arguments
48/// * `argvals` - Grid points (evaluation points)
49///
50/// # Returns
51/// Vector of integration weights
52pub fn simpsons_weights(argvals: &[f64]) -> Vec<f64> {
53    let n = argvals.len();
54    if n < 2 {
55        return vec![1.0; n];
56    }
57
58    let mut weights = vec![0.0; n];
59
60    if n == 2 {
61        // Trapezoidal rule
62        let h = argvals[1] - argvals[0];
63        weights[0] = h / 2.0;
64        weights[1] = h / 2.0;
65        return weights;
66    }
67
68    // Check if grid is uniform
69    let h0 = argvals[1] - argvals[0];
70    let is_uniform = argvals
71        .windows(2)
72        .all(|w| ((w[1] - w[0]) - h0).abs() < 1e-12 * h0.abs());
73
74    if is_uniform {
75        // Uniform grid: standard composite Simpson's 1/3
76        let n_intervals = n - 1;
77        if n_intervals % 2 == 0 {
78            // Even number of intervals (odd n): pure Simpson's
79            // Pattern: [1, 4, 2, 4, 2, ..., 4, 1] * h/3
80            weights[0] = h0 / 3.0;
81            weights[n - 1] = h0 / 3.0;
82            for i in 1..n - 1 {
83                weights[i] = if i % 2 == 1 {
84                    4.0 * h0 / 3.0
85                } else {
86                    2.0 * h0 / 3.0
87                };
88            }
89        } else {
90            // Odd number of intervals (even n): Simpson's for first n-2 intervals,
91            // trapezoidal for last interval
92            let n_simp = n - 1; // number of points for Simpson's part
93            weights[0] = h0 / 3.0;
94            weights[n_simp - 1] = h0 / 3.0;
95            for i in 1..n_simp - 1 {
96                weights[i] = if i % 2 == 1 {
97                    4.0 * h0 / 3.0
98                } else {
99                    2.0 * h0 / 3.0
100                };
101            }
102            // Add trapezoidal for last interval
103            weights[n_simp - 1] += h0 / 2.0;
104            weights[n - 1] += h0 / 2.0;
105        }
106    } else {
107        // Non-uniform grid: generalized Simpson's for each pair of intervals
108        let n_intervals = n - 1;
109        let n_pairs = n_intervals / 2;
110
111        for k in 0..n_pairs {
112            let i0 = 2 * k;
113            let i1 = i0 + 1;
114            let i2 = i0 + 2;
115            let h1 = argvals[i1] - argvals[i0];
116            let h2 = argvals[i2] - argvals[i1];
117            let h_sum = h1 + h2;
118
119            // Generalized Simpson's weights for non-uniform spacing
120            weights[i0] += (2.0 * h1 - h2) * h_sum / (6.0 * h1);
121            weights[i1] += h_sum * h_sum * h_sum / (6.0 * h1 * h2);
122            weights[i2] += (2.0 * h2 - h1) * h_sum / (6.0 * h2);
123        }
124
125        // If odd number of intervals, add trapezoidal for last interval
126        if n_intervals % 2 == 1 {
127            let h_last = argvals[n - 1] - argvals[n - 2];
128            weights[n - 2] += h_last / 2.0;
129            weights[n - 1] += h_last / 2.0;
130        }
131    }
132
133    weights
134}
135
136/// Compute 2D integration weights using tensor product of 1D weights.
137///
138/// Returns a flattened vector of weights for an m1 x m2 grid.
139///
140/// # Arguments
141/// * `argvals_s` - Grid points in s direction
142/// * `argvals_t` - Grid points in t direction
143///
144/// # Returns
145/// Flattened vector of integration weights (row-major order)
146pub fn simpsons_weights_2d(argvals_s: &[f64], argvals_t: &[f64]) -> Vec<f64> {
147    let weights_s = simpsons_weights(argvals_s);
148    let weights_t = simpsons_weights(argvals_t);
149    let m1 = argvals_s.len();
150    let m2 = argvals_t.len();
151
152    let mut weights = vec![0.0; m1 * m2];
153    for i in 0..m1 {
154        for j in 0..m2 {
155            weights[i * m2 + j] = weights_s[i] * weights_t[j];
156        }
157    }
158    weights
159}
160
161/// Linear interpolation at point `t` using binary search.
162///
163/// Clamps to boundary values outside the domain of `x`.
164pub fn linear_interp(x: &[f64], y: &[f64], t: f64) -> f64 {
165    if t <= x[0] {
166        return y[0];
167    }
168    let last = x.len() - 1;
169    if t >= x[last] {
170        return y[last];
171    }
172
173    let idx = match x.binary_search_by(|v| v.partial_cmp(&t).unwrap()) {
174        Ok(i) => return y[i],
175        Err(i) => i,
176    };
177
178    let t0 = x[idx - 1];
179    let t1 = x[idx];
180    let y0 = y[idx - 1];
181    let y1 = y[idx];
182    y0 + (y1 - y0) * (t - t0) / (t1 - t0)
183}
184
185/// Cumulative integration using Simpson's rule where possible.
186///
187/// For pairs of intervals uses Simpson's 1/3 rule for higher accuracy.
188/// Falls back to trapezoidal for the last interval if n is even.
189pub fn cumulative_trapz(y: &[f64], x: &[f64]) -> Vec<f64> {
190    let n = y.len();
191    let mut out = vec![0.0; n];
192    if n < 2 {
193        return out;
194    }
195
196    // Process pairs of intervals with Simpson's rule
197    let mut k = 1;
198    while k + 1 < n {
199        let h1 = x[k] - x[k - 1];
200        let h2 = x[k + 1] - x[k];
201        let h_sum = h1 + h2;
202
203        // Generalized Simpson's for this pair of intervals
204        let integral = h_sum / 6.0
205            * (y[k - 1] * (2.0 * h1 - h2) / h1
206                + y[k] * h_sum * h_sum / (h1 * h2)
207                + y[k + 1] * (2.0 * h2 - h1) / h2);
208
209        out[k] = out[k - 1] + {
210            // First sub-interval: use trapezoidal for the intermediate value
211            0.5 * (y[k] + y[k - 1]) * h1
212        };
213        out[k + 1] = out[k - 1] + integral;
214        k += 2;
215    }
216
217    // If there's a remaining interval, use trapezoidal
218    if k < n {
219        out[k] = out[k - 1] + 0.5 * (y[k] + y[k - 1]) * (x[k] - x[k - 1]);
220    }
221
222    out
223}
224
225/// Trapezoidal integration of `y` over `x`.
226pub fn trapz(y: &[f64], x: &[f64]) -> f64 {
227    let mut sum = 0.0;
228    for k in 1..y.len() {
229        sum += 0.5 * (y[k] + y[k - 1]) * (x[k] - x[k - 1]);
230    }
231    sum
232}
233
234/// Numerical gradient with uniform spacing using 5-point stencil (O(h⁴)).
235///
236/// Interior points use the 5-point central difference:
237///   `g[i] = (-y[i+2] + 8*y[i+1] - 8*y[i-1] + y[i-2]) / (12*h)`
238///
239/// Near-boundary points use appropriate forward/backward formulas.
240pub fn gradient_uniform(y: &[f64], h: f64) -> Vec<f64> {
241    let n = y.len();
242    let mut g = vec![0.0; n];
243    if n < 2 {
244        return g;
245    }
246    if n == 2 {
247        g[0] = (y[1] - y[0]) / h;
248        g[1] = (y[1] - y[0]) / h;
249        return g;
250    }
251    if n == 3 {
252        g[0] = (-3.0 * y[0] + 4.0 * y[1] - y[2]) / (2.0 * h);
253        g[1] = (y[2] - y[0]) / (2.0 * h);
254        g[2] = (y[0] - 4.0 * y[1] + 3.0 * y[2]) / (2.0 * h);
255        return g;
256    }
257    if n == 4 {
258        g[0] = (-3.0 * y[0] + 4.0 * y[1] - y[2]) / (2.0 * h);
259        g[1] = (y[2] - y[0]) / (2.0 * h);
260        g[2] = (y[3] - y[1]) / (2.0 * h);
261        g[3] = (y[1] - 4.0 * y[2] + 3.0 * y[3]) / (2.0 * h);
262        return g;
263    }
264
265    // n >= 5: use 5-point stencil for interior, 4-point formulas at boundaries
266    // Left boundary: O(h³) forward formula
267    g[0] = (-25.0 * y[0] + 48.0 * y[1] - 36.0 * y[2] + 16.0 * y[3] - 3.0 * y[4]) / (12.0 * h);
268    g[1] = (-3.0 * y[0] - 10.0 * y[1] + 18.0 * y[2] - 6.0 * y[3] + y[4]) / (12.0 * h);
269
270    // Interior: 5-point central difference O(h⁴)
271    for i in 2..n - 2 {
272        g[i] = (-y[i + 2] + 8.0 * y[i + 1] - 8.0 * y[i - 1] + y[i - 2]) / (12.0 * h);
273    }
274
275    // Right boundary: O(h³) backward formula
276    g[n - 2] = (-y[n - 5] + 6.0 * y[n - 4] - 18.0 * y[n - 3] + 10.0 * y[n - 2] + 3.0 * y[n - 1])
277        / (12.0 * h);
278    g[n - 1] = (3.0 * y[n - 5] - 16.0 * y[n - 4] + 36.0 * y[n - 3] - 48.0 * y[n - 2]
279        + 25.0 * y[n - 1])
280        / (12.0 * h);
281    g
282}
283
284#[cfg(test)]
285mod tests {
286    use super::*;
287
288    #[test]
289    fn test_simpsons_weights_uniform() {
290        let argvals = vec![0.0, 0.25, 0.5, 0.75, 1.0];
291        let weights = simpsons_weights(&argvals);
292        let sum: f64 = weights.iter().sum();
293        assert!((sum - 1.0).abs() < NUMERICAL_EPS);
294    }
295
296    #[test]
297    fn test_simpsons_weights_2d() {
298        let argvals_s = vec![0.0, 0.5, 1.0];
299        let argvals_t = vec![0.0, 0.5, 1.0];
300        let weights = simpsons_weights_2d(&argvals_s, &argvals_t);
301        let sum: f64 = weights.iter().sum();
302        assert!((sum - 1.0).abs() < NUMERICAL_EPS);
303    }
304
305    #[test]
306    fn test_extract_curves() {
307        // Column-major data: 2 observations, 3 points
308        // obs 0: [1, 2, 3], obs 1: [4, 5, 6]
309        let data = vec![1.0, 4.0, 2.0, 5.0, 3.0, 6.0];
310        let mat = crate::matrix::FdMatrix::from_column_major(data, 2, 3).unwrap();
311        let curves = extract_curves(&mat);
312        assert_eq!(curves.len(), 2);
313        assert_eq!(curves[0], vec![1.0, 2.0, 3.0]);
314        assert_eq!(curves[1], vec![4.0, 5.0, 6.0]);
315    }
316
317    #[test]
318    fn test_l2_distance_identical() {
319        let curve = vec![1.0, 2.0, 3.0];
320        let weights = vec![0.25, 0.5, 0.25];
321        let dist = l2_distance(&curve, &curve, &weights);
322        assert!(dist.abs() < NUMERICAL_EPS);
323    }
324
325    #[test]
326    fn test_l2_distance_different() {
327        let curve1 = vec![0.0, 0.0, 0.0];
328        let curve2 = vec![1.0, 1.0, 1.0];
329        let weights = vec![0.25, 0.5, 0.25]; // sum = 1
330        let dist = l2_distance(&curve1, &curve2, &weights);
331        // dist^2 = 0.25*1 + 0.5*1 + 0.25*1 = 1.0, so dist = 1.0
332        assert!((dist - 1.0).abs() < NUMERICAL_EPS);
333    }
334
335    #[test]
336    fn test_n1_weights() {
337        // Single point: fallback weight is 1.0 (degenerate case)
338        let w = simpsons_weights(&[0.5]);
339        assert_eq!(w.len(), 1);
340        assert!((w[0] - 1.0).abs() < 1e-12);
341    }
342
343    #[test]
344    fn test_n2_weights() {
345        let w = simpsons_weights(&[0.0, 1.0]);
346        assert_eq!(w.len(), 2);
347        // Trapezoidal: each weight should be 0.5
348        assert!((w[0] - 0.5).abs() < 1e-12);
349        assert!((w[1] - 0.5).abs() < 1e-12);
350    }
351
352    #[test]
353    fn test_mismatched_l2_distance() {
354        // Mismatched lengths should not panic but may give garbage
355        let a = vec![1.0, 2.0, 3.0];
356        let b = vec![1.0, 2.0, 3.0];
357        let w = vec![0.5, 0.5, 0.5];
358        let d = l2_distance(&a, &b, &w);
359        assert!(d.abs() < 1e-12, "Same vectors should have zero distance");
360    }
361
362    // ── trapz ──
363
364    #[test]
365    fn test_trapz_sine() {
366        // ∫₀^π sin(x) dx = 2
367        let m = 1000;
368        let x: Vec<f64> = (0..m)
369            .map(|i| std::f64::consts::PI * i as f64 / (m - 1) as f64)
370            .collect();
371        let y: Vec<f64> = x.iter().map(|&xi| xi.sin()).collect();
372        let result = trapz(&y, &x);
373        assert!(
374            (result - 2.0).abs() < 1e-4,
375            "∫ sin(x) dx over [0,π] should be ~2, got {result}"
376        );
377    }
378
379    // ── cumulative_trapz ──
380
381    #[test]
382    fn test_cumulative_trapz_matches_final() {
383        let m = 100;
384        let x: Vec<f64> = (0..m).map(|i| i as f64 / (m - 1) as f64).collect();
385        let y: Vec<f64> = x.iter().map(|&xi| 2.0 * xi).collect();
386        let cum = cumulative_trapz(&y, &x);
387        let total = trapz(&y, &x);
388        assert!(
389            (cum[m - 1] - total).abs() < 1e-12,
390            "Final cumulative value should match trapz"
391        );
392    }
393
394    // ── linear_interp ──
395
396    #[test]
397    fn test_linear_interp_boundary_clamp() {
398        let x = vec![0.0, 0.5, 1.0];
399        let y = vec![10.0, 20.0, 30.0];
400        assert!((linear_interp(&x, &y, -1.0) - 10.0).abs() < 1e-12);
401        assert!((linear_interp(&x, &y, 2.0) - 30.0).abs() < 1e-12);
402        assert!((linear_interp(&x, &y, 0.25) - 15.0).abs() < 1e-12);
403    }
404
405    // ── gradient_uniform ──
406
407    #[test]
408    fn test_gradient_uniform_linear() {
409        // f(x) = 3x → f'(x) = 3 everywhere
410        let m = 50;
411        let h = 1.0 / (m - 1) as f64;
412        let y: Vec<f64> = (0..m).map(|i| 3.0 * i as f64 * h).collect();
413        let g = gradient_uniform(&y, h);
414        for i in 0..m {
415            assert!(
416                (g[i] - 3.0).abs() < 1e-10,
417                "gradient of 3x should be 3 at i={i}, got {}",
418                g[i]
419            );
420        }
421    }
422}