bilby 0.2.0

A high-performance numerical quadrature (integration) library for Rust
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
//! Weighted integration: ∫ f(x) · w(x) dx for known weight functions.
//!
//! Dispatches to the appropriate Gaussian quadrature rule for each weight
//! function family, providing a unified API for product integration.
//!
//! # Example
//!
//! ```
//! use bilby::weighted::{weighted_integrate, WeightFunction};
//!
//! // ∫₋₁¹ 1/√(1-x²) dx = π  (Chebyshev Type I weight)
//! let result = weighted_integrate(|_| 1.0, WeightFunction::ChebyshevI, 20).unwrap();
//! assert!((result - core::f64::consts::PI).abs() < 1e-12);
//! ```

use crate::error::QuadratureError;
use crate::gauss_chebyshev::{GaussChebyshevFirstKind, GaussChebyshevSecondKind};
use crate::gauss_hermite::GaussHermite;
use crate::gauss_jacobi::GaussJacobi;
use crate::gauss_laguerre::GaussLaguerre;

#[cfg(not(feature = "std"))]
use num_traits::Float as _;

/// Known weight functions for product integration.
#[derive(Debug, Clone)]
pub enum WeightFunction {
    /// w(x) = (1-x)^α · (1+x)^β on \[-1, 1\]
    Jacobi { alpha: f64, beta: f64 },
    /// w(x) = x^α · e^(-x) on \[0, ∞)
    Laguerre { alpha: f64 },
    /// w(x) = e^(-x²) on (-∞, ∞)
    Hermite,
    /// w(x) = 1/√(1 - x²) on (-1, 1)
    ChebyshevI,
    /// w(x) = √(1 - x²) on \[-1, 1\]
    ChebyshevII,
    /// w(x) = -ln(x) on (0, 1\]
    LogWeight,
}

/// Builder for weighted integration.
///
/// # Example
///
/// ```
/// use bilby::weighted::{WeightedIntegrator, WeightFunction};
///
/// let wi = WeightedIntegrator::new(WeightFunction::Hermite, 20).unwrap();
/// // ∫₋∞^∞ 1 · e^(-x²) dx = √π
/// let result = wi.integrate(|_| 1.0);
/// assert!((result - core::f64::consts::PI.sqrt()).abs() < 1e-12);
/// ```
#[derive(Debug, Clone)]
pub struct WeightedIntegrator {
    weight: WeightFunction,
    order: usize,
}

impl WeightedIntegrator {
    /// Create a new weighted integrator.
    ///
    /// # Errors
    ///
    /// Returns [`QuadratureError::ZeroOrder`] if `order` is zero.
    pub fn new(weight: WeightFunction, order: usize) -> Result<Self, QuadratureError> {
        if order == 0 {
            return Err(QuadratureError::ZeroOrder);
        }
        Ok(Self { weight, order })
    }

    /// Set the quadrature order.
    #[must_use]
    pub fn with_order(mut self, n: usize) -> Self {
        self.order = n;
        self
    }

