use ndarray::{Ix2, Array, RcArray, ArrayBase, Data};
use lapack::c::Layout;
use super::matrix::{Matrix, MFloat};
use super::error::{LinalgError, NotSquareError};
use super::impls::solve::ImplSolve;
pub trait SquareMatrix: Matrix {
fn inv(self) -> Result<Self, LinalgError>;
fn trace(&self) -> Result<Self::Scalar, LinalgError>;
#[doc(hidden)]
fn check_square(&self) -> Result<(), NotSquareError> {
let (rows, cols) = self.size();
if rows == cols {
Ok(())
} else {
Err(NotSquareError {
rows: rows,
cols: cols,
})
}
}
fn square_size(&self) -> Result<usize, NotSquareError> {
self.check_square()?;
let (n, _) = self.size();
Ok(n)
}
}
fn trace<A: MFloat, S>(a: &ArrayBase<S, Ix2>) -> A
where S: Data<Elem = A>
{
let n = a.rows();
(0..n).fold(A::zero(), |sum, i| sum + a[(i, i)])
}
impl<A: MFloat> SquareMatrix for Array<A, Ix2> {
fn inv(self) -> Result<Self, LinalgError> {
self.check_square()?;
let (n, _) = self.size();
let layout = self.layout()?;
let (ipiv, a) = ImplSolve::lu(layout, n, n, self.into_raw_vec())?;
let a = ImplSolve::inv(layout, n, a, &ipiv)?;
let m = Array::from_vec(a).into_shape((n, n)).unwrap();
match layout {
Layout::RowMajor => Ok(m),
Layout::ColumnMajor => Ok(m.reversed_axes()),
}
}
fn trace(&self) -> Result<Self::Scalar, LinalgError> {
self.check_square()?;
Ok(trace(self))
}
}
impl<A: MFloat> SquareMatrix for RcArray<A, Ix2> {
fn inv(self) -> Result<Self, LinalgError> {
let i = self.to_owned().inv()?;
Ok(i.into_shared())
}
fn trace(&self) -> Result<Self::Scalar, LinalgError> {
self.check_square()?;
Ok(trace(self))
}
}