Skip to main content

trueno_sparse/
error.rs

1//! Error types for sparse matrix operations.
2
3/// Errors that can occur during sparse matrix construction or operations.
4#[derive(Debug, Clone, thiserror::Error)]
5pub enum SparseError {
6    /// CSR offsets array has wrong length (expected rows + 1).
7    #[error("CSR offsets length {actual} != rows + 1 ({expected})")]
8    InvalidOffsetsLength {
9        /// Actual length of offsets array.
10        actual: usize,
11        /// Expected length (rows + 1).
12        expected: usize,
13    },
14
15    /// CSR offsets are not monotonically non-decreasing.
16    #[error("CSR offsets not monotonic at index {index}: offsets[{index}]={value} > offsets[{next_index}]={next_value}")]
17    NonMonotonicOffsets {
18        /// Index where violation occurs.
19        index: usize,
20        /// Value at the violating index.
21        value: u32,
22        /// Next index.
23        next_index: usize,
24        /// Value at next index.
25        next_value: u32,
26    },
27
28    /// CSR offsets[0] must be 0.
29    #[error("CSR offsets[0] = {value}, expected 0")]
30    NonZeroFirstOffset {
31        /// Actual value of offsets[0].
32        value: u32,
33    },
34
35    /// CSR offsets[rows] must equal nnz.
36    #[error("CSR offsets[rows] = {offset_last} != nnz ({nnz})")]
37    OffsetNnzMismatch {
38        /// Value of offsets[rows].
39        offset_last: u32,
40        /// Expected nnz (length of values/col_indices).
41        nnz: usize,
42    },
43
44    /// Column index out of bounds.
45    #[error("Column index {col} >= cols ({cols}) at position {position}")]
46    ColumnOutOfBounds {
47        /// The out-of-bounds column index.
48        col: u32,
49        /// Number of columns.
50        cols: usize,
51        /// Position in col_indices array.
52        position: usize,
53    },
54
55    /// col_indices and values length mismatch.
56    #[error("col_indices length {col_len} != values length {val_len}")]
57    LengthMismatch {
58        /// Length of col_indices.
59        col_len: usize,
60        /// Length of values.
61        val_len: usize,
62    },
63
64    /// COO arrays have mismatched lengths.
65    #[error("COO arrays have mismatched lengths: row_indices={row_len}, col_indices={col_len}, values={val_len}")]
66    CooLengthMismatch {
67        /// Length of row_indices.
68        row_len: usize,
69        /// Length of col_indices.
70        col_len: usize,
71        /// Length of values.
72        val_len: usize,
73    },
74
75    /// COO row index out of bounds.
76    #[error("Row index {row} >= rows ({rows}) at position {position}")]
77    RowOutOfBounds {
78        /// The out-of-bounds row index.
79        row: u32,
80        /// Number of rows.
81        rows: usize,
82        /// Position in row_indices array.
83        position: usize,
84    },
85
86    /// Dimension mismatch in SpMV.
87    #[error("SpMV dimension mismatch: matrix cols={matrix_cols}, x length={x_len}")]
88    SpMVDimensionMismatch {
89        /// Number of columns in the matrix.
90        matrix_cols: usize,
91        /// Length of x vector.
92        x_len: usize,
93    },
94
95    /// Output dimension mismatch in SpMV.
96    #[error("SpMV output dimension mismatch: matrix rows={matrix_rows}, y length={y_len}")]
97    SpMVOutputDimensionMismatch {
98        /// Number of rows in the matrix.
99        matrix_rows: usize,
100        /// Length of y vector.
101        y_len: usize,
102    },
103}