trace

Function trace 

Source
pub fn trace<T>(array: ArrayView<'_, T, Ix2>) -> Result<T, &'static str>
where T: Clone + Zero + Add<Output = T>,
Expand description

Calculate the trace of a square matrix (sum of diagonal elements)

§Arguments

  • array - Input square matrix

§Returns

The trace of the matrix

§Examples

use ndarray::array;
use scirs2_core::ndarray_ext::matrix::trace;

let a = array![[1, 2, 3], [4, 5, 6], [7, 8, 9]];
let tr = trace(a.view()).unwrap();
assert_eq!(tr, 15);  // 1 + 5 + 9 = 15