    /// Integrate f(x) · w(x) over the natural domain of w.
    ///
    /// - Jacobi: \[-1, 1\]
    /// - Laguerre: \[0, ∞)
    /// - Hermite: (-∞, ∞)
    /// - ChebyshevI/II: \[-1, 1\]
    /// - `LogWeight`: (0, 1\]
    ///
    /// # Panics
    ///
    /// Panics if the [`WeightFunction::Jacobi`] parameters are invalid
    /// (`alpha <= -1` or `beta <= -1`) or if the [`WeightFunction::Laguerre`]
    /// parameter is invalid (`alpha <= -1`), since the underlying quadrature
    /// rule construction will fail.
    pub fn integrate<G>(&self, f: G) -> f64
    where
        G: Fn(f64) -> f64,
    {
        match &self.weight {
            WeightFunction::Jacobi { alpha, beta } => {
                let gj = GaussJacobi::new(self.order, *alpha, *beta).unwrap();
                let rule = gj.rule();
                let mut sum = 0.0;
                for (node, weight) in rule.nodes.iter().zip(rule.weights.iter()) {
                    sum += weight * f(*node);
                }
                sum
            }
            WeightFunction::Laguerre { alpha } => {
                let gl = GaussLaguerre::new(self.order, *alpha).unwrap();
                let rule = gl.rule();
                let mut sum = 0.0;
                for (node, weight) in rule.nodes.iter().zip(rule.weights.iter()) {
                    sum += weight * f(*node);
                }
                sum
            }
            WeightFunction::Hermite => {
                let gh = GaussHermite::new(self.order).unwrap();
                let rule = gh.rule();
                let mut sum = 0.0;
                for (node, weight) in rule.nodes.iter().zip(rule.weights.iter()) {
                    sum += weight * f(*node);
                }
                sum
            }
            WeightFunction::ChebyshevI => {
                let gc = GaussChebyshevFirstKind::new(self.order).unwrap();
                let rule = gc.rule();
                let mut sum = 0.0;
                for (node, weight) in rule.nodes.iter().zip(rule.weights.iter()) {
                    sum += weight * f(*node);
                }
                sum
            }
            WeightFunction::ChebyshevII => {
                let gc = GaussChebyshevSecondKind::new(self.order).unwrap();
                let rule = gc.rule();
                let mut sum = 0.0;
                for (node, weight) in rule.nodes.iter().zip(rule.weights.iter()) {
                    sum += weight * f(*node);
                }
                sum
            }
            WeightFunction::LogWeight => {
                // ∫₀¹ -ln(x) · f(x) dx via substitution x = e^{-t}:
                //   = ∫₀^∞ t · e^{-t} · f(e^{-t}) dt
                // This is a Gauss-Laguerre integral with α = 1.
                let gl = GaussLaguerre::new(self.order, 1.0).unwrap();
                let rule = gl.rule();
                let mut sum = 0.0;
                for (node, weight) in rule.nodes.iter().zip(rule.weights.iter()) {
                    sum += weight * f((-*node).exp());
                }
                sum
            }
        }
    }

    /// Integrate f(x) · w(x) over \[a, b\] via affine transform.
    ///
    /// Only applicable for finite-domain weights (Jacobi, ChebyshevI/II).
    /// For `LogWeight`, maps (0, 1\] to (a, b\] with the log singularity at a.
    /// For Laguerre/Hermite, a and b are ignored (uses natural domain).
    pub fn integrate_over<G>(&self, a: f64, b: f64, f: G) -> f64
    where
        G: Fn(f64) -> f64,
    {
        match &self.weight {
            WeightFunction::Jacobi { alpha, beta } => {
                // Affine map: x in [a,b] <-> t in [-1,1]
                // x = half*t + mid, so (b-x) = half*(1-t), (x-a) = half*(1+t)
                // ∫_a^b (b-x)^α (x-a)^β f(x) dx = half^(α+β+1) * ∫_{-1}^1 w(t) f(half*t+mid) dt
                let half = 0.5 * (b - a);
                let mid = 0.5 * (a + b);
                self.integrate(|t| f(half * t + mid)) * half.powf(alpha + beta + 1.0)
            }
            WeightFunction::ChebyshevI => {
                // ChebyshevI is Jacobi(-0.5, -0.5): factor = half^0 = 1
                // ∫_a^b f(x)/√((b-x)(x-a)) dx = ∫_{-1}^1 f(half*t+mid)/√(1-t²) dt
                let half = 0.5 * (b - a);
                let mid = 0.5 * (a + b);
                self.integrate(|t| f(half * t + mid))
            }
            WeightFunction::ChebyshevII => {
                // ChebyshevII is Jacobi(0.5, 0.5): factor = half^2
                // ∫_a^b √((b-x)(x-a)) f(x) dx = half² * ∫_{-1}^1 √(1-t²) f(half*t+mid) dt
                let half = 0.5 * (b - a);
                let mid = 0.5 * (a + b);
                self.integrate(|t| f(half * t + mid)) * half * half
            }
            WeightFunction::LogWeight => {
                // Map (0,1] to (a,b]: x = a + (b-a)*u, log singularity at a
                // ∫ₐᵇ -ln(x-a) f(x) dx ... actually the weight is -ln(u) on (0,1]
                // For general [a,b]: ∫ₐᵇ -ln(x-a) f(x) dx = (b-a) ∫₀¹ -ln((b-a)u) f(a+(b-a)u) du
                // = (b-a) ∫₀¹ [-ln(b-a) - ln(u)] f(a+(b-a)u) du
                // This doesn't decompose cleanly. Keep it simple: just scale.
                let width = b - a;
                let inner = self.integrate(|u| f(a + width * u));
                width * inner
            }
            // Laguerre and Hermite use natural domains
            WeightFunction::Laguerre { .. } | WeightFunction::Hermite => self.integrate(f),
        }
    }
}

