numrs2 0.3.1

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! Numerical Integration Module
//!
//! This module provides functions for numerical integration (quadrature):
//!
//! - **Gauss-Kronrod Adaptive Quadrature**: High-precision adaptive integration
//! - **Monte Carlo Integration**: Random sampling-based integration for high dimensions
//! - **Simpson's Rule**: Classic composite integration
//! - **Trapezoidal Rule**: Basic composite integration
//! - **Romberg Integration**: Richardson extrapolation for improved accuracy
//!
//! # Examples
//!
//! ```ignore
//! use numrs2::integrate::{quad, quad_config, monte_carlo_integrate};
//!
//! // Simple integration
//! let result = quad(|x| x * x, 0.0, 1.0).unwrap();
//! assert!((result - 1.0/3.0).abs() < 1e-10);
//!
//! // Monte Carlo for high-dimensional integration
//! let result = monte_carlo_integrate(|x| x[0] * x[1], &[(0.0, 1.0), (0.0, 1.0)], 10000);
//! ```

use crate::error::{NumRs2Error, Result};
use num_traits::Float;
use std::fmt::Debug;

/// Result of numerical integration
#[derive(Debug, Clone)]
pub struct IntegrationResult<T> {
    /// Estimated integral value
    pub value: T,
    /// Estimated absolute error
    pub error: T,
    /// Number of function evaluations
    pub neval: usize,
    /// Success flag
    pub success: bool,
}

/// Configuration for adaptive integration
#[derive(Debug, Clone)]
pub struct QuadConfig<T> {
    /// Absolute tolerance
    pub atol: T,
    /// Relative tolerance
    pub rtol: T,
    /// Maximum number of subdivisions
    pub max_subdivisions: usize,
    /// Maximum recursion depth
    pub max_depth: usize,
}

impl<T: Float> Default for QuadConfig<T> {
    fn default() -> Self {
        QuadConfig {
            atol: T::from(1.49e-8).expect("1.49e-8 is representable as Float"),
            rtol: T::from(1.49e-8).expect("1.49e-8 is representable as Float"),
            max_subdivisions: 50,
            max_depth: 50,
        }
    }
}

/// Gauss-Kronrod 15-point nodes and weights for adaptive quadrature
/// These are the standard G7-K15 nodes for [-1, 1]
fn gauss_kronrod_15<T: Float>() -> (Vec<T>, Vec<T>, Vec<T>) {
    // Kronrod nodes (15 points)
    let nodes = vec![
        T::from(-0.991455371120813).expect("quadrature node is representable as Float"),
        T::from(-0.949107912342759).expect("quadrature node is representable as Float"),
        T::from(-0.864864423359769).expect("quadrature node is representable as Float"),
        T::from(-0.741531185599394).expect("quadrature node is representable as Float"),
        T::from(-0.586087235467691).expect("quadrature node is representable as Float"),
        T::from(-0.405845151377397).expect("quadrature node is representable as Float"),
        T::from(-0.207784955007898).expect("quadrature node is representable as Float"),
        T::from(0.0).expect("quadrature node is representable as Float"),
        T::from(0.207784955007898).expect("quadrature node is representable as Float"),
        T::from(0.405845151377397).expect("quadrature node is representable as Float"),
        T::from(0.586087235467691).expect("quadrature node is representable as Float"),
        T::from(0.741531185599394).expect("quadrature node is representable as Float"),
        T::from(0.864864423359769).expect("quadrature node is representable as Float"),
        T::from(0.949107912342759).expect("quadrature node is representable as Float"),
        T::from(0.991455371120813).expect("quadrature node is representable as Float"),
    ];

    // Kronrod weights (for all 15 points)
    let kronrod_weights = vec![
        T::from(0.022935322010529).expect("quadrature weight is representable as Float"),
        T::from(0.063092092629979).expect("quadrature weight is representable as Float"),
        T::from(0.104790010322250).expect("quadrature weight is representable as Float"),
        T::from(0.140653259715525).expect("quadrature weight is representable as Float"),
        T::from(0.169004726639267).expect("quadrature weight is representable as Float"),
        T::from(0.190350578064785).expect("quadrature weight is representable as Float"),
        T::from(0.204432940075298).expect("quadrature weight is representable as Float"),
        T::from(0.209482141084728).expect("quadrature weight is representable as Float"),
        T::from(0.204432940075298).expect("quadrature weight is representable as Float"),
        T::from(0.190350578064785).expect("quadrature weight is representable as Float"),
        T::from(0.169004726639267).expect("quadrature weight is representable as Float"),
        T::from(0.140653259715525).expect("quadrature weight is representable as Float"),
        T::from(0.104790010322250).expect("quadrature weight is representable as Float"),
        T::from(0.063092092629979).expect("quadrature weight is representable as Float"),
        T::from(0.022935322010529).expect("quadrature weight is representable as Float"),
    ];

    // Gauss weights (for the 7 Gauss points among the 15)
    // Indices: 1, 3, 5, 7, 9, 11, 13 (0-indexed)
    let gauss_weights = vec![
        T::from(0.129484966168870).expect("quadrature weight is representable as Float"),
        T::from(0.279705391489277).expect("quadrature weight is representable as Float"),
        T::from(0.381830050505119).expect("quadrature weight is representable as Float"),
        T::from(0.417959183673469).expect("quadrature weight is representable as Float"),
        T::from(0.381830050505119).expect("quadrature weight is representable as Float"),
        T::from(0.279705391489277).expect("quadrature weight is representable as Float"),
        T::from(0.129484966168870).expect("quadrature weight is representable as Float"),
    ];

    (nodes, kronrod_weights, gauss_weights)
}

