picard/
error.rs

1// src/error.rs
2
3//! Error types for the Picard crate.
4
5use std::fmt;
6
7/// Errors that can occur during PICARD computation.
8#[derive(Debug, Clone)]
9pub enum PicardError {
10    /// Algorithm did not converge within the maximum number of iterations.
11    NotConverged {
12        /// Final gradient norm achieved.
13        gradient_norm: f64,
14        /// Requested tolerance.
15        tolerance: f64,
16        /// Number of iterations performed.
17        iterations: usize,
18    },
19
20    /// Input dimensions are invalid.
21    InvalidDimensions {
22        /// Description of the dimension error.
23        message: String,
24    },
25
26    /// A singular matrix was encountered during computation.
27    SingularMatrix,
28
29    /// General computation error.
30    ComputationError {
31        /// Description of what went wrong.
32        message: String,
33    },
34
35    /// Invalid configuration parameter.
36    InvalidConfig {
37        /// Name of the invalid parameter.
38        parameter: String,
39        /// Description of why it's invalid.
40        message: String,
41    },
42}
43
44impl fmt::Display for PicardError {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            PicardError::NotConverged {
48                gradient_norm,
49                tolerance,
50                iterations,
51            } => {
52                write!(
53                    f,
54                    "PICARD did not converge after {} iterations. \
55                     Final gradient norm: {:.4e}, requested tolerance: {:.4e}. \
56                     Consider increasing max_iter or tolerance.",
57                    iterations, gradient_norm, tolerance
58                )
59            }
60            PicardError::InvalidDimensions { message } => {
61                write!(f, "Invalid dimensions: {}", message)
62            }
63            PicardError::SingularMatrix => {
64                write!(f, "Singular matrix encountered during computation")
65            }
66            PicardError::ComputationError { message } => {
67                write!(f, "Computation error: {}", message)
68            }
69            PicardError::InvalidConfig { parameter, message } => {
70                write!(f, "Invalid configuration for '{}': {}", parameter, message)
71            }
72        }
73    }
74}
75
76impl std::error::Error for PicardError {}
77
78/// Convenience type alias for Results with PicardError.
79pub type Result<T> = std::result::Result<T, PicardError>;