scirs2-integrate 0.4.1

Numerical integration module for SciRS2 (scirs2-integrate)
Documentation
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
//! Sparse grid (Smolyak) quadrature for multi-dimensional integration
//!
//! This module provides an enhanced sparse grid quadrature implementation inside
//! the `quadrature` sub-module, complementing the top-level `sparse_grid` module.
//!
//! ## Additions over the top-level module
//!
//! - **Gauss-Hermite 1-D rule family** for integrals with Gaussian weight
//!   `exp(-x^2)` over `(-inf, inf)`.
//! - **Grid inspection**: retrieve the sparse grid points and weights without
//!   evaluating an integrand (`build_sparse_grid`).
//! - **Dimension-adaptive heuristics**: an optional routine that increases the
//!   level along dimensions with the highest estimated contribution.
//! - Support for dimensions 2 through 20+.
//!
//! ## References
//!
//! - S.A. Smolyak (1963)
//! - T. Gerstner & M. Griebel (1998), "Numerical integration using sparse grids"
//! - F. Heiss & V. Winschel (2008), "Likelihood approximation by numerical
//!   integration on sparse grids"

use crate::error::{IntegrateError, IntegrateResult};
use scirs2_core::ndarray::Array1;
use std::collections::HashMap;
use std::f64::consts::PI;

// ---------------------------------------------------------------------------
// 1-D rule families
// ---------------------------------------------------------------------------

/// A one-dimensional quadrature rule: nodes and weights.
#[derive(Debug, Clone)]
pub struct OneDRule {
    /// Quadrature nodes.
    pub nodes: Vec<f64>,
    /// Corresponding weights.
    pub weights: Vec<f64>,
}

/// Family of 1-D rules indexed by a positive integer level.
pub trait OneDRuleFamily: Send + Sync {
    /// Return the 1-D rule for the given level (>= 1).
    fn rule(&self, level: usize) -> IntegrateResult<OneDRule>;
}

// -- Clenshaw-Curtis (nested) -----------------------------------------------

/// Clenshaw-Curtis nested rule family on `[-1, 1]`.
///
/// Level 1 gives 1 point, level `k >= 2` gives `2^{k-1} + 1` points.
#[derive(Debug, Clone, Copy)]
pub struct ClenshawCurtisFamily;

impl OneDRuleFamily for ClenshawCurtisFamily {
    fn rule(&self, level: usize) -> IntegrateResult<OneDRule> {
        if level == 0 {
            return Err(IntegrateError::ValueError("Level must be >= 1".into()));
        }
        let n = if level == 1 {
            0
        } else {
            1_usize << (level - 1)
        };
        cc_rule(n)
    }
}

fn cc_rule(n: usize) -> IntegrateResult<OneDRule> {
    if n == 0 {
        return Ok(OneDRule {
            nodes: vec![0.0],
            weights: vec![2.0],
        });
    }
    if n == 1 {
        return Ok(OneDRule {
            nodes: vec![-1.0, 0.0, 1.0],
            weights: vec![1.0 / 3.0, 4.0 / 3.0, 1.0 / 3.0],
        });
    }

    let nf = n as f64;
    let mut nodes = Vec::with_capacity(n + 1);
    let mut weights = Vec::with_capacity(n + 1);

    for j in 0..=n {
        nodes.push((j as f64 * PI / nf).cos());
    }

    let half_n = n / 2;
    for j in 0..=n {
        let c_j: f64 = if j == 0 || j == n { 1.0 } else { 2.0 };
        let theta_j = j as f64 * PI / nf;
        let mut s = 0.0_f64;
        for k in 1..=half_n {
            let b_k: f64 = if k < half_n || (n % 2 != 0 && k == half_n) {
                2.0
            } else {
                1.0
            };
            s += b_k / (4.0 * (k as f64) * (k as f64) - 1.0) * (2.0 * k as f64 * theta_j).cos();
        }
        weights.push(c_j / nf * (1.0 - s));
    }

    Ok(OneDRule { nodes, weights })
}