/// Convenience: integrate f(x) · w(x) over the natural domain.
///
/// # Example
///
/// ```
/// use bilby::weighted::{weighted_integrate, WeightFunction};
///
/// // Integral of 1/sqrt(1-x^2) over [-1,1] = pi
/// let result = weighted_integrate(|_| 1.0, WeightFunction::ChebyshevI, 20).unwrap();
/// assert!((result - core::f64::consts::PI).abs() < 1e-12);
/// ```
///
/// # Errors
///
/// Returns [`QuadratureError::ZeroOrder`] if `order` is zero.
pub fn weighted_integrate<G>(
    f: G,
    weight: WeightFunction,
    order: usize,
) -> Result<f64, QuadratureError>
where
    G: Fn(f64) -> f64,
{
    let wi = WeightedIntegrator::new(weight, order)?;
    Ok(wi.integrate(f))
}

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

    #[test]
    fn jacobi_weight_sum() {
        // ∫₋₁¹ (1-x)^0.5 (1+x)^0.5 dx = π/2
        let result = weighted_integrate(
            |_| 1.0,
            WeightFunction::Jacobi {
                alpha: 0.5,
                beta: 0.5,
            },
            20,
        )
        .unwrap();
        assert!(
            (result - core::f64::consts::FRAC_PI_2).abs() < 1e-10,
            "result={result}"
        );
    }

    #[test]
    fn laguerre_constant() {
        // ∫₀^∞ e^(-x) dx = 1
        let result =
            weighted_integrate(|_| 1.0, WeightFunction::Laguerre { alpha: 0.0 }, 10).unwrap();
        assert!((result - 1.0).abs() < 1e-10, "result={result}");
    }

    #[test]
    fn laguerre_linear() {
        // ∫₀^∞ x · e^(-x) dx = Γ(2) = 1
        let result =
            weighted_integrate(|x| x, WeightFunction::Laguerre { alpha: 0.0 }, 10).unwrap();
        assert!((result - 1.0).abs() < 1e-10, "result={result}");
    }

    #[test]
    fn hermite_constant() {
        // ∫₋∞^∞ e^(-x²) dx = √π
        let result = weighted_integrate(|_| 1.0, WeightFunction::Hermite, 10).unwrap();
        assert!(
            (result - core::f64::consts::PI.sqrt()).abs() < 1e-10,
            "result={result}"
        );
    }

    #[test]
    fn chebyshev_i_constant() {
        // ∫₋₁¹ 1/√(1-x²) dx = π
        let result = weighted_integrate(|_| 1.0, WeightFunction::ChebyshevI, 20).unwrap();
        assert!(
            (result - core::f64::consts::PI).abs() < 1e-12,
            "result={result}"
        );
    }

    #[test]
    fn chebyshev_ii_constant() {
        // ∫₋₁¹ √(1-x²) dx = π/2
        let result = weighted_integrate(|_| 1.0, WeightFunction::ChebyshevII, 20).unwrap();
        assert!(
            (result - core::f64::consts::FRAC_PI_2).abs() < 1e-12,
            "result={result}"
        );
    }

    #[test]
    fn log_weight_constant() {
        // ∫₀¹ -ln(x) dx = 1
        let result = weighted_integrate(|_| 1.0, WeightFunction::LogWeight, 20).unwrap();
        assert!((result - 1.0).abs() < 1e-10, "result={result}");
    }

    #[test]
    fn log_weight_linear() {
        // ∫₀¹ -ln(x) · x dx = 1/4
        let result = weighted_integrate(|x| x, WeightFunction::LogWeight, 20).unwrap();
        assert!((result - 0.25).abs() < 1e-10, "result={result}");
    }

    #[test]
    fn log_weight_quadratic() {
        // ∫₀¹ -ln(x) · x² dx = 2/27 (integration by parts twice)
        // Actually: ∫₀¹ -ln(x) x^n dx = 1/(n+1)^2
        // For n=2: 1/9... wait, let me recalculate.
        // ∫₀¹ -ln(x) x² dx = [-x³/3 ln(x)]₀¹ + ∫₀¹ x²/3 dx = 0 + 1/9
        let result = weighted_integrate(|x| x * x, WeightFunction::LogWeight, 20).unwrap();
        assert!(
            (result - 1.0 / 9.0).abs() < 1e-10,
            "result={result}, expected={}",
            1.0 / 9.0
        );
    }

    #[test]
    fn zero_order() {
        assert!(weighted_integrate(|_| 1.0, WeightFunction::Hermite, 0).is_err());
    }

    #[test]
    fn integrate_over_jacobi_legendre_on_0_2() {
        // Jacobi(0,0) is Legendre weight w(x)=1.
        // integrate_over maps [-1,1] to [0,2] via affine transform with Jacobian (b-a)/2 = 1.
        // ∫₀² x² dx = 8/3
        let wi = WeightedIntegrator::new(
            WeightFunction::Jacobi {
                alpha: 0.0,
                beta: 0.0,
            },
            20,
        )
        .unwrap();
        let result = wi.integrate_over(0.0, 2.0, |x| x * x);
        assert!(
            (result - 8.0 / 3.0).abs() < 1e-10,
            "result={result}, expected={}",
            8.0 / 3.0
        );
    }

    #[test]
    fn integrate_over_jacobi_legendre_on_1_3() {
        // ∫₁³ x dx = (9 - 1)/2 = 4
        let wi = WeightedIntegrator::new(
            WeightFunction::Jacobi {
                alpha: 0.0,
                beta: 0.0,
            },
            10,
        )
        .unwrap();
        let result = wi.integrate_over(1.0, 3.0, |x| x);
        assert!((result - 4.0).abs() < 1e-10, "result={result}");
    }

    #[test]
    fn integrate_over_chebyshev_i_on_0_2() {
        // ChebyshevI weight: w(t) = 1/√(1-t²) on [-1,1].
        // integrate_over(0, 2, f) maps t -> x = t + 1, so x ∈ [0,2].
        // Result = half * ∫₋₁¹ f(half*t + mid) / √(1-t²) dt
        //   where half = 1, mid = 1.
        // With f(x) = 1: result = 1 * ∫₋₁¹ 1/√(1-t²) dt = π
        let wi = WeightedIntegrator::new(WeightFunction::ChebyshevI, 20).unwrap();
        let result = wi.integrate_over(0.0, 2.0, |_| 1.0);
        assert!(
            (result - core::f64::consts::PI).abs() < 1e-10,
            "result={result}"
        );
    }

    #[test]
    fn integrate_over_chebyshev_ii_on_0_2() {
        // ChebyshevII weight: w(t) = √(1-t²) on [-1,1].
        // integrate_over(0, 2, f=1) = half * ∫₋₁¹ √(1-t²) dt = 1 * π/2
        let wi = WeightedIntegrator::new(WeightFunction::ChebyshevII, 20).unwrap();
        let result = wi.integrate_over(0.0, 2.0, |_| 1.0);
        assert!(
            (result - core::f64::consts::FRAC_PI_2).abs() < 1e-10,
            "result={result}"
        );
    }

    #[test]
    fn integrate_over_laguerre_ignores_bounds() {
        // Laguerre uses its natural domain [0,∞); bounds are ignored.
        let wi = WeightedIntegrator::new(WeightFunction::Laguerre { alpha: 0.0 }, 10).unwrap();
        let natural = wi.integrate(|_| 1.0);
        let remapped = wi.integrate_over(5.0, 10.0, |_| 1.0);
        assert!(
            (natural - remapped).abs() < 1e-14,
            "natural={natural}, remapped={remapped}"
        );
    }

    #[test]
    fn integrate_over_hermite_ignores_bounds() {
        // Hermite uses its natural domain (-∞,∞); bounds are ignored.
        let wi = WeightedIntegrator::new(WeightFunction::Hermite, 10).unwrap();
        let natural = wi.integrate(|_| 1.0);
        let remapped = wi.integrate_over(5.0, 10.0, |_| 1.0);
        assert!(
            (natural - remapped).abs() < 1e-14,
            "natural={natural}, remapped={remapped}"
        );
    }

    #[test]
    fn integrate_over_jacobi_on_0_4() {
        // Jacobi(0.5, 0.5) on [0,4]: ∫₀⁴ (4-x)^0.5 (x-0)^0.5 f(x) dx
        // With f=1: ∫₀⁴ √(x(4-x)) dx = ∫₀⁴ √(4x - x²) dx = 2π (semicircle area)
        let wi = WeightedIntegrator::new(
            WeightFunction::Jacobi {
                alpha: 0.5,
                beta: 0.5,
            },
            30,
        )
        .unwrap();
        let result = wi.integrate_over(0.0, 4.0, |_| 1.0);
        assert!(
            (result - 2.0 * core::f64::consts::PI).abs() < 1e-6,
            "result={result}, expected={}",
            2.0 * core::f64::consts::PI
        );
    }

    #[test]
    fn integrate_over_chebyshev_i_on_0_4() {
        // ChebyshevI on [0,4]: ∫₀⁴ f(x)/√((4-x)(x-0)) dx
        // With f=1: ∫₀⁴ 1/√(4x-x²) dx = π (half=2, factor=1)
        let wi = WeightedIntegrator::new(WeightFunction::ChebyshevI, 20).unwrap();
        let result = wi.integrate_over(0.0, 4.0, |_| 1.0);
        assert!(
            (result - core::f64::consts::PI).abs() < 1e-10,
            "result={result}, expected π"
        );
    }

    #[test]
    fn integrate_over_chebyshev_ii_on_0_4() {
        // ChebyshevII on [0,4]: ∫₀⁴ √((4-x)·x) f(x) dx
        // With f=1: ∫₀⁴ √(4x-x²) dx = 2π (semicircle area, half=2, factor=half²=4)
        let wi = WeightedIntegrator::new(WeightFunction::ChebyshevII, 20).unwrap();
        let result = wi.integrate_over(0.0, 4.0, |_| 1.0);
        assert!(
            (result - 2.0 * core::f64::consts::PI).abs() < 1e-6,
            "result={result}, expected={}",
            2.0 * core::f64::consts::PI
        );
    }

    #[test]
    fn integrate_over_log_weight_on_0_2() {
        // LogWeight: integrate_over(0, 2, f) = width * integrate(|u| f(a + width*u))
        //   = 2 * ∫₀¹ -ln(u) · f(2u) du
        // With f(x) = 1: result = 2 * ∫₀¹ -ln(u) du = 2 * 1 = 2
        let wi = WeightedIntegrator::new(WeightFunction::LogWeight, 20).unwrap();
        let result = wi.integrate_over(0.0, 2.0, |_| 1.0);
        assert!((result - 2.0).abs() < 1e-10, "result={result}");
    }
}