use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinearError(pub LinearErrorEnum);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LinearErrorEnum {
IndexOutOfBounds {
index: (usize, usize),
shape: (usize, usize),
},
ShapeMismatch {
left: (usize, usize),
right: (usize, usize),
},
InnerDimensionMismatch { left_cols: usize, right_rows: usize },
LengthMismatch { expected: usize, found: usize },
NotSquare { shape: (usize, usize) },
Singular { at_column: usize },
ZeroDiagonal { at_index: usize },
WrongTriangle { at: (usize, usize) },
NotBinary { at: (usize, usize) },
Overflow { operation: &'static str },
NotPositiveDefinite { at_index: usize },
EmptyMatrix,
}
impl LinearError {
pub fn kind(&self) -> &LinearErrorEnum {
&self.0
}
pub fn new(variant: LinearErrorEnum) -> Self {
Self(variant)
}
#[allow(non_snake_case)]
pub fn IndexOutOfBounds(index: (usize, usize), shape: (usize, usize)) -> Self {
Self(LinearErrorEnum::IndexOutOfBounds { index, shape })
}
#[allow(non_snake_case)]
pub fn ShapeMismatch(left: (usize, usize), right: (usize, usize)) -> Self {
Self(LinearErrorEnum::ShapeMismatch { left, right })
}
#[allow(non_snake_case)]
pub fn InnerDimensionMismatch(left_cols: usize, right_rows: usize) -> Self {
Self(LinearErrorEnum::InnerDimensionMismatch {
left_cols,
right_rows,
})
}
#[allow(non_snake_case)]
pub fn LengthMismatch(expected: usize, found: usize) -> Self {
Self(LinearErrorEnum::LengthMismatch { expected, found })
}
#[allow(non_snake_case)]
pub fn NotSquare(shape: (usize, usize)) -> Self {
Self(LinearErrorEnum::NotSquare { shape })
}
#[allow(non_snake_case)]
pub fn Singular(at_column: usize) -> Self {
Self(LinearErrorEnum::Singular { at_column })
}
#[allow(non_snake_case)]
pub fn ZeroDiagonal(at_index: usize) -> Self {
Self(LinearErrorEnum::ZeroDiagonal { at_index })
}
#[allow(non_snake_case)]
pub fn WrongTriangle(at: (usize, usize)) -> Self {
Self(LinearErrorEnum::WrongTriangle { at })
}
#[allow(non_snake_case)]
pub fn NotBinary(at: (usize, usize)) -> Self {
Self(LinearErrorEnum::NotBinary { at })
}
#[allow(non_snake_case)]
pub fn Overflow(operation: &'static str) -> Self {
Self(LinearErrorEnum::Overflow { operation })
}
#[allow(non_snake_case)]
pub fn NotPositiveDefinite(at_index: usize) -> Self {
Self(LinearErrorEnum::NotPositiveDefinite { at_index })
}
#[allow(non_snake_case)]
pub fn EmptyMatrix() -> Self {
Self(LinearErrorEnum::EmptyMatrix)
}
}
impl fmt::Display for LinearError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl fmt::Display for LinearErrorEnum {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LinearErrorEnum::IndexOutOfBounds { index, shape } => write!(
f,
"Index out of bounds: ({}, {}) is outside a {}x{} matrix.",
index.0, index.1, shape.0, shape.1
),
LinearErrorEnum::ShapeMismatch { left, right } => write!(
f,
"Shape mismatch: left is {}x{}, right is {}x{}.",
left.0, left.1, right.0, right.1
),
LinearErrorEnum::InnerDimensionMismatch {
left_cols,
right_rows,
} => write!(
f,
"Inner dimension mismatch: left has {left_cols} columns, right has {right_rows} rows."
),
LinearErrorEnum::LengthMismatch { expected, found } => {
write!(f, "Length mismatch: expected {expected}, found {found}.")
}
LinearErrorEnum::NotSquare { shape } => write!(
f,
"Not square: this operation needs a square matrix, got {}x{}.",
shape.0, shape.1
),
LinearErrorEnum::Singular { at_column } => {
write!(f, "Singular: no pivot available in column {at_column}.")
}
LinearErrorEnum::ZeroDiagonal { at_index } => write!(
f,
"Zero diagonal: entry {at_index} on the diagonal is zero, so the substitution cannot divide by it."
),
LinearErrorEnum::WrongTriangle { at } => write!(
f,
"Wrong triangle: a non-zero entry at ({}, {}) is outside the expected triangle.",
at.0, at.1
),
LinearErrorEnum::NotBinary { at } => write!(
f,
"Not binary: the entry at ({}, {}) is outside {{0, 1}} and cannot be packed into GF(2).",
at.0, at.1
),
LinearErrorEnum::Overflow { operation } => write!(
f,
"Overflow: {operation} produced a value the scalar type cannot hold. The exact result exists in the unbounded structure; it does not fit this representation."
),
LinearErrorEnum::NotPositiveDefinite { at_index } => write!(
f,
"Not positive definite: the Cholesky radicand at diagonal entry {at_index} is not positive, so the matrix has no Cholesky factor. It may still be invertible."
),
LinearErrorEnum::EmptyMatrix => write!(
f,
"Empty matrix: the operation has no meaning on an empty matrix."
),
}
}
}
impl core::error::Error for LinearError {}
impl core::error::Error for LinearErrorEnum {}