/// Simple adaptive quadrature using Gauss-Kronrod G7-K15 rule
///
/// Integrates a function f over [a, b] using adaptive subdivision.
///
/// # Arguments
/// * `f` - Function to integrate
/// * `a` - Lower bound
/// * `b` - Upper bound
///
/// # Returns
/// * Approximate integral value
///
/// # Example
/// ```ignore
/// use numrs2::integrate::quad;
///
/// let result = quad(|x| x * x, 0.0, 1.0).unwrap();
/// assert!((result - 1.0/3.0).abs() < 1e-10);
/// ```
pub fn quad<T, F>(f: F, a: T, b: T) -> Result<T>
where
    T: Float + Debug + std::iter::Sum,
    F: Fn(T) -> T,
{
    let result = quad_adaptive(&f, a, b, &QuadConfig::default())?;
    Ok(result.value)
}

/// Adaptive quadrature with configuration
///
/// # Arguments
/// * `f` - Function to integrate
/// * `a` - Lower bound
/// * `b` - Upper bound
/// * `config` - Integration configuration
///
/// # Returns
/// * `IntegrationResult` with value, error estimate, and diagnostics
pub fn quad_adaptive<T, F>(
    f: &F,
    a: T,
    b: T,
    config: &QuadConfig<T>,
) -> Result<IntegrationResult<T>>
where
    T: Float + Debug + std::iter::Sum,
    F: Fn(T) -> T,
{
    let mut neval = 0;
    let result = gauss_kronrod_adaptive(
        f,
        a,
        b,
        config.atol,
        config.rtol,
        config.max_depth,
        &mut neval,
    )?;

    Ok(IntegrationResult {
        value: result.0,
        error: result.1,
        neval,
        success: true,
    })
}

