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
43/// Error types for core geometric algebra operations (no_std version)
44#[cfg(not(feature = "std"))]
45#[derive(Debug, Clone, PartialEq)]
46pub enum CoreError {
47    /// Invalid dimension for operation
48    InvalidDimension { expected: usize, actual: usize },
49
50    /// Division by zero
51    DivisionByZero,
52
53    /// Numerical instability detected
54    NumericalInstability,
55
56    /// Invalid basis vector index
57    InvalidBasisIndex(usize, usize),
58
59    /// Matrix is singular (non-invertible)
60    SingularMatrix,
61
62    /// Invalid metric signature
63    InvalidSignature {
64        positive: usize,
65        negative: usize,
66        zero: usize,
67        dimension: usize,
68    },
69}
70
71#[cfg(not(feature = "std"))]
72impl fmt::Display for CoreError {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        match self {
75            CoreError::InvalidDimension { expected, actual } => {
76                write!(
77                    f,
78                    "Invalid dimension: expected {}, got {}",
79                    expected, actual
80                )
81            }
82            CoreError::DivisionByZero => {
83                write!(f, "Division by zero")
84            }
85            CoreError::NumericalInstability => {
86                write!(f, "Numerical instability detected")
87            }
88            CoreError::InvalidBasisIndex(idx, max) => {
89                write!(f, "Invalid basis vector index: {} (max: {})", idx, max)
90            }
91            CoreError::SingularMatrix => {
92                write!(f, "Matrix is singular and cannot be inverted")
93            }
94            CoreError::InvalidSignature {
95                positive,
96                negative,
97                zero,
98                dimension,
99            } => {
100                write!(
101                    f,
102                    "Invalid metric signature: positive {} + negative {} + zero {} != dimension {}",
103                    positive, negative, zero, dimension
104                )
105            }
106        }
107    }
108}
109
110/// Result type for core operations
111pub type CoreResult<T> = Result<T, CoreError>;