fml  0.1-0
Fused Matrix Library
trace.hh
1 // This file is part of fml which is released under the Boost Software
2 // License, Version 1.0. See accompanying file LICENSE or copy at
3 // https://www.boost.org/LICENSE_1_0.txt
4 
5 #ifndef FML_CPU_LINALG_TRACE_H
6 #define FML_CPU_LINALG_TRACE_H
7 #pragma once
8 
9 
10 #include "../../_internals/omp.hh"
11 
12 #include "../cpumat.hh"
13 
14 
15 namespace fml
16 {
17 namespace linalg
18 {
26  template <typename REAL>
27  REAL trace(const cpumat<REAL> &x)
28  {
29  const REAL *x_d = x.data_ptr();
30  const len_t m = x.nrows();
31  const len_t minmn = std::min(m, x.ncols());
32 
33  REAL tr = 0;
34  #pragma omp parallel for simd if(minmn > fml::omp::OMP_MIN_SIZE) reduction(+:tr)
35  for (len_t i=0; i<minmn; i++)
36  tr += x_d[i + i*m];
37 
38  return tr;
39  }
40 }
41 }
42 
43 
44 #endif
fml::cpumat
Matrix class for data held on a single CPU.
Definition: cpumat.hh:36
fml::unimat::nrows
len_t nrows() const
Number of rows.
Definition: unimat.hh:36
fml::linalg::trace
REAL trace(const cpumat< REAL > &x)
Computes the trace, i.e. the sum of the diagonal.
Definition: trace.hh:27
fml::unimat::ncols
len_t ncols() const
Number of columns.
Definition: unimat.hh:38
fml::unimat::data_ptr
REAL * data_ptr()
Pointer to the internal array.
Definition: unimat.hh:40
fml
Core namespace.
Definition: dimops.hh:10