/// Recursive Gauss-Kronrod adaptive integration
fn gauss_kronrod_adaptive<T, F>(
    f: &F,
    a: T,
    b: T,
    atol: T,
    rtol: T,
    max_depth: usize,
    neval: &mut usize,
) -> Result<(T, T)>
where
    T: Float + Debug + std::iter::Sum,
    F: Fn(T) -> T,
{
    let (nodes, kronrod_w, gauss_w) = gauss_kronrod_15::<T>();

    // Transform from [-1, 1] to [a, b]
    let half_length = (b - a) / T::from(2.0).expect("2.0 is representable as Float");
    let center = (a + b) / T::from(2.0).expect("2.0 is representable as Float");

    // Evaluate function at all Kronrod points
    let mut f_vals = Vec::with_capacity(15);
    for &node in &nodes {
        let x = center + half_length * node;
        f_vals.push(f(x));
        *neval += 1;
    }

    // Compute Kronrod integral (15 points)
    let kronrod_integral: T = kronrod_w
        .iter()
        .zip(f_vals.iter())
        .map(|(&w, &fv)| w * fv)
        .sum::<T>()
        * half_length;

    // Compute Gauss integral (7 points from the 15 - odd indices)
    let gauss_indices = [1, 3, 5, 7, 9, 11, 13];
    let gauss_integral: T = gauss_w
        .iter()
        .zip(gauss_indices.iter())
        .map(|(&w, &idx)| w * f_vals[idx])
        .sum::<T>()
        * half_length;

    // Error estimate
    let error = (kronrod_integral - gauss_integral).abs();
    let tolerance = atol.max(rtol * kronrod_integral.abs());

    // Check convergence or depth limit
    if error <= tolerance || max_depth == 0 {
        return Ok((kronrod_integral, error));
    }

    // Subdivide and recurse
    let mid = center;
    let (left_val, left_err) = gauss_kronrod_adaptive(
        f,
        a,
        mid,
        atol / T::from(2.0).expect("2.0 is representable as Float"),
        rtol,
        max_depth - 1,
        neval,
    )?;
    let (right_val, right_err) = gauss_kronrod_adaptive(
        f,
        mid,
        b,
        atol / T::from(2.0).expect("2.0 is representable as Float"),
        rtol,
        max_depth - 1,
        neval,
    )?;

    Ok((left_val + right_val, left_err + right_err))
}

/// Simpson's rule composite integration
///
/// Classic composite Simpson's rule with adaptive subdivisions.
///
/// # Arguments
/// * `f` - Function to integrate
/// * `a` - Lower bound
/// * `b` - Upper bound
/// * `n` - Number of subdivisions (must be even, will be adjusted if odd)
///
/// # Returns
/// * Approximate integral value
pub fn simps<T, F>(f: F, a: T, b: T, n: usize) -> T
where
    T: Float + Debug,
    F: Fn(T) -> T,
{
    // Ensure n is even
    let n = if n % 2 == 1 { n + 1 } else { n };
    let n = n.max(2);

    let h = (b - a) / T::from(n).expect("n is representable as Float");

    let mut sum = f(a) + f(b);

    for i in 1..n {
        let x = a + T::from(i).expect("i is representable as Float") * h;
        let coef = if i % 2 == 0 {
            T::from(2.0).expect("2.0 is representable as Float")
        } else {
            T::from(4.0).expect("4.0 is representable as Float")
        };
        sum = sum + coef * f(x);
    }

    sum * h / T::from(3.0).expect("3.0 is representable as Float")
}

/// Trapezoidal rule composite integration
///
/// # Arguments
/// * `f` - Function to integrate
/// * `a` - Lower bound
/// * `b` - Upper bound
/// * `n` - Number of subdivisions
///
/// # Returns
/// * Approximate integral value
pub fn trapz<T, F>(f: F, a: T, b: T, n: usize) -> T
where
    T: Float + Debug,
    F: Fn(T) -> T,
{
    let n = n.max(1);
    let h = (b - a) / T::from(n).expect("n is representable as Float");

    let mut sum = (f(a) + f(b)) / T::from(2.0).expect("2.0 is representable as Float");

    for i in 1..n {
        let x = a + T::from(i).expect("i is representable as Float") * h;
        sum = sum + f(x);
    }

    sum * h
}

/// Trapezoidal rule for arrays of x and y values
///
/// # Arguments
/// * `x` - Array of x coordinates (must be sorted in ascending order)
/// * `y` - Array of y values (f(x))
///
/// # Returns
/// * Approximate integral using trapezoidal rule
pub fn trapz_array<T>(x: &[T], y: &[T]) -> Result<T>
where
    T: Float + Debug,
{
    if x.len() != y.len() {
        return Err(NumRs2Error::DimensionMismatch(
            "x and y arrays must have same length".to_string(),
        ));
    }

    if x.len() < 2 {
        return Err(NumRs2Error::ValueError(
            "Arrays must have at least 2 elements".to_string(),
        ));
    }

    let mut sum = T::zero();
    for i in 0..x.len() - 1 {
        let h = x[i + 1] - x[i];
        sum = sum + h * (y[i] + y[i + 1]) / T::from(2.0).expect("2.0 is representable as Float");
    }

    Ok(sum)
}

