Skip to main content

amari_core/
error.rs

1//! Error types for core geometric algebra operations
2
3#[cfg(feature = "std")]
4use thiserror::Error;
5
6#[cfg(not(feature = "std"))]
7use core::fmt;
8
9/// Error types for core geometric algebra operations
10#[cfg(feature = "std")]
11#[derive(Error, Debug, Clone, PartialEq)]
12pub enum CoreError {
13    /// Invalid dimension for operation
14    #[error("Invalid dimension: expected {expected}, got {actual}")]
15    InvalidDimension { expected: usize, actual: usize },
16
17    /// Division by zero
18    #[error("Division by zero")]
19    DivisionByZero,
20
21    /// Numerical instability detected
22    #[error("Numerical instability detected")]
23    NumericalInstability,
24
25    /// Invalid basis vector index
26    #[error("Invalid basis vector index: {0} (max: {1})")]
27    InvalidBasisIndex(usize, usize),
28
29    /// Matrix is singular (non-invertible)
30    #[error("Matrix is singular and cannot be inverted")]
31    SingularMatrix,
32
33    /// Invalid metric signature
34    #[error("Invalid metric signature: positive {positive} + negative {negative} + zero {zero} != dimension {dimension}")]
35    InvalidSignature {
36        positive: usize,
37        negative: usize,
38        zero: usize,
39        dimension: usize,
40    },
41
42    /// GF(2) dimension mismatch
43    #[error("GF(2) dimension mismatch: expected {expected}, got {got}")]
44    GF2DimensionMismatch { expected: usize, got: usize },
45
46    /// GF(2) matrix is not square
47    #[error("GF(2) matrix is not square: {rows}x{cols}")]
48    GF2NotSquare { rows: usize, cols: usize },
49
50    /// GF(2) index out of bounds
51    #[error("GF(2) index {index} out of bounds for dimension {dim}")]
52    GF2IndexOutOfBounds { index: usize, dim: usize },
53}
54
55/// Error types for core geometric algebra operations (no_std version)
56#[cfg(not(feature = "std"))]
57#[derive(Debug, Clone, PartialEq)]
58pub enum CoreError {
59    /// Invalid dimension for operation
60    InvalidDimension { expected: usize, actual: usize },
61
62    /// Division by zero
63    DivisionByZero,
64
65    /// Numerical instability detected
66    NumericalInstability,
67
68    /// Invalid basis vector index
69    InvalidBasisIndex(usize, usize),
70
71    /// Matrix is singular (non-invertible)
72    SingularMatrix,
73
74    /// Invalid metric signature
75    InvalidSignature {
76        positive: usize,
77        negative: usize,
78        zero: usize,
79        dimension: usize,
80    },
81
82    /// GF(2) dimension mismatch
83    GF2DimensionMismatch { expected: usize, got: usize },
84
85    /// GF(2) matrix is not square
86    GF2NotSquare { rows: usize, cols: usize },
87
88    /// GF(2) index out of bounds
89    GF2IndexOutOfBounds { index: usize, dim: usize },
90}
91
92#[cfg(not(feature = "std"))]
93impl fmt::Display for CoreError {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        match self {
96            CoreError::InvalidDimension { expected, actual } => {
97                write!(
98                    f,
99                    "Invalid dimension: expected {}, got {}",
100                    expected, actual
101                )
102            }
103            CoreError::DivisionByZero => {
104                write!(f, "Division by zero")
105            }
106            CoreError::NumericalInstability => {
107                write!(f, "Numerical instability detected")
108            }
109            CoreError::InvalidBasisIndex(idx, max) => {
110                write!(f, "Invalid basis vector index: {} (max: {})", idx, max)
111            }
112            CoreError::SingularMatrix => {
113                write!(f, "Matrix is singular and cannot be inverted")
114            }
115            CoreError::InvalidSignature {
116                positive,
117                negative,
118                zero,
119                dimension,
120            } => {
121                write!(
122                    f,
123                    "Invalid metric signature: positive {} + negative {} + zero {} != dimension {}",
124                    positive, negative, zero, dimension
125                )
126            }
127            CoreError::GF2DimensionMismatch { expected, got } => {
128                write!(
129                    f,
130                    "GF(2) dimension mismatch: expected {}, got {}",
131                    expected, got
132                )
133            }
134            CoreError::GF2NotSquare { rows, cols } => {
135                write!(f, "GF(2) matrix is not square: {}x{}", rows, cols)
136            }
137            CoreError::GF2IndexOutOfBounds { index, dim } => {
138                write!(
139                    f,
140                    "GF(2) index {} out of bounds for dimension {}",
141                    index, dim
142                )
143            }
144        }
145    }
146}
147
148/// Result type for core operations
149pub type CoreResult<T> = Result<T, CoreError>;