1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Define trait for Hermite matrices

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;

/// Methods for square matrices
///
/// This trait defines method for square matrices,
/// but does not assure that the matrix is square.
/// If not square, `NotSquareError` will be thrown.
pub trait SquareMatrix: Matrix {
    // fn eig(self) -> (Self::Vector, Self);
    /// inverse matrix
    fn inv(self) -> Result<Self, LinalgError>;
    /// trace of matrix
    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,
            })
        }
    }
    /// test matrix is square and return its size
    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> {
        // XXX unnecessary clone (should use into_owned())
        let i = self.to_owned().inv()?;
        Ok(i.into_shared())
    }
    fn trace(&self) -> Result<Self::Scalar, LinalgError> {
        self.check_square()?;
        Ok(trace(self))
    }
}