utils.hh 5.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
//! A set of utilities for testing ELM kernels in C++

#ifndef ELM_KERNEL_TEST_UTILS_HH_
#define ELM_KERNEL_TEST_UTILS_HH_

namespace ELM {
namespace Utils {


10
11
12
13
14
template<size_t N, typename T=double>
class VectorStatic {
 public:
  VectorStatic() {}
  VectorStatic(T t) { *this = t; }
15
  VectorStatic(const VectorStatic<N,T>& other) = default;
16

17
18
  T& operator()(size_t i) { assert(0 <= i && i < N); return d_[i]; }
  const T& operator()(size_t i) const { assert(0 <= i && i < N); return d_[i]; }
19

20
21
  T& operator[](size_t i) { assert(0 <= i && i < N); return d_[i]; }
  const T& operator[](size_t i) const { assert(0 <= i && i < N); return d_[i]; }
22
23
24
25
26
27
28
  
  void operator=(T t) {
    for (size_t i=0; i!=N; ++i) {
      d_[i] = t;
    }
  }

29
30
31
  double const * begin() const { return d_.begin(); }
  double const * end() const { return d_.end(); }
  
32
33
34
35
 private:
  std::array<T,N> d_;
};
  
36
37
38
39
40

template<size_t ROW, size_t COL, typename T=double>
class MatrixStatic {
 public:
  MatrixStatic() {}
41
  MatrixStatic(T t) { *this = t; }
42
  MatrixStatic(const MatrixStatic<ROW,COL,T>& other) = default;
43
  
44
45
  T& operator()(size_t i, size_t j) { assert(0 <= i && i < ROW && 0 <= j && j < COL); return d_[i][j]; }
  const T& operator()(size_t i, size_t j) const { assert(0 <= i && i < ROW && 0 <= j && j < COL); return d_[i][j]; }
46

47
48
  VectorStatic<COL,T>& operator[](size_t i) { assert(0 <= i && i < ROW); return d_[i]; }
  const VectorStatic<COL,T>& operator[](size_t i) const { assert(0 <= i && i < ROW); return d_[i]; }
49
50
51
52
53
54
55
56
57
58
59
60
61

  void operator=(T t) {
    for (size_t i=0; i!=ROW; ++i) {
      for (size_t j=0; j!=COL; ++j) {
        d_[i][j] = t;
      }
    }
  }

  double const * begin() const { return &d_[0][0]; }
  double const * end() const { return &d_[ROW-1][COL-1] +1; }
  
 private:
62
  std::array<VectorStatic<COL,T>,ROW> d_;
63
64
65
66
};
  

enum struct Ordering { C, FORTRAN };
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
template <typename T=double, Ordering O=Ordering::C>
class Matrix {

 public:
  Matrix(size_t nrows, size_t ncols) :
      nrows_(nrows),
      ncols_(ncols),
      d_(nrows*ncols) {}

  inline
  const T& operator()(size_t i, size_t j) const {
    return d_[ncols_*i + j];
  }

  inline
  T& operator()(size_t i, size_t j) {
    return d_[ncols_*i + j];
  }

  void operator=(T val) {
    for (size_t lcv=0; lcv!=nrows_*ncols_; ++lcv) {
      d_[lcv] = val;
    }
  }

  typename std::vector<T>::const_iterator begin() const {
    return d_.begin();
  }

  typename std::vector<T>::const_iterator end() const {
    return d_.end();
  }
  
  /*
  inline
  T* operator[](size_t i) {
    return &d_[i*ncols];
  }
  */
 private:
  size_t nrows_, ncols_;
  std::vector<T> d_;

};


template <typename T>
class Matrix<T,Ordering::FORTRAN> {

 public:
  Matrix(size_t nrows, size_t ncols) :
      nrows_(nrows),
      ncols_(ncols),
      d_(nrows*ncols) {}

  inline
  const T& operator()(size_t i, size_t j) const {
    return d_[nrows_*j + i];
  }

  inline
  T& operator()(size_t i, size_t j) {
    return d_[nrows_*j + i];
  }

  /*
  inline
  T* operator[](size_t i) {
    return &d_[i*ncols];
  }
  */
  
  void operator=(T val) {
    for (size_t lcv=0; lcv!=nrows_*ncols_; ++lcv) {
      d_[lcv] = val;
    }
  }


  typename std::vector<T>::const_iterator begin() const {
    return d_.begin();
  }

  typename std::vector<T>::const_iterator end() const {
    return d_.end();
  }
  
 private:
  size_t nrows_, ncols_;
  std::vector<T> d_;

};



template <typename T=double, Ordering O=Ordering::C>
class TensorRank3 {

 public:
  TensorRank3(size_t n_first, size_t n_second, size_t n_third) :
      n_first_(n_first),
      n_second_(n_second),
      n_third_(n_third),
      d_(n_first*n_second*n_third) {}

  inline
  const T& operator()(size_t i, size_t j, size_t k) const {
    return d_[n_third_ * (n_second_*i + j) + k];
  }

  inline
  T& operator()(size_t i, size_t j, size_t k) {
    return d_[n_third_ * (n_second_*i + j) + k];
  }

  /*
  inline
  T* operator[](size_t i) {
    return &d_[i*n_second_*n_third_];
  }
  */
  
  void operator=(T val) {
    for (size_t lcv=0; lcv!=n_first_*n_second_*n_third_; ++lcv) {
      d_[lcv] = val;
    }
  }


  typename std::vector<T>::const_iterator begin() const {
    return d_.begin();
  }

  typename std::vector<T>::const_iterator end() const {
    return d_.end();
  }
  
 private:
  size_t n_first_, n_second_, n_third_;
  std::vector<T> d_;

};


template <typename T>
class TensorRank3<T,Ordering::FORTRAN> {

 public:
  TensorRank3(size_t n_first, size_t n_second, size_t n_third) :
      n_first_(n_first),
      n_second_(n_second),
      n_third_(n_third),
      d_(n_first*n_second*n_third) {}

  inline
  const T& operator()(size_t i, size_t j, size_t k) const {
    return d_[n_first_*(n_second_*k + j) + i];
  }

  inline
  T& operator()(size_t i, size_t j, size_t k) {
    return d_[n_first_*(n_second_*k + j) + i];
  }

  /*
  inline
  T* operator[](size_t k) {
    return &d_[k*n_first_*n_second_];
  }
  */

  void operator=(T val) {
    for (size_t lcv=0; lcv!=n_first_*n_second_*n_third_; ++lcv) {
      d_[lcv] = val;
    }
  }
  
  typename std::vector<T>::const_iterator begin() const {
    return d_.begin();
  }

  typename std::vector<T>::const_iterator end() const {
    return d_.end();
  }

 private:
  size_t n_first_, n_second_, n_third_;
  std::vector<T> d_;

};

  
} // namespace Utils
} // namespace ELM


#endif