use ndarray::{Array1, ArrayBase, Data, Ix1, Ix2};
use crate::decomposition::lu;
use crate::{InvalidInput, Real, Scalar};
pub fn solve<A, SA, SB>(
a: &ArrayBase<SA, Ix2>,
b: &ArrayBase<SB, Ix1>,
) -> Result<Array1<A>, InvalidInput>
where
A: Scalar,
A::Real: Real,
SA: Data<Elem = A>,
SB: Data<Elem = A>,
{
if !a.is_square() {
return Err(InvalidInput::Shape(
"input matrix is not square".to_string(),
));
}
if b.len() != a.nrows() {
return Err(InvalidInput::Shape(format!(
"The number of elements in `b`, {}, must be the same as the number of rows in `a`, {}",
b.len(),
a.nrows()
)));
}
let factorized = lu::Factorized::from(a.to_owned());
if factorized.is_singular() {
Err(InvalidInput::Value("`a` is a singular matrix".to_string()))
} else {
factorized.solve(b)
}
}