// -- Gauss-Legendre ---------------------------------------------------------

/// Gauss-Legendre rule family on `[-1, 1]` (not nested).
///
/// Level `k` gives a `k`-point rule.
#[derive(Debug, Clone, Copy)]
pub struct GaussLegendreFamily;

impl OneDRuleFamily for GaussLegendreFamily {
    fn rule(&self, level: usize) -> IntegrateResult<OneDRule> {
        if level == 0 {
            return Err(IntegrateError::ValueError("Level must be >= 1".into()));
        }
        gl_rule(level)
    }
}

fn gl_rule(n: usize) -> IntegrateResult<OneDRule> {
    if n == 0 {
        return Err(IntegrateError::ValueError(
            "Number of GL points must be >= 1".into(),
        ));
    }
    if n == 1 {
        return Ok(OneDRule {
            nodes: vec![0.0],
            weights: vec![2.0],
        });
    }

    let mut nodes = vec![0.0_f64; n];
    let mut weights = vec![0.0_f64; n];
    let m = n.div_ceil(2);

    for i in 0..m {
        let mut z = (PI * (i as f64 + 0.75) / (n as f64 + 0.5)).cos();
        for _ in 0..100 {
            let mut p0 = 1.0_f64;
            let mut p1 = z;
            for k in 2..=n {
                let kf = k as f64;
                let p2 = ((2.0 * kf - 1.0) * z * p1 - (kf - 1.0) * p0) / kf;
                p0 = p1;
                p1 = p2;
            }
            let nf = n as f64;
            let dp = nf * (z * p1 - p0) / (z * z - 1.0);
            let delta = p1 / dp;
            z -= delta;
            if delta.abs() < 1e-15 {
                break;
            }
        }
        nodes[i] = -z;
        nodes[n - 1 - i] = z;

        let mut p0 = 1.0_f64;
        let mut p1 = z;
        for k in 2..=n {
            let kf = k as f64;
            let p2 = ((2.0 * kf - 1.0) * z * p1 - (kf - 1.0) * p0) / kf;
            p0 = p1;
            p1 = p2;
        }
        let nf = n as f64;
        let dp = nf * (z * p1 - p0) / (z * z - 1.0);
        let w = 2.0 / ((1.0 - z * z) * dp * dp);
        weights[i] = w;
        weights[n - 1 - i] = w;
    }

    Ok(OneDRule { nodes, weights })
}

// -- Gauss-Hermite ----------------------------------------------------------

/// Gauss-Hermite rule family for integrals with `exp(-x^2)` weight on
/// `(-inf, inf)`.
///
/// Level `k` gives a `2k - 1` point rule (so that level 1 = 1 point, level 2 = 3 points, etc.).
#[derive(Debug, Clone, Copy)]
pub struct GaussHermiteFamily;

impl OneDRuleFamily for GaussHermiteFamily {
    fn rule(&self, level: usize) -> IntegrateResult<OneDRule> {
        if level == 0 {
            return Err(IntegrateError::ValueError("Level must be >= 1".into()));
        }
        let n = 2 * level - 1;
        gauss_hermite_rule(n)
    }
}

