Skip to main content

trace

Function trace 

Source
pub fn trace<T, D0, D1, L>(a: &Slice<T, (D0, D1), L>) -> T
where T: ComplexFloat + Add<Output = T> + Copy, D0: Dim, D1: Dim, L: Layout,
Expand description

Computes the trace of a square matrix (sum of diagonal elements).

ยงExamples

use mdarray::tensor;
use mdarray_linalg::utils::trace;

let a = tensor![[1., 2., 3.],
                [4., 5., 6.],
                [7., 8., 9.]];

let tr = trace(&a);
assert_eq!(tr, 15.0);