Skip to main content

tensor4all_tcicore/
error.rs

1//! Error types for tensor4all-tcicore.
2//!
3//! [`MatrixCIError`] enumerates errors that can occur during matrix cross
4//! interpolation operations. All public API functions return
5//! [`Result<T>`](Result) which uses this error type.
6
7use thiserror::Error;
8
9/// Errors that can occur during matrix cross interpolation operations
10///
11/// # Examples
12///
13/// ```
14/// use tensor4all_tcicore::MatrixCIError;
15///
16/// let err = MatrixCIError::DimensionMismatch {
17///     expected_rows: 3,
18///     expected_cols: 3,
19///     actual_rows: 2,
20///     actual_cols: 2,
21/// };
22/// assert!(format!("{err}").contains("Dimension mismatch"));
23///
24/// let err = MatrixCIError::FullRank;
25/// assert!(format!("{err}").contains("full rank"));
26///
27/// // Matching on error variants
28/// match err {
29///     MatrixCIError::FullRank => assert!(true),
30///     _ => panic!("unexpected variant"),
31/// }
32/// ```
33#[derive(Debug, Error)]
34pub enum MatrixCIError {
35    /// Dimension mismatch between matrix and MatrixCI object
36    #[error("Dimension mismatch: expected ({expected_rows}, {expected_cols}), got ({actual_rows}, {actual_cols})")]
37    DimensionMismatch {
38        /// Expected number of rows
39        expected_rows: usize,
40        /// Expected number of columns
41        expected_cols: usize,
42        /// Actual number of rows
43        actual_rows: usize,
44        /// Actual number of columns
45        actual_cols: usize,
46    },
47
48    /// Index out of bounds
49    #[error(
50        "Index out of bounds: ({row}, {col}) is out of bounds for a ({nrows}, {ncols}) matrix"
51    )]
52    IndexOutOfBounds {
53        /// Row index that was out of bounds
54        row: usize,
55        /// Column index that was out of bounds
56        col: usize,
57        /// Number of rows in the matrix
58        nrows: usize,
59        /// Number of columns in the matrix
60        ncols: usize,
61    },
62
63    /// Duplicate pivot row
64    #[error("Cannot add pivot: row {row} already has a pivot")]
65    DuplicatePivotRow {
66        /// Row index that already has a pivot
67        row: usize,
68    },
69
70    /// Duplicate pivot column
71    #[error("Cannot add pivot: column {col} already has a pivot")]
72    DuplicatePivotCol {
73        /// Column index that already has a pivot
74        col: usize,
75    },
76
77    /// Matrix is already full rank
78    #[error("Cannot find a new pivot: matrix is already full rank")]
79    FullRank,
80
81    /// Empty row or column set
82    #[error("Cannot find a new pivot in an empty set of {dimension}")]
83    EmptyIndexSet {
84        /// Description of the empty dimension ("rows" or "columns")
85        dimension: String,
86    },
87
88    /// Invalid argument
89    #[error("Invalid argument: {message}")]
90    InvalidArgument {
91        /// Description of the invalid argument
92        message: String,
93    },
94
95    /// Singular matrix encountered
96    #[error("Singular matrix encountered during decomposition")]
97    SingularMatrix,
98
99    /// Rank deficient matrix
100    #[error("Rank-deficient matrix is not supported for this operation")]
101    RankDeficient,
102
103    /// NaN values encountered
104    #[error("NaN values encountered in {matrix}")]
105    NaNEncountered {
106        /// Name of the matrix where NaN was encountered
107        matrix: String,
108    },
109}
110
111/// Result type for matrix CI operations
112pub type Result<T> = std::result::Result<T, MatrixCIError>;