fml  0.1-0
Fused Matrix Library
utils.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_UTILS_H
6 #define FML_CPU_UTILS_H
7 #pragma once
8 
9 
10 #include <cmath>
11 #include <stdexcept>
12 
13 #include "../../_internals/linalgutils.hh"
14 
15 #include "../internals/lapack.hh"
16 
17 #include "../cpumat.hh"
18 
19 
20 namespace utils
21 {
22  template <typename REAL>
23  bool is_symmetric(const cpumat<REAL> &x)
24  {
25  if (!x.is_square())
26  return false;
27 
28  const int blocksize = 8;
29  const len_t n = x.nrows();
30 
31  for (len_t j=0; j<n; j+=blocksize)
32  {
33  for (len_t i=j; i<n; i+=blocksize)
34  {
35  for (len_t col=j; col<j+blocksize && col<n; ++col)
36  {
37  for (len_t row=i; row<i+blocksize && row<n; ++row)
38  {
39  const bool check = samenum(x[col + n*row], x[row + n*col]);
40  if (!check)
41  return false;
42  }
43  }
44  }
45  }
46 
47  return true;
48  }
49 
50 
51  template <typename REAL>
52  void symmetrize(cpumat<REAL> &x)
53  {
54  if (!x.is_square())
55  throw std::runtime_error("non-square matrix");
56 
57  const int blocksize = 8;
58  const len_t n = x.nrows();
59 
60  REAL *x_d = x.data_ptr();
61  for (len_t j=0; j<n; j+=blocksize)
62  {
63  for (len_t i=j+1; i<n; i+=blocksize)
64  {
65  for (len_t col=j; col<j+blocksize && col<n; ++col)
66  {
67  for (len_t row=i; row<i+blocksize && row<n; ++row)
68  x_d[col + n*row] = x_d[row + n*col];
69  }
70  }
71  }
72  }
73 }
74 
75 
76 #endif
REAL * data_ptr()
Pointer to the internal array.
Definition: unimat.hh:34
bool is_square() const
Is the matrix square?
Definition: unimat.hh:28
len_t nrows() const
Number of rows.
Definition: unimat.hh:30
Matrix class for data held on a single CPU.
Definition: cpumat.hh:31
Definition: utils.hh:20