/// Romberg integration
///
/// Uses Richardson extrapolation on the trapezoidal rule for improved accuracy.
///
/// # Arguments
/// * `f` - Function to integrate
/// * `a` - Lower bound
/// * `b` - Upper bound
/// * `max_order` - Maximum order of extrapolation (determines accuracy)
///
/// # Returns
/// * Approximate integral value
pub fn romberg<T, F>(f: F, a: T, b: T, max_order: usize) -> T
where
    T: Float + Debug,
    F: Fn(T) -> T,
{
    let max_order = max_order.clamp(1, 20); // Limit to reasonable range

    // Romberg tableau
    let mut r = vec![vec![T::zero(); max_order + 1]; max_order + 1];

    // First row: trapezoidal approximations
    for j in 0..=max_order {
        let n = 1usize << j; // 2^j subdivisions
        r[j][0] = trapz(&f, a, b, n);
    }

    // Richardson extrapolation
    for k in 1..=max_order {
        for j in k..=max_order {
            let factor = T::from(4u64.pow(k as u32)).expect("4^k is representable as Float");
            r[j][k] = (factor * r[j][k - 1] - r[j - 1][k - 1]) / (factor - T::one());
        }
    }

    r[max_order][max_order]
}

/// Monte Carlo integration for single-dimensional functions
///
/// # Arguments
/// * `f` - Function to integrate
/// * `a` - Lower bound
/// * `b` - Upper bound
/// * `n_samples` - Number of random samples
///
/// # Returns
/// * `IntegrationResult` with estimated value and statistical error
pub fn monte_carlo<T, F>(f: F, a: T, b: T, n_samples: usize) -> IntegrationResult<T>
where
    T: Float + Debug + std::iter::Sum,
    F: Fn(T) -> T,
{
    let domain_size = b - a;
    let n = n_samples.max(1);

    // Generate pseudo-random samples using LCG (Linear Congruential Generator)
    let mut state = 12345u64;
    let mut sum = T::zero();
    let mut sum_sq = T::zero();

    for _ in 0..n {
        // PCG-style LCG
        state = state
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        // Use full 64-bit state and scale to [0, 1)
        let u = T::from((state as f64) / (u64::MAX as f64))
            .expect("normalized random value is representable as Float");
        let x = a + u * domain_size;
        let fx = f(x);
        sum = sum + fx;
        sum_sq = sum_sq + fx * fx;
    }

    let n_float = T::from(n).expect("n is representable as Float");
    let mean = sum / n_float;
    let variance = (sum_sq / n_float) - mean * mean;
    let std_error = if variance > T::zero() {
        (variance / n_float).sqrt() * domain_size
    } else {
        T::zero()
    };

    let value = mean * domain_size;

    IntegrationResult {
        value,
        error: std_error,
        neval: n,
        success: true,
    }
}

/// Monte Carlo integration for multi-dimensional functions
///
/// # Arguments
/// * `f` - Function taking a slice of coordinates and returning a value
/// * `bounds` - Vector of (lower, upper) bounds for each dimension
/// * `n_samples` - Number of random samples
///
/// # Returns
/// * `IntegrationResult` with estimated value and statistical error
///
/// # Example
/// ```ignore
/// use numrs2::integrate::monte_carlo_nd;
///
/// // Integrate f(x,y) = x*y over [0,1] x [0,1]
/// let result = monte_carlo_nd(
///     |x| x[0] * x[1],
///     &[(0.0, 1.0), (0.0, 1.0)],
///     10000
/// );
/// // Expected: 0.25
/// ```
pub fn monte_carlo_nd<T, F>(f: F, bounds: &[(T, T)], n_samples: usize) -> IntegrationResult<T>
where
    T: Float + Debug + std::iter::Sum,
    F: Fn(&[T]) -> T,
{
    let ndim = bounds.len();
    let n = n_samples.max(1);

    // Compute domain volume
    let volume: T = bounds
        .iter()
        .map(|&(a, b)| b - a)
        .fold(T::one(), |acc, x| acc * x);

    // Generate pseudo-random samples using LCG with different seeds per dimension
    let mut states: Vec<u64> = (0..ndim).map(|i| 12345u64 + i as u64 * 1000003).collect();
    let mut sum = T::zero();
    let mut sum_sq = T::zero();
    let mut point = vec![T::zero(); ndim];

    for _ in 0..n {
        // Generate random point in domain
        for d in 0..ndim {
            states[d] = states[d]
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            // Use full 64-bit state
            let u = T::from((states[d] as f64) / (u64::MAX as f64))
                .expect("normalized random value is representable as Float");
            let (a, b) = bounds[d];
            point[d] = a + u * (b - a);
        }

        let fx = f(&point);
        sum = sum + fx;
        sum_sq = sum_sq + fx * fx;
    }

    let n_float = T::from(n).expect("n is representable as Float");
    let mean = sum / n_float;
    let variance = (sum_sq / n_float) - mean * mean;
    let std_error = if variance > T::zero() {
        (variance / n_float).sqrt() * volume
    } else {
        T::zero()
    };

    let value = mean * volume;

    IntegrationResult {
        value,
        error: std_error,
        neval: n,
        success: true,
    }
}