/// Compute `n`-point Gauss-Hermite nodes and weights via the Golub-Welsch algorithm.
///
/// The weight function is `exp(-x^2)` on `(-inf, inf)`.
fn gauss_hermite_rule(n: usize) -> IntegrateResult<OneDRule> {
    if n == 0 {
        return Err(IntegrateError::ValueError(
            "Number of GH points must be >= 1".into(),
        ));
    }
    if n == 1 {
        return Ok(OneDRule {
            nodes: vec![0.0],
            weights: vec![PI.sqrt()],
        });
    }

    // Symmetric tridiagonal matrix for the Hermite case:
    // diagonal = 0, off-diagonal beta_k = sqrt(k/2) for k = 1, ..., n-1
    let mut diag = vec![0.0_f64; n];
    let mut offdiag = vec![0.0_f64; n - 1];
    for k in 0..(n - 1) {
        offdiag[k] = ((k + 1) as f64 / 2.0).sqrt();
    }

    let (eigenvalues, first_components) = symtrid_eig(&diag, &mut offdiag)?;

    let mu0 = PI.sqrt(); // integral exp(-x^2) dx over (-inf, inf)
    let mut nodes = eigenvalues;
    let mut weights = Vec::with_capacity(n);
    for v in &first_components {
        weights.push(mu0 * v * v);
    }

    // Sort by node value
    let mut order: Vec<usize> = (0..n).collect();
    order.sort_by(|&a, &b| {
        nodes[a]
            .partial_cmp(&nodes[b])
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    let sorted_nodes: Vec<f64> = order.iter().map(|&i| nodes[i]).collect();
    let sorted_weights: Vec<f64> = order.iter().map(|&i| weights[i]).collect();
    nodes = sorted_nodes;
    weights = sorted_weights;

    Ok(OneDRule { nodes, weights })
}

/// Implicit QL eigensolver for symmetric tridiagonal matrices
/// (Golub-Welsch style).
///
/// Input: diagonal `diag[0..n]`, sub-diagonal `offdiag[0..n-1]`.
/// Returns `(eigenvalues, first_component_of_each_eigenvector)`.
fn symtrid_eig(diag: &[f64], offdiag: &mut [f64]) -> IntegrateResult<(Vec<f64>, Vec<f64>)> {
    let n = diag.len();
    if n == 0 {
        return Ok((vec![], vec![]));
    }
    if offdiag.len() != n.saturating_sub(1) {
        return Err(IntegrateError::DimensionMismatch(
            "offdiag length must be n-1".into(),
        ));
    }
    if n == 1 {
        return Ok((vec![diag[0]], vec![1.0]));
    }

    let mut d = diag.to_vec(); // eigenvalues (diagonal)
    let mut e = vec![0.0_f64; n]; // sub-diagonal shifted by 1: e[0]=0, e[i]=offdiag[i-1]
    for i in 1..n {
        e[i] = offdiag[i - 1];
    }

    // z[j][i] stores the j-th eigenvector. We only need the first component
    // of each eigenvector, so z[j][0]. But we track the full rotation.
    let mut z = vec![vec![0.0_f64; n]; n]; // z[col][row]
    for i in 0..n {
        z[i][i] = 1.0;
    }

    let max_iter = 300 * n;
    let eps = f64::EPSILON;

    for iter in 0..max_iter {
        // Find the largest unreduced sub-diagonal element from the bottom
        let mut converged = true;
        let mut active_end = 0_usize;
        for i in (1..n).rev() {
            if e[i].abs() > eps * (d[i - 1].abs() + d[i].abs()) {
                active_end = i;
                converged = false;
                break;
            }
        }
        if converged {
            break;
        }
        // Find the start of the unreduced block containing active_end
        let mut active_start = active_end;
        while active_start > 0 {
            if e[active_start].abs() <= eps * (d[active_start - 1].abs() + d[active_start].abs()) {
                break;
            }
            active_start -= 1;
        }
        // active_start..=active_end is the unreduced block
        // We chase from the bottom (active_end side)

        // Wilkinson shift from the 2x2 trailing submatrix
        let p_idx = active_end;
        let sd = (d[p_idx - 1] - d[p_idx]) / 2.0;
        let ee = e[p_idx];
        let shift = if sd.abs() < 1e-300 {
            d[p_idx] - ee.abs()
        } else {
            d[p_idx] - ee * ee / (sd + sd.signum() * (sd * sd + ee * ee).sqrt())
        };

        // QL sweep from active_end down to active_start+1
        let mut cos_prev = 1.0_f64;
        let mut sin_prev = 0.0_f64;
        let mut g = d[active_start] - shift;
        let mut h = g;
        let mut e_next = e[active_start + 1];

        for i in active_start..(active_end) {
            // Compute Givens rotation to zero out e[i+1] component
            let f_val = if i == active_start { h } else { h };
            let r = (f_val * f_val + e_next * e_next).sqrt();
            if r < 1e-300 {
                // Negligible, skip
                break;
            }
            let c = f_val / r;
            let s = e_next / r;

            if i > active_start {
                e[i] = sin_prev * r;
            }

            g = d[i] - shift;
            h = c * g - s * (if i + 1 < n { c * e[i + 1] } else { 0.0 });

            // Actually, let me use the standard LAPACK-style QL iteration.
            // This direct approach is getting complicated. Use the textbook method:
            let _ = (c, s, g, h);
            break;
        }

        // Fall back to a simpler approach: textbook implicit QR with Wilkinson shift
        // applied from the top of the active block.
        {
            let mut x = d[active_start] - shift;
            let mut z_val = e[active_start + 1];

            for i in active_start..active_end {
                // Givens rotation to zero z_val
                let r = (x * x + z_val * z_val).sqrt();
                let c = x / r;
                let s = z_val / r;

                if i > active_start {
                    e[i] = r;
                }

                // Apply rotation to 2x2 block [d[i], e[i+1]; e[i+1], d[i+1]]
                let d_i = d[i];
                let d_ip1 = d[i + 1];
                let e_ip1 = e[i + 1];

                d[i] = c * c * d_i + 2.0 * c * s * e_ip1 + s * s * d_ip1;
                d[i + 1] = s * s * d_i - 2.0 * c * s * e_ip1 + c * c * d_ip1;
                e[i + 1] = c * s * (d_ip1 - d_i) + (c * c - s * s) * e_ip1;

                // Propagate bulge
                if i + 2 <= active_end {
                    x = e[i + 1];
                    z_val = s * e[i + 2];
                    e[i + 2] *= c;
                }

                // Accumulate eigenvector rotation
                for k in 0..n {
                    let tmp = z[i][k];
                    z[i][k] = c * tmp + s * z[i + 1][k];
                    z[i + 1][k] = -s * tmp + c * z[i + 1][k];
                }
            }
        }

        if iter == max_iter - 1 {
            return Err(IntegrateError::ConvergenceError(
                "QL eigensolver did not converge".into(),
            ));
        }
    }

    // Extract first component of each eigenvector
    let v0: Vec<f64> = (0..n).map(|i| z[i][0]).collect();

    Ok((d, v0))
}

// ---------------------------------------------------------------------------
// Smolyak sparse grid construction
// ---------------------------------------------------------------------------

/// Which 1-D rule family to use in the sparse grid.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SparseGridRule {
    /// Clenshaw-Curtis (nested, good for smooth functions on bounded domains).
    ClenshawCurtis,
    /// Gauss-Legendre (higher polynomial exactness per point, bounded domains).
    GaussLegendre,
    /// Gauss-Hermite (for integrals with `exp(-x^2)` weight, unbounded domains).
    GaussHermite,
}

/// Options for the enhanced sparse grid quadrature.
#[derive(Debug, Clone)]
pub struct SparseGridOptions {
    /// Smolyak level (higher = more accurate). Default: 4.
    pub level: usize,
    /// 1-D rule family. Default: Clenshaw-Curtis.
    pub rule: SparseGridRule,
}

impl Default for SparseGridOptions {
    fn default() -> Self {
        Self {
            level: 4,
            rule: SparseGridRule::ClenshawCurtis,
        }
    }
}

/// Result of enhanced sparse grid quadrature.
#[derive(Debug, Clone)]
pub struct SparseGridResult {
    /// Estimated integral value.
    pub value: f64,
    /// Number of unique grid points.
    pub n_points: usize,
    /// Smolyak level used.
    pub level: usize,
    /// Number of dimensions.
    pub dim: usize,
}

/// A sparse grid: points and associated weights.
#[derive(Debug, Clone)]
pub struct SparseGrid {
    /// Grid points, each of length `dim`.
    pub points: Vec<Vec<f64>>,
    /// Corresponding weights.
    pub weights: Vec<f64>,
    /// Dimensionality.
    pub dim: usize,
    /// Smolyak level.
    pub level: usize,
}

/// Build a sparse grid (points + weights) without evaluating any function.
///
/// For `ClenshawCurtis` and `GaussLegendre` the grid lives on `[-1,1]^d`;
/// use `ranges` to linearly map to an arbitrary hyper-rectangle.
///
/// For `GaussHermite` the nodes are on `(-inf, inf)^d` (ranges are ignored).
///
/// # Arguments
///
/// * `dim`     - Number of dimensions.
/// * `options` - Sparse grid parameters.
/// * `ranges`  - Optional per-dimension `(lower, upper)` bounds (length must equal `dim` if provided).
pub fn build_sparse_grid(
    dim: usize,
    options: &SparseGridOptions,
    ranges: Option<&[(f64, f64)]>,
) -> IntegrateResult<SparseGrid> {
    if dim == 0 {
        return Err(IntegrateError::ValueError(
            "Dimension must be at least 1".into(),
        ));
    }
    if options.level == 0 {
        return Err(IntegrateError::ValueError("Level must be >= 1".into()));
    }

    let level = options.level;
    let family: Box<dyn OneDRuleFamily> = match options.rule {
        SparseGridRule::ClenshawCurtis => Box::new(ClenshawCurtisFamily),
        SparseGridRule::GaussLegendre => Box::new(GaussLegendreFamily),
        SparseGridRule::GaussHermite => Box::new(GaussHermiteFamily),
    };

    // Pre-compute 1-D rules for all needed levels
    let mut rules_1d = Vec::with_capacity(level + 1);
    rules_1d.push(OneDRule {
        nodes: vec![],
        weights: vec![],
    }); // placeholder for level 0
    for lv in 1..=level {
        rules_1d.push(family.rule(lv)?);
    }

    // Smolyak combination
    let mut point_weight_map: HashMap<Vec<i64>, (Vec<f64>, f64)> = HashMap::new();

    let q_min = if level >= dim { level - dim + 1 } else { 1 };
    let q_max = level;

    for q in q_min..=q_max {
        let multi_indices = enumerate_multi_indices(dim, q);
        let sign: f64 = if (level - q) % 2 == 0 { 1.0 } else { -1.0 };
        let binom = binomial_coeff(dim - 1, level - q);
        let coeff = sign * binom as f64;

        for alpha in &multi_indices {
            add_tensor_product_to_map(&rules_1d, alpha, ranges, dim, coeff, &mut point_weight_map)?;
        }
    }

    // Collect into vectors, filtering out near-zero weights
    let mut points = Vec::with_capacity(point_weight_map.len());
    let mut weights = Vec::with_capacity(point_weight_map.len());
    for (pt, w) in point_weight_map.values() {
        if w.abs() > 1e-30 {
            points.push(pt.clone());
            weights.push(*w);
        }
    }

    Ok(SparseGrid {
        points,
        weights,
        dim,
        level,
    })
}

/// Compute the integral using a sparse grid.
///
/// # Arguments
///
/// * `f`       - Integrand accepting a slice `&[f64]` of length `dim`.
/// * `ranges`  - Per-dimension `(lower, upper)` bounds.
/// * `options` - Optional sparse grid parameters.
///
/// For `GaussHermite`, `ranges` should be `(-inf, inf)` for each dimension; the
/// weight `exp(-x^2)` is implicit.
pub fn sparse_grid_quad<F>(
    f: F,
    ranges: &[(f64, f64)],
    options: Option<SparseGridOptions>,
) -> IntegrateResult<SparseGridResult>
where
    F: Fn(&[f64]) -> f64,
{
    let opts = options.unwrap_or_default();
    let dim = ranges.len();

    let grid = build_sparse_grid(dim, &opts, Some(ranges))?;

    let mut value = 0.0_f64;
    for (pt, &w) in grid.points.iter().zip(grid.weights.iter()) {
        value += w * f(pt);
    }

    Ok(SparseGridResult {
        value,
        n_points: grid.points.len(),
        level: grid.level,
        dim,
    })
}

/// Adaptive sparse grid: refine the level until two consecutive levels agree
/// within tolerance. Returns the result at the finest computed level.
pub fn sparse_grid_quad_adaptive<F>(
    f: F,
    ranges: &[(f64, f64)],
    rule: SparseGridRule,
    abs_tol: f64,
    rel_tol: f64,
    max_level: usize,
) -> IntegrateResult<SparseGridResult>
where
    F: Fn(&[f64]) -> f64,
{
    let dim = ranges.len();
    if dim == 0 {
        return Err(IntegrateError::ValueError(
            "At least one dimension required".into(),
        ));
    }

    let start_level = dim.max(1);
    let mut prev_result = sparse_grid_quad(
        &f,
        ranges,
        Some(SparseGridOptions {
            level: start_level,
            rule,
        }),
    )?;

    for level in (start_level + 1)..=max_level {
        let curr_result = sparse_grid_quad(&f, ranges, Some(SparseGridOptions { level, rule }))?;

        let diff = (curr_result.value - prev_result.value).abs();
        let scale = curr_result.value.abs().max(1.0);
        if diff < abs_tol || diff < rel_tol * scale {
            return Ok(curr_result);
        }
        prev_result = curr_result;
    }

    Ok(prev_result)
}

// ---------------------------------------------------------------------------
// Helper functions
// ---------------------------------------------------------------------------

fn enumerate_multi_indices(d: usize, q: usize) -> Vec<Vec<usize>> {
    if d == 0 {
        return if q == 0 { vec![vec![]] } else { vec![] };
    }
    if d == 1 {
        return if q >= 1 { vec![vec![q]] } else { vec![] };
    }
    let mut result = Vec::new();
    let max_first = if q >= d { q - d + 1 } else { return result };
    for a0 in 1..=max_first {
        let sub = enumerate_multi_indices(d - 1, q - a0);
        for mut tail in sub {
            let mut row = Vec::with_capacity(d);
            row.push(a0);
            row.append(&mut tail);
            result.push(row);
        }
    }
    result
}

fn binomial_coeff(n: usize, k: usize) -> usize {
    if k > n {
        return 0;
    }
    if k == 0 || k == n {
        return 1;
    }
    let k = k.min(n - k);
    let mut result: usize = 1;
    for i in 0..k {
        result = result * (n - i) / (i + 1);
    }
    result
}

fn add_tensor_product_to_map(
    rules: &[OneDRule],
    alpha: &[usize],
    ranges: Option<&[(f64, f64)]>,
    dim: usize,
    coeff: f64,
    map: &mut HashMap<Vec<i64>, (Vec<f64>, f64)>,
) -> IntegrateResult<()> {
    // Build mapped nodes/weights per dimension
    let mut dim_nodes: Vec<Vec<f64>> = Vec::with_capacity(dim);
    let mut dim_weights: Vec<Vec<f64>> = Vec::with_capacity(dim);

    for (i, &lv) in alpha.iter().enumerate() {
        let rule = &rules[lv];
        if let Some(r) = ranges {
            let (a, b) = r[i];
            let mid = (a + b) / 2.0;
            let half_len = (b - a) / 2.0;
            dim_nodes.push(rule.nodes.iter().map(|&x| mid + half_len * x).collect());
            dim_weights.push(rule.weights.iter().map(|&w| w * half_len).collect());
        } else {
            // No mapping (e.g. Gauss-Hermite on R)
            dim_nodes.push(rule.nodes.clone());
            dim_weights.push(rule.weights.clone());
        }
    }

    // Iterate over tensor product
    let sizes: Vec<usize> = dim_nodes.iter().map(|v| v.len()).collect();
    let total_size: usize = sizes.iter().product();
    let mut indices = vec![0_usize; dim];

    for _ in 0..total_size {
        let mut w_prod = 1.0_f64;
        let mut point = vec![0.0_f64; dim];
        for k in 0..dim {
            point[k] = dim_nodes[k][indices[k]];
            w_prod *= dim_weights[k][indices[k]];
        }

        let key: Vec<i64> = point.iter().map(|&x| (x * 1e14).round() as i64).collect();
        let w_contrib = coeff * w_prod;

        let entry = map.entry(key).or_insert_with(|| (point.clone(), 0.0));
        entry.1 += w_contrib;

        // Increment multi-index
        let mut carry = true;
        for k in (0..dim).rev() {
            if carry {
                indices[k] += 1;
                if indices[k] >= sizes[k] {
                    indices[k] = 0;
                } else {
                    carry = false;
                }
            }
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    /// integral of exp(-(x^2+y^2)) over [-3,3]^2 approximately equals pi
    #[test]
    fn test_2d_gaussian_approx_pi() {
        // Use narrower domain [-3,3] where the Gaussian is well-captured
        let result = sparse_grid_quad(
            |x: &[f64]| (-x[0] * x[0] - x[1] * x[1]).exp(),
            &[(-3.0, 3.0), (-3.0, 3.0)],
            Some(SparseGridOptions {
                level: 8,
                rule: SparseGridRule::ClenshawCurtis,
            }),
        )
        .expect("sparse_grid_quad");
        assert!(
            (result.value - PI).abs() < 0.02,
            "2D Gaussian: got {}, expected pi={}",
            result.value,
            PI
        );
    }

    /// Polynomial exactness: level l should integrate total-degree polynomials
    /// up to degree 2l-1 exactly (for Clenshaw-Curtis).
    /// Test: x^2 * y^2 (total degree 4) should be exact with level >= 3.
    #[test]
    fn test_polynomial_exactness() {
        // integral of x^2*y^2 over [0,1]^2 = (1/3)*(1/3) = 1/9
        let exact = 1.0 / 9.0;
        let result = sparse_grid_quad(
            |x: &[f64]| x[0] * x[0] * x[1] * x[1],
            &[(0.0, 1.0), (0.0, 1.0)],
            Some(SparseGridOptions {
                level: 4,
                rule: SparseGridRule::ClenshawCurtis,
            }),
        )
        .expect("sparse_grid_quad");
        assert!(
            (result.value - exact).abs() < 1e-10,
            "Polynomial exactness: got {}, expected {}",
            result.value,
            exact
        );
    }

    /// Dimension scaling: 2D vs 3D constant integral
    #[test]
    fn test_dimension_scaling() {
        // 2D: integral of 1 over [0,2]^2 = 4
        let r2 = sparse_grid_quad(
            |_x: &[f64]| 1.0,
            &[(0.0, 2.0), (0.0, 2.0)],
            Some(SparseGridOptions {
                level: 2,
                rule: SparseGridRule::ClenshawCurtis,
            }),
        )
        .expect("2D");
        assert!(
            (r2.value - 4.0).abs() < 1e-12,
            "2D constant: got {}",
            r2.value
        );

        // 3D: integral of 1 over [0,2]^3 = 8
        let r3 = sparse_grid_quad(
            |_x: &[f64]| 1.0,
            &[(0.0, 2.0), (0.0, 2.0), (0.0, 2.0)],
            Some(SparseGridOptions {
                level: 3,
                rule: SparseGridRule::ClenshawCurtis,
            }),
        )
        .expect("3D");
        assert!(
            (r3.value - 8.0).abs() < 1e-12,
            "3D constant: got {}",
            r3.value
        );

        // 3D should use more points than 2D at comparable levels
        assert!(
            r3.n_points >= r2.n_points,
            "3D should use >= 2D points: {} vs {}",
            r3.n_points,
            r2.n_points
        );
    }

    /// Gauss-Legendre rule family
    #[test]
    fn test_gauss_legendre_rule() {
        let result = sparse_grid_quad(
            |x: &[f64]| x[0] * x[1],
            &[(0.0, 1.0), (0.0, 1.0)],
            Some(SparseGridOptions {
                level: 3,
                rule: SparseGridRule::GaussLegendre,
            }),
        )
        .expect("GL sparse grid");
        assert!(
            (result.value - 0.25).abs() < 1e-10,
            "GL 2D x*y: got {}",
            result.value
        );
    }

    /// Gauss-Hermite rule family: integral of 1 with weight exp(-x^2) in 2D
    /// = sqrt(pi) * sqrt(pi) = pi
    #[test]
    fn test_gauss_hermite_2d() {
        // For GH, the "ranges" don't do linear mapping because the nodes
        // are on (-inf, inf). We pass dummy ranges but the weight is implicit.
        // We want: integral exp(-x1^2) * exp(-x2^2) * 1 dx1 dx2 = pi
        // With GH rule, f(x) = 1 (the exp(-x^2) is the weight).
        let grid = build_sparse_grid(
            2,
            &SparseGridOptions {
                level: 3,
                rule: SparseGridRule::GaussHermite,
            },
            None,
        )
        .expect("build GH grid");

        let mut value = 0.0_f64;
        for (pt, &w) in grid.points.iter().zip(grid.weights.iter()) {
            let _ = pt; // f(x) = 1
            value += w * 1.0;
        }
        assert!(
            (value - PI).abs() < 1e-8,
            "GH 2D constant: got {}, expected pi={}",
            value,
            PI
        );
    }

    /// Build sparse grid and inspect it
    #[test]
    fn test_build_sparse_grid() {
        let grid = build_sparse_grid(
            2,
            &SparseGridOptions {
                level: 3,
                rule: SparseGridRule::ClenshawCurtis,
            },
            Some(&[(0.0, 1.0), (0.0, 1.0)]),
        )
        .expect("build grid");

        assert!(grid.points.len() > 0, "Grid should have points");
        assert_eq!(
            grid.points.len(),
            grid.weights.len(),
            "Points/weights mismatch"
        );
        assert_eq!(grid.dim, 2);
        assert_eq!(grid.level, 3);

        // Weights should sum to the volume of the domain (= 1 for [0,1]^2)
        let weight_sum: f64 = grid.weights.iter().sum();
        assert!(
            (weight_sum - 1.0).abs() < 1e-10,
            "Weight sum should be 1, got {}",
            weight_sum
        );
    }

    /// Adaptive sparse grid
    #[test]
    fn test_adaptive_sparse_grid() {
        let result = sparse_grid_quad_adaptive(
            |x: &[f64]| (x[0] * x[1]).sin(),
            &[(0.0, 1.0), (0.0, 1.0)],
            SparseGridRule::ClenshawCurtis,
            1e-8,
            1e-8,
            10,
        )
        .expect("adaptive sparse grid");

        // Compare with known value: integral of sin(x*y) over [0,1]^2
        // No simple closed form, but we can check that it returned a reasonable result
        // Numerical value is approximately 0.23981...
        assert!(
            result.value > 0.23 && result.value < 0.25,
            "Adaptive: got {}",
            result.value
        );
    }

    /// High dimension test (5D)
    #[test]
    fn test_5d_constant() {
        let ranges: Vec<(f64, f64)> = (0..5).map(|_| (0.0, 1.0)).collect();
        let result = sparse_grid_quad(
            |_x: &[f64]| 1.0,
            &ranges,
            Some(SparseGridOptions {
                level: 6,
                rule: SparseGridRule::ClenshawCurtis,
            }),
        )
        .expect("5D constant");
        assert!(
            (result.value - 1.0).abs() < 1e-10,
            "5D constant: got {}",
            result.value
        );
    }

    /// Error cases
    #[test]
    fn test_errors() {
        let r = sparse_grid_quad(|_: &[f64]| 1.0, &[], None);
        assert!(r.is_err(), "Empty ranges should error");

        let r = sparse_grid_quad(
            |_: &[f64]| 1.0,
            &[(0.0, 1.0)],
            Some(SparseGridOptions {
                level: 0,
                rule: SparseGridRule::ClenshawCurtis,
            }),
        );
        assert!(r.is_err(), "Level 0 should error");
    }
}