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
//! Trace calculation

use ndarray::*;
use std::iter::Sum;

use super::error::*;
use super::types::*;

pub trait Trace {
    type Output;
    fn trace(&self) -> Result<Self::Output>;
}

impl<A, S> Trace for ArrayBase<S, Ix2>
where
    A: Scalar + Sum,
    S: Data<Elem = A>,
{
    type Output = A;

    fn trace(&self) -> Result<Self::Output> {
        let n = match self.is_square() {
            true => Ok(self.nrows()),
            false => Err(LinalgError::NotSquare {
                rows: self.nrows() as i32,
                cols: self.ncols() as i32,
            }),
        }?;
        Ok((0..n as usize).map(|i| self[(i, i)]).sum())
    }
}