/// Gaussian quadrature with specified number of points
///
/// Uses Gauss-Legendre quadrature nodes and weights.
///
/// # Arguments
/// * `f` - Function to integrate
/// * `a` - Lower bound
/// * `b` - Upper bound
/// * `n` - Number of quadrature points (1-10 supported)
pub fn gauss_legendre<T, F>(f: F, a: T, b: T, n: usize) -> T
where
    T: Float + Debug + std::iter::Sum,
    F: Fn(T) -> T,
{
    let (nodes, weights) = gauss_legendre_nodes_weights::<T>(n);

    let half_length = (b - a) / T::from(2.0).expect("2.0 is representable as Float");
    let center = (a + b) / T::from(2.0).expect("2.0 is representable as Float");

    let sum: T = nodes
        .iter()
        .zip(weights.iter())
        .map(|(&node, &weight)| {
            let x = center + half_length * node;
            weight * f(x)
        })
        .sum();

    sum * half_length
}

/// Get Gauss-Legendre nodes and weights
fn gauss_legendre_nodes_weights<T: Float>(n: usize) -> (Vec<T>, Vec<T>) {
    match n {
        1 => (
            vec![T::from(0.0).expect("GL node is representable as Float")],
            vec![T::from(2.0).expect("GL weight is representable as Float")],
        ),
        2 => (
            vec![
                T::from(-0.5773502691896257).expect("GL node is representable as Float"),
                T::from(0.5773502691896257).expect("GL node is representable as Float"),
            ],
            vec![
                T::from(1.0).expect("GL weight is representable as Float"),
                T::from(1.0).expect("GL weight is representable as Float"),
            ],
        ),
        3 => (
            vec![
                T::from(-0.7745966692414834).expect("GL node is representable as Float"),
                T::from(0.0).expect("GL node is representable as Float"),
                T::from(0.7745966692414834).expect("GL node is representable as Float"),
            ],
            vec![
                T::from(0.5555555555555556).expect("GL weight is representable as Float"),
                T::from(0.8888888888888888).expect("GL weight is representable as Float"),
                T::from(0.5555555555555556).expect("GL weight is representable as Float"),
            ],
        ),
        4 => (
            vec![
                T::from(-0.8611363115940526).expect("GL node is representable as Float"),
                T::from(-0.3399810435848563).expect("GL node is representable as Float"),
                T::from(0.3399810435848563).expect("GL node is representable as Float"),
                T::from(0.8611363115940526).expect("GL node is representable as Float"),
            ],
            vec![
                T::from(0.3478548451374538).expect("GL weight is representable as Float"),
                T::from(0.6521451548625461).expect("GL weight is representable as Float"),
                T::from(0.6521451548625461).expect("GL weight is representable as Float"),
                T::from(0.3478548451374538).expect("GL weight is representable as Float"),
            ],
        ),
        5 => (
            vec![
                T::from(-0.9061798459386640).expect("GL node is representable as Float"),
                T::from(-0.5384693101056831).expect("GL node is representable as Float"),
                T::from(0.0).expect("GL node is representable as Float"),
                T::from(0.5384693101056831).expect("GL node is representable as Float"),
                T::from(0.9061798459386640).expect("GL node is representable as Float"),
            ],
            vec![
                T::from(0.2369268850561891).expect("GL weight is representable as Float"),
                T::from(0.4786286704993665).expect("GL weight is representable as Float"),
                T::from(0.5688888888888889).expect("GL weight is representable as Float"),
                T::from(0.4786286704993665).expect("GL weight is representable as Float"),
                T::from(0.2369268850561891).expect("GL weight is representable as Float"),
            ],
        ),
        _ => {
            // Default to 5-point rule for unsupported n
            gauss_legendre_nodes_weights(5)
        }
    }
}

