scirs2_sparse/ml_preconditioner/
types.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[non_exhaustive]
6pub enum PreconditionerType {
7 Jacobi,
9 SSOR,
11 ILU0,
13 IC0,
15 AMG,
17 SPAI,
19 Polynomial,
21 None,
23}
24
25impl std::fmt::Display for PreconditionerType {
26 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27 match self {
28 Self::Jacobi => write!(f, "Jacobi"),
29 Self::SSOR => write!(f, "SSOR"),
30 Self::ILU0 => write!(f, "ILU(0)"),
31 Self::IC0 => write!(f, "IC(0)"),
32 Self::AMG => write!(f, "AMG"),
33 Self::SPAI => write!(f, "SPAI"),
34 Self::Polynomial => write!(f, "Polynomial"),
35 Self::None => write!(f, "None"),
36 #[allow(unreachable_patterns)]
37 _ => write!(f, "Unknown"),
38 }
39 }
40}
41
42#[derive(Debug, Clone)]
44pub struct MatrixFeatures {
45 pub n: usize,
47 pub nnz: usize,
49 pub density: f64,
51 pub max_row_nnz: usize,
53 pub mean_row_nnz: f64,
55 pub bandwidth: usize,
57 pub bandwidth_ratio: f64,
59 pub cond_estimate: f64,
61 pub spectral_radius: f64,
63 pub diag_dominance: f64,
65 pub symmetry_measure: f64,
67 pub has_positive_diagonal: bool,
69}
70
71#[derive(Debug, Clone)]
73pub struct SelectionConfig {
74 pub use_cost_model: bool,
76 pub max_features: usize,
78}
79
80impl Default for SelectionConfig {
81 fn default() -> Self {
82 Self {
83 use_cost_model: true,
84 max_features: 12,
85 }
86 }
87}
88
89#[derive(Debug, Clone)]
91pub struct SelectionResult {
92 pub recommended: PreconditionerType,
94 pub confidence: f64,
96 pub all_scores: Vec<(PreconditionerType, f64)>,
98 pub features: MatrixFeatures,
100}
101
102#[derive(Debug, Clone)]
104pub struct CostEstimate {
105 pub setup_cost: f64,
107 pub per_iteration_cost: f64,
109 pub estimated_iterations: usize,
111 pub total_cost: f64,
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118
119 #[test]
120 fn test_preconditioner_type_display() {
121 assert_eq!(format!("{}", PreconditionerType::Jacobi), "Jacobi");
122 assert_eq!(format!("{}", PreconditionerType::ILU0), "ILU(0)");
123 assert_eq!(format!("{}", PreconditionerType::None), "None");
124 }
125
126 #[test]
127 fn test_selection_config_default() {
128 let cfg = SelectionConfig::default();
129 assert!(cfg.use_cost_model);
130 assert_eq!(cfg.max_features, 12);
131 }
132
133 #[test]
134 fn test_preconditioner_type_eq() {
135 assert_eq!(PreconditionerType::AMG, PreconditionerType::AMG);
136 assert_ne!(PreconditionerType::Jacobi, PreconditionerType::SSOR);
137 }
138}