From f6b62431aaed1c9c0eec0d8787e427abc21555cc Mon Sep 17 00:00:00 2001 From: imrn99 <95699343+imrn99@users.noreply.github.com> Date: Mon, 7 Oct 2024 07:31:25 +0200 Subject: [PATCH] add new errors --- integraal/src/structure/definitions.rs | 8 +++- integraal/src/structure/implementations.rs | 48 +++++++++++++--------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/integraal/src/structure/definitions.rs b/integraal/src/structure/definitions.rs index 075582d..99207c0 100644 --- a/integraal/src/structure/definitions.rs +++ b/integraal/src/structure/definitions.rs @@ -9,10 +9,14 @@ use crate::{ComputeMethod, DomainDescriptor, FunctionDescriptor, Scalar}; /// Integral error #[derive(Debug, Copy, Clone, PartialEq)] pub enum IntegraalError { - /// One or more parameters are missing. - MissingParameters(&'static str), + /// Some parameters do not fit the requirements of the computation method. + BadParameters(&'static str), /// Specified parameters are conflicting or ambiguous. InconsistentParameters(&'static str), + /// One or more parameters are missing. + MissingParameters(&'static str), + /// A given method isn't implemented for the specified parameters (e.g. due to requirements). + Unimplemented(&'static str), } /// Main integral computation structure diff --git a/integraal/src/structure/implementations.rs b/integraal/src/structure/implementations.rs index 20039d3..e157547 100644 --- a/integraal/src/structure/implementations.rs +++ b/integraal/src/structure/implementations.rs @@ -145,13 +145,17 @@ fn values_explicit_arm( }) .sum() } - #[cfg(feature = "boole")] // FIXME: replace by an error + #[cfg(feature = "boole")] ComputeMethod::Boole => { - unimplemented!("E: Boole's method isn't implemented for non-uniform domains"); + return Err(IntegraalError::Unimplemented( + "Boole's method isn't implemented for non-uniform domains", + )); } - #[cfg(feature = "romberg")] // FIXME: replace by an error + #[cfg(feature = "romberg")] ComputeMethod::Romberg { .. } => { - unimplemented!("E: Romberg's method isn't implemented for non-uniform domains"); + return Err(IntegraalError::Unimplemented( + "Romberg's method isn't implemented for non-uniform domains", + )); } #[cfg(feature = "montecarlo")] ComputeMethod::MonteCarlo { n_sample: _ } => { @@ -214,12 +218,12 @@ fn values_uniform_arm( .sum() } ComputeMethod::Boole => { - // FIXME: replace with an error - assert_eq!( - *n_step % 4, - 0, - "E: domain should be divided into a multiple of 4 segments for Boole's method" - ); + if *n_step % 4 != 0 { + return Err(IntegraalError::BadParameters( + "domain should be divided into a multiple of 4 segments for Boole's method", + )); + } + let c = X::from(2.0 / 45.0).unwrap() * *step; let m1 = X::from(7.0).unwrap() * (vals[0] + vals[*n_step - 1]); @@ -326,13 +330,17 @@ fn closure_explicit_arm( }) .sum() } - #[cfg(feature = "boole")] // FIXME: replace by an error + #[cfg(feature = "boole")] ComputeMethod::Boole => { - unimplemented!("E: Boole's method isn't implemented for non-uniform domains"); + return Err(IntegraalError::Unimplemented( + "Boole's method isn't implemented for non-uniform domains", + )); } - #[cfg(feature = "romberg")] // FIXME: replace by an error + #[cfg(feature = "romberg")] ComputeMethod::Romberg { .. } => { - unimplemented!("E: Romberg's method isn't implemented for non-uniform domains"); + return Err(IntegraalError::Unimplemented( + "Romberg's method isn't implemented for non-uniform domains", + )); } #[cfg(feature = "montecarlo")] ComputeMethod::MonteCarlo { n_sample: _ } => { @@ -401,12 +409,12 @@ fn closure_uniform_arm( .sum() } ComputeMethod::Boole => { - // FIXME: replace with an error - assert_eq!( - *n_step % 4, - 0, - "E: domain should be divided into a multiple of 4 segments for Boole's method" - ); + if *n_step % 4 != 0 { + return Err(IntegraalError::BadParameters( + "domain should be divided into a multiple of 4 segments for Boole's method", + )); + } + let c = X::from(2.0 / 45.0).unwrap() * *step; let m1 = X::from(7.0).unwrap()