peroxide/complex/
integral.rs

1use crate::complex::C64;
2use crate::numerical::integral::{GKIntegrable, GLKIntegrable, NCIntegrable};
3use crate::structure::polynomial::{lagrange_polynomial, Calculus, Polynomial};
4
5// Newton Cotes Quadrature for Complex Functions of one Real Variable
6impl NCIntegrable for C64 {
7    type NodeY = (Vec<f64>, Vec<f64>);
8    type NCPolynomial = (Polynomial, Polynomial);
9
10    fn compute_node_y<F>(f: F, node_x: &[f64]) -> Self::NodeY
11    where
12        F: Fn(f64) -> Self,
13    {
14        node_x
15            .iter()
16            .map(|x| {
17                let z = f(*x);
18                (z.re, z.im)
19            })
20            .unzip()
21    }
22
23    fn compute_polynomial(node_x: &[f64], node_y: &Self::NodeY) -> Self::NCPolynomial {
24        (
25            lagrange_polynomial(node_x.to_vec(), node_y.0.to_vec()),
26            lagrange_polynomial(node_x.to_vec(), node_y.1.to_vec()),
27        )
28    }
29
30    fn integrate_polynomial(p: &Self::NCPolynomial) -> Self::NCPolynomial {
31        (p.0.integral(), p.1.integral())
32    }
33
34    fn evaluate_polynomial(p: &Self::NCPolynomial, x: f64) -> Self {
35        p.0.eval(x) + C64::I * p.1.eval(x)
36    }
37}
38
39// Gauss Lagrange and Kronrod Quadrature for Complex Functions of one Real Variable
40impl GLKIntegrable for C64 {
41    const ZERO: Self = C64::ZERO;
42}
43
44// Gauss Kronrod Quadrature for Complex Functions of one Real Variable
45impl GKIntegrable for C64 {
46    fn is_finite(&self) -> bool {
47        C64::is_finite(*self)
48    }
49
50    fn gk_norm(&self) -> f64 {
51        self.norm()
52    }
53}