gauss_quad/chebyshev/
mod.rs

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
//! Numerical integration using the [Gauss-Chebyshev quadrature](https://en.wikipedia.org/wiki/Chebyshev%E2%80%93Gauss_quadrature) rule.
//!
//! This rule can integrate formulas on the form f(x) * (1 - x^2)^`a` on finite intervals, where `a` is either -1/2 or 1/2.

use crate::{Node, Weight, __impl_node_weight_rule};

use core::{f64::consts::PI, fmt};
use std::backtrace::Backtrace;

#[cfg(feature = "rayon")]
use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};

/// A Gauss-Chebyshev quadrature scheme of the first kind.
///
/// Used to integrate functions of the form
/// f(x) / sqrt(1 - x^2) on finite intervals.
///
/// # Example
///
/// ```
/// # use gauss_quad::chebyshev::{GaussChebyshevFirstKind, GaussChebyshevError};
/// # use approx::assert_relative_eq;
/// # use core::f64::consts::PI;
/// let rule = GaussChebyshevFirstKind::new(2)?;
///
/// assert_relative_eq!(rule.integrate(0.0, 2.0, |x| x), PI);
/// # Ok::<(), GaussChebyshevError>(())
/// ```
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GaussChebyshevFirstKind {
    node_weight_pairs: Vec<(Node, Weight)>,
}

impl GaussChebyshevFirstKind {
    /// Create a new `GaussChebyshevFirstKind` rule that can integrate functions of the form f(x) / sqrt(1 - x^2).
    ///
    /// # Errors
    ///
    /// Returns an error if `degree` is less than 2.
    pub fn new(degree: usize) -> Result<Self, GaussChebyshevError> {
        if degree < 2 {
            return Err(GaussChebyshevError::new());
        }

        let n = degree as f64;

        Ok(Self {
            node_weight_pairs: (1..degree + 1)
                .map(|i| ((PI * (2.0 * (i as f64) - 1.0) / (2.0 * n)).cos(), PI / n))
                .collect(),
        })
    }

    fn argument_transformation(x: f64, a: f64, b: f64) -> f64 {
        0.5 * ((b - a) * x + (b + a))
    }

    fn scale_factor(a: f64, b: f64) -> f64 {
        0.5 * (b - a)
    }

    #[cfg(feature = "rayon")]
    /// Same as [`new`](Self::new) but runs in parallel.
    pub fn par_new(degree: usize) -> Result<Self, GaussChebyshevError> {
        if degree < 2 {
            return Err(GaussChebyshevError::new());
        }

        let n = degree as f64;

        Ok(Self {
            node_weight_pairs: (1..degree + 1)
                .into_par_iter()
                .map(|i| ((PI * (2.0 * (i as f64) - 1.0) / (2.0 * n)).cos(), PI / n))
                .collect(),
        })
    }

    /// Returns the value of the integral of the given `integrand` in the inverval \[`a`, `b`\].
    ///
    /// # Example
    ///
    /// ```
    /// # use gauss_quad::chebyshev::{GaussChebyshevFirstKind, GaussChebyshevError};
    /// # use approx::assert_relative_eq;
    /// # use core::f64::consts::PI;
    /// let rule = GaussChebyshevFirstKind::new(2)?;
    ///
    /// assert_relative_eq!(rule.integrate(-1.0, 1.0, |x| 1.5 * x * x - 0.5), PI / 4.0);
    /// # Ok::<(), GaussChebyshevError>(())
    /// ```
    pub fn integrate<F>(&self, a: f64, b: f64, integrand: F) -> f64
    where
        F: Fn(f64) -> f64,
    {
        let result: f64 = self
            .node_weight_pairs
            .iter()
            .map(|(x, w)| integrand(Self::argument_transformation(*x, a, b)) * w)
            .sum();
        result * Self::scale_factor(a, b)
    }

    #[cfg(feature = "rayon")]
    /// Same as [`integrate`](Self::integrate) but runs in parallel.
    pub fn par_integrate<F>(&self, a: f64, b: f64, integrand: F) -> f64
    where
        F: Sync + Fn(f64) -> f64,
    {
        let result: f64 = self
            .node_weight_pairs
            .par_iter()
            .map(|(x, w)| integrand(Self::argument_transformation(*x, a, b)) * w)
            .sum();
        result * Self::scale_factor(a, b)
    }
}

__impl_node_weight_rule! {GaussChebyshevFirstKind, GaussChebyshevFirstKindNodes, GaussChebyshevFirstKindWeights, GaussChebyshevFirstKindIter, GaussChebyshevFirstKindIntoIter}

/// A Gauss-Chebyshev quadrature scheme of the second kind.
///
/// Used to integrate functions of the form
/// f(x) * sqrt(1 - x^2) on finite intervals.
///
/// # Example
///
/// ```
/// # use gauss_quad::chebyshev::{GaussChebyshevSecondKind, GaussChebyshevError};
/// # use approx::assert_relative_eq;
/// # use core::f64::consts::PI;
/// let rule = GaussChebyshevSecondKind::new(2)?;
///
/// assert_relative_eq!(rule.integrate(-1.0, 1.0, |x| x * x), PI / 8.0);
/// # Ok::<(), GaussChebyshevError>(())
/// ```
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GaussChebyshevSecondKind {
    node_weight_pairs: Vec<(Node, Weight)>,
}

impl GaussChebyshevSecondKind {
    /// Create a new `GaussChebyshev` rule that can integrate functions of the form f(x) * sqrt(1 - x^2).
    ///
    /// # Errors
    ///
    /// Returns an error if `degree` is less than 2.
    pub fn new(degree: usize) -> Result<Self, GaussChebyshevError> {
        if degree < 2 {
            return Err(GaussChebyshevError::new());
        }

        let n = degree as f64;

        Ok(Self {
            node_weight_pairs: (1..degree + 1)
                .map(|i| {
                    let over_n_plus_1 = 1.0 / (n + 1.0);
                    let sin_val = (PI * i as f64 * over_n_plus_1).sin();
                    (
                        (PI * i as f64 * over_n_plus_1).cos(),
                        PI * over_n_plus_1 * sin_val * sin_val,
                    )
                })
                .collect(),
        })
    }

    #[cfg(feature = "rayon")]
    /// Same as [`new`](Self::new) but runs in parallel.
    pub fn par_new(degree: usize) -> Result<Self, GaussChebyshevError> {
        if degree < 2 {
            return Err(GaussChebyshevError::new());
        }

        let n = degree as f64;

        Ok(Self {
            node_weight_pairs: (1..degree + 1)
                .into_par_iter()
                .map(|i| {
                    let over_n_plus_1 = 1.0 / (n + 1.0);
                    let sin_val = (PI * i as f64 * over_n_plus_1).sin();
                    (
                        (PI * i as f64 * over_n_plus_1).cos(),
                        PI * over_n_plus_1 * sin_val * sin_val,
                    )
                })
                .collect(),
        })
    }

    fn argument_transformation(x: f64, a: f64, b: f64) -> f64 {
        0.5 * ((b - a) * x + (b + a))
    }

    fn scale_factor(a: f64, b: f64) -> f64 {
        0.5 * (b - a)
    }

    /// Returns the value of the integral of the given `integrand` in the inverval \[`a`, `b`\].
    ///
    /// # Example
    ///
    /// ```
    /// # use gauss_quad::chebyshev::{GaussChebyshevSecondKind, GaussChebyshevError};
    /// # use approx::assert_relative_eq;
    /// # use core::f64::consts::PI;
    /// let rule = GaussChebyshevSecondKind::new(2)?;
    ///
    /// assert_relative_eq!(rule.integrate(-1.0, 1.0, |x| 1.5 * x * x - 0.5), -PI / 16.0);
    /// # Ok::<(), GaussChebyshevError>(())
    /// ```
    pub fn integrate<F>(&self, a: f64, b: f64, integrand: F) -> f64
    where
        F: Fn(f64) -> f64,
    {
        let result: f64 = self
            .node_weight_pairs
            .iter()
            .map(|(x, w)| integrand(Self::argument_transformation(*x, a, b)) * w)
            .sum();
        result * Self::scale_factor(a, b)
    }

    #[cfg(feature = "rayon")]
    /// Same as [`integrate`](Self::integrate) but runs in parallel.
    pub fn par_integrate<F>(&self, a: f64, b: f64, integrand: F) -> f64
    where
        F: Fn(f64) -> f64 + Sync,
    {
        let result: f64 = self
            .node_weight_pairs
            .par_iter()
            .map(|(x, w)| integrand(Self::argument_transformation(*x, a, b)) * w)
            .sum();
        result * Self::scale_factor(a, b)
    }
}

__impl_node_weight_rule! {GaussChebyshevSecondKind, GaussChebyshevSecondKindNodes, GaussChebyshevSecondKindWeights, GaussChebyshevSecondKindIter, GaussChebyshevSecondKindIntoIter}

/// The error returned when attempting to create a [`GaussChebyshevFirstKind`] or [`GaussChebyshevSecondKind`] struct with a degree less than 2.
#[derive(Debug)]
pub struct GaussChebyshevError(Backtrace);

impl GaussChebyshevError {
    pub(crate) fn new() -> Self {
        Self(Backtrace::capture())
    }

    /// Returns a [`Backtrace`] to where the error was created.
    ///
    /// This backtrace is captured with [`Backtrace::capture`], see it for more information about how to make it display information when printed.
    pub fn backtrace(&self) -> &Backtrace {
        &self.0
    }
}

impl fmt::Display for GaussChebyshevError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "the degree must be at least 2")
    }
}

impl std::error::Error for GaussChebyshevError {}

#[cfg(test)]
mod test {
    use approx::assert_abs_diff_eq;

    use super::{GaussChebyshevFirstKind, GaussChebyshevSecondKind};

    use core::f64::consts::PI;

    #[test]
    fn check_error() {
        assert!(GaussChebyshevFirstKind::new(1).is_err());
        assert!(GaussChebyshevSecondKind::new(1).is_err());
    }

    #[test]
    fn check_chebyshev_1st_deg_5() {
        // Source: https://mathworld.wolfram.com/Chebyshev-GaussQuadrature.html
        let ans = [
            (0.5 * (0.5 * (5.0 + f64::sqrt(5.0))).sqrt(), PI / 5.0),
            (0.5 * (0.5 * (5.0 - f64::sqrt(5.0))).sqrt(), PI / 5.0),
            (0.0, PI / 5.0),
            (-0.5 * (0.5 * (5.0 - f64::sqrt(5.0))).sqrt(), PI / 5.0),
            (-0.5 * (0.5 * (5.0 + f64::sqrt(5.0))).sqrt(), PI / 5.0),
        ];

        let rule = GaussChebyshevFirstKind::new(5).unwrap();

        for ((x, w), (x_should, w_should)) in rule.into_iter().zip(ans.into_iter()) {
            assert_abs_diff_eq!(x, x_should);
            assert_abs_diff_eq!(w, w_should);
        }
    }

    #[test]
    fn check_chebyshev_2nd_deg_5() {
        // I couldn't find lists of nodes and weights to compare to. So this function computes
        // them itself with formulas from Wikipedia.

        let deg = 5;
        let rule = GaussChebyshevSecondKind::new(deg).unwrap();
        let deg = deg as f64;

        for (i, (x, w)) in rule.into_iter().enumerate() {
            // Source: https://en.wikipedia.org/wiki/Chebyshev%E2%80%93Gauss_quadrature
            let ii = i as f64 + 1.0;
            let x_should = (ii * PI / (deg + 1.0)).cos();
            let w_should = PI / (deg + 1.0) * (ii * PI / (deg + 1.0)).sin().powi(2);

            assert_abs_diff_eq!(x, x_should);
            assert_abs_diff_eq!(w, w_should);
        }
    }

    #[test]
    fn check_integral_of_line() {
        let rule = GaussChebyshevFirstKind::new(2).unwrap();

        assert_abs_diff_eq!(rule.integrate(0.0, 2.0, |x| x), PI);
    }

    #[test]
    fn check_integral_of_legendre_2() {
        let rule1 = GaussChebyshevFirstKind::new(2).unwrap();
        let rule2 = GaussChebyshevSecondKind::new(2).unwrap();

        fn legendre_2(x: f64) -> f64 {
            1.5 * x * x - 0.5
        }

        assert_abs_diff_eq!(rule1.integrate(-1.0, 1.0, legendre_2), PI / 4.0);
        assert_abs_diff_eq!(rule2.integrate(-1.0, 1.0, legendre_2), -PI / 16.0);
    }

    #[test]
    fn check_integral_of_parabola() {
        let rule = GaussChebyshevSecondKind::new(2).unwrap();

        assert_abs_diff_eq!(rule.integrate(-1.0, 1.0, |x| x * x), PI / 8.0);
    }

    #[cfg(feature = "rayon")]
    #[test]
    fn test_par_integrate() {
        let rule1 = GaussChebyshevFirstKind::par_new(2).unwrap();
        let rule2 = GaussChebyshevSecondKind::par_new(2).unwrap();

        assert_abs_diff_eq!(rule1.par_integrate(0.0, 2.0, |x| x), PI);
        assert_abs_diff_eq!(rule2.par_integrate(-1.0, 1.0, |x| x * x), PI / 8.0);
    }

    #[cfg(feature = "rayon")]
    #[test]
    fn check_par_error() {
        assert!(GaussChebyshevFirstKind::new(0).is_err());
        assert!(GaussChebyshevSecondKind::new(0).is_err());
    }
}