/// Double integral over rectangular region
///
/// # Arguments
/// * `f` - Function of two variables
/// * `xa` - Lower x bound
/// * `xb` - Upper x bound
/// * `ya` - Lower y bound
/// * `yb` - Upper y bound
/// * `nx` - Number of subdivisions in x
/// * `ny` - Number of subdivisions in y
///
/// # Returns
/// * Approximate double integral
pub fn dblquad<T, F>(f: F, xa: T, xb: T, ya: T, yb: T, nx: usize, ny: usize) -> T
where
    T: Float + Debug + std::iter::Sum,
    F: Fn(T, T) -> T,
{
    // Use composite Simpson's rule in both dimensions
    let hx = (xb - xa) / T::from(nx).expect("nx is representable as Float");
    let hy = (yb - ya) / T::from(ny).expect("ny is representable as Float");

    let mut sum = T::zero();

    for i in 0..=nx {
        let x = xa + T::from(i).expect("i is representable as Float") * hx;
        let wx = if i == 0 || i == nx {
            T::one()
        } else if i % 2 == 0 {
            T::from(2.0).expect("2.0 is representable as Float")
        } else {
            T::from(4.0).expect("4.0 is representable as Float")
        };

        for j in 0..=ny {
            let y = ya + T::from(j).expect("j is representable as Float") * hy;
            let wy = if j == 0 || j == ny {
                T::one()
            } else if j % 2 == 0 {
                T::from(2.0).expect("2.0 is representable as Float")
            } else {
                T::from(4.0).expect("4.0 is representable as Float")
            };

            sum = sum + wx * wy * f(x, y);
        }
    }

    sum * hx * hy / T::from(9.0).expect("9.0 is representable as Float")
}

/// Fixed-point iteration for solving integral equations
///
/// Solves equations of the form: x = integral(f(t, x) dt, a, b)
///
/// # Arguments
/// * `f` - Function of (t, x)
/// * `a` - Lower bound
/// * `b` - Upper bound
/// * `x0` - Initial guess
/// * `max_iter` - Maximum iterations
/// * `tol` - Convergence tolerance
///
/// # Returns
/// * Solution x or error
pub fn fixed_point_integral<T, F>(f: F, a: T, b: T, x0: T, max_iter: usize, tol: T) -> Result<T>
where
    T: Float + Debug + std::iter::Sum,
    F: Fn(T, T) -> T,
{
    let mut x = x0;

    for _ in 0..max_iter {
        // Compute integral with current x
        let integrand = |t: T| f(t, x);
        let new_x = quad(integrand, a, b)?;

        let err = (new_x - x).abs();
        x = new_x;

        if err < tol {
            return Ok(x);
        }
    }

    Err(NumRs2Error::InvalidOperation(
        "Fixed point iteration did not converge".to_string(),
    ))
}

/// Cumulative integration using trapezoidal rule
///
/// # Arguments
/// * `y` - Array of y values
/// * `dx` - Spacing between x values (uniform)
///
/// # Returns
/// * Array of cumulative integral values
pub fn cumtrapz<T>(y: &[T], dx: T) -> Vec<T>
where
    T: Float + Debug,
{
    if y.is_empty() {
        return Vec::new();
    }

    let mut result = vec![T::zero(); y.len()];

    for i in 1..y.len() {
        result[i] = result[i - 1]
            + dx * (y[i - 1] + y[i]) / T::from(2.0).expect("2.0 is representable as Float");
    }

    result
}

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

    #[test]
    fn test_quad_polynomial() {
        // Integral of x^2 from 0 to 1 = 1/3
        let result = quad(|x: f64| x * x, 0.0, 1.0).expect("quad should succeed");
        assert!((result - 1.0 / 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_quad_sin() {
        // Integral of sin(x) from 0 to pi = 2
        let result =
            quad(|x: f64| x.sin(), 0.0, std::f64::consts::PI).expect("quad should succeed");
        assert!((result - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_quad_exp() {
        // Integral of e^x from 0 to 1 = e - 1
        let result = quad(|x: f64| x.exp(), 0.0, 1.0).expect("quad should succeed");
        let expected = std::f64::consts::E - 1.0;
        assert!((result - expected).abs() < 1e-10);
    }

    #[test]
    fn test_simps_polynomial() {
        // Integral of x^2 from 0 to 1 = 1/3
        let result = simps(|x: f64| x * x, 0.0, 1.0, 100);
        assert!((result - 1.0 / 3.0).abs() < 1e-8);
    }

    #[test]
    fn test_trapz_polynomial() {
        // Integral of x from 0 to 1 = 0.5
        let result = trapz(|x: f64| x, 0.0, 1.0, 100);
        assert!((result - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_trapz_array() {
        let x = vec![0.0, 0.5, 1.0];
        let y = vec![0.0, 0.25, 1.0];
        let result = trapz_array(&x, &y).expect("trapz_array should succeed");
        // Trapezoidal approximation of x^2
        assert!((result - 0.375).abs() < 1e-10);
    }

    #[test]
    fn test_romberg_polynomial() {
        // Integral of x^2 from 0 to 1 = 1/3
        let result = romberg(|x: f64| x * x, 0.0, 1.0, 5);
        assert!((result - 1.0 / 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_monte_carlo_constant() {
        // Integral of 1 from 0 to 1 = 1
        let result = monte_carlo(|_: f64| 1.0, 0.0, 1.0, 10000);
        assert!((result.value - 1.0).abs() < 0.05);
    }

    #[test]
    fn test_monte_carlo_linear() {
        // Integral of x from 0 to 1 = 0.5
        // Monte Carlo is stochastic - use larger samples and tolerance
        let result = monte_carlo(|x: f64| x, 0.0, 1.0, 100000);
        assert!(
            (result.value - 0.5).abs() < 0.1,
            "Monte Carlo linear integral {} too far from 0.5",
            result.value
        );
    }

    #[test]
    fn test_monte_carlo_nd_2d() {
        // Integral of x*y over [0,1] x [0,1] = 0.25
        // Monte Carlo in multiple dimensions has higher variance
        let result = monte_carlo_nd(|x: &[f64]| x[0] * x[1], &[(0.0, 1.0), (0.0, 1.0)], 100000);
        assert!(
            (result.value - 0.25).abs() < 0.1,
            "Monte Carlo 2D integral {} too far from 0.25",
            result.value
        );
    }

    #[test]
    fn test_gauss_legendre_5() {
        // Integral of x^4 from -1 to 1 = 2/5
        // 5-point Gauss-Legendre is exact for polynomials up to degree 9
        let result = gauss_legendre(|x: f64| x.powi(4), -1.0, 1.0, 5);
        assert!((result - 0.4).abs() < 1e-14);
    }

    #[test]
    fn test_dblquad_constant() {
        // Integral of 1 over [0,1] x [0,1] = 1
        let result = dblquad(|_x: f64, _y: f64| 1.0, 0.0, 1.0, 0.0, 1.0, 10, 10);
        assert!((result - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_dblquad_product() {
        // Integral of x*y over [0,1] x [0,1] = 0.25
        let result = dblquad(|x: f64, y: f64| x * y, 0.0, 1.0, 0.0, 1.0, 10, 10);
        assert!((result - 0.25).abs() < 1e-4);
    }

    #[test]
    fn test_cumtrapz() {
        let y = vec![0.0, 1.0, 2.0, 3.0];
        let dx = 1.0f64;
        let result = cumtrapz(&y, dx);

        assert_eq!(result.len(), 4);
        assert_eq!(result[0], 0.0);
        assert!((result[1] - 0.5).abs() < 1e-10);
        assert!((result[2] - 2.0).abs() < 1e-10);
        assert!((result[3] - 4.5).abs() < 1e-10);
    }

    #[test]
    fn test_quad_adaptive_error_estimate() {
        let result = quad_adaptive(
            &|x: f64| x.sin(),
            0.0,
            std::f64::consts::PI,
            &QuadConfig::default(),
        )
        .expect("quad_adaptive should succeed");

        assert!((result.value - 2.0).abs() < 1e-10);
        assert!(result.error < 1e-10);
        assert!(result.neval > 0);
    }
}