use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MathError {
DimensionMismatch {
expected: (usize, usize),
found: (usize, usize),
},
IncompatibleInnerDimensions {
lhs: (usize, usize),
rhs: (usize, usize),
},
NotSquare {
rows: usize,
cols: usize,
},
SingularMatrix,
InvalidPolDeg(usize),
}
impl fmt::Display for MathError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MathError::DimensionMismatch { expected, found } => {
write!(
f,
"Dimension mismatch: expected {}x{}, found {}x{}",
expected.0, expected.1, found.0, found.1
)
}
MathError::IncompatibleInnerDimensions { lhs, rhs } => {
write!(
f,
"Incompatible inner dimensions for gemm: {}x{} and {}x{}",
lhs.0, lhs.1, rhs.0, rhs.1
)
}
MathError::NotSquare { rows, cols } => {
write!(f, "Expected square matrix, but got {}x{}", rows, cols)
}
MathError::SingularMatrix => {
write!(f, "Matrix is singular (non-invertible)")
}
MathError::InvalidPolDeg(deg) => {
write!(f, "Invalid polynomial degree {}", deg)
}
}
}
}
impl std::error::Error for MathError {}
pub trait MatrixOps: Sized + Clone {
type Scalar: Copy;
fn shape(&self) -> (usize, usize);
fn zeros(size: usize) -> Self;
fn identity(size: usize) -> Self;
fn diag(size: usize, val: Self::Scalar) -> Self;
fn scale_assign(&mut self, alpha: Self::Scalar);
fn axpy(&mut self, alpha: Self::Scalar, x: &Self);
fn from_axpy(&mut self, alpha: Self::Scalar, x: &Self, y: &Self);
fn gemm(&mut self, alpha: Self::Scalar, a: &Self, b: &Self, beta: Self::Scalar);
fn solve_in_place(&self, b: &mut Self) -> Result<(), MathError>;
fn copy_from(&mut self, x: &Self);
}