quad_rs/
result.rs

1use crate::{state::IntegrationState, IntegrableFloat, IntegrationError, IntegrationOutput};
2use nalgebra::ComplexField;
3
4impl<I, O, F> From<IntegrationState<I, O, F>> for IntegrationResult<I, O>
5where
6    O: IntegrationOutput<Float = F> + Clone,
7    I: ComplexField<RealField = F> + Copy,
8    F: IntegrableFloat,
9{
10    fn from(val: IntegrationState<I, O, F>) -> Self {
11        IntegrationResult {
12            number_of_function_evaluations: 0,
13            result: val.integral.clone(),
14            values: if val.accumulate_values {
15                val.into_resolved()
16            } else {
17                None
18            },
19        }
20    }
21}
22
23#[derive(Debug)]
24pub struct Values<I, O>
25where
26    O: IntegrationOutput,
27{
28    /// The nodes at which the integral was evaluated
29    pub points: Vec<I>,
30    /// The weights used to evaluate the integral
31    pub weights: Vec<O::Float>,
32    /// The value of the integrand at `points`
33    pub values: Vec<O>,
34}
35
36#[derive(Debug)]
37pub struct IntegrationResult<I, O>
38where
39    O: IntegrationOutput,
40{
41    number_of_function_evaluations: usize,
42    pub result: Option<O>,
43    pub values: Option<Values<I, O>>,
44}
45
46impl<I, O> IntegrationResult<I, O>
47where
48    O: IntegrationOutput,
49{
50    pub fn result(&self) -> Result<&O, IntegrationError<O>> {
51        match self.result {
52            Some(ref x) => Ok(x),
53            None => Err(IntegrationError::NoSolution),
54        }
55    }
56
57    pub fn with_result(mut self, result: O) -> Self {
58        self.result = Some(result);
59        self
60    }
61
62    pub fn with_number_of_evaluations(mut self, number_of_function_evaluations: usize) -> Self {
63        self.number_of_function_evaluations = number_of_function_evaluations;
64        self
65    }
66}
67
68impl<I, O> Default for IntegrationResult<I, O>
69where
70    O: IntegrationOutput,
71{
72    fn default() -> IntegrationResult<I, O> {
73        IntegrationResult {
74            result: None,
75            values: None,
76            number_of_function_evaluations: 0,
77        }
78    }
79}
80
81impl<I, O> std::fmt::Display for IntegrationResult<I, O>
82where
83    O: IntegrationOutput + std::fmt::Display,
84{
85    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86        if self.result.is_none() {
87            write!(f, "The integrator has no solution")
88        } else {
89            write!(
90                f,
91                "Result: {}, N-evals: {}",
92                self.result.as_ref().unwrap(),
93                self.number_of_function_evaluations,
94            )
95        }
96    }
97}