forked from tmbdev/clstm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multidim.h
360 lines (327 loc) · 9.75 KB
/
multidim.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// -*- C++ -*-
// A simple and sane multidimensional array class.
#ifndef multidim_
#define multidim_
#define MDSTR(X) # X
#define MDSTR1(X) MDSTR(X)
#define MDCHECK(X) while (!(X)) { THROW("FAILED: " __FILE__ ":" MDSTR1(__LINE__) ":" # X); }
#include <stdlib.h>
namespace multidim {
template <class T>
struct mdarray {
static const int MAXRANK = 8;
int dims[MAXRANK+1] = {0, 0};
int total = 0;
int fill = 0;
T *data = 0;
bool owned = false;
void take(mdarray<T> &other) {
for (int i = 0; i < MAXRANK+1; i++) dims[i] = other.dims[i];
total = other.total;
fill = other.fill;
data = other.data;
owned = other.owned;
for (int i = 0; i < MAXRANK+1; i++) other.dims[i] = 0;
other.total = 0;
other.fill = 0;
other.data = 0;
other.owned = false;
}
// cleared array by default
mdarray() {
}
// deallocate data on deletion
~mdarray() {
clear();
}
// create an array of the given size
template <typename ... Args>
mdarray(Args ... args) {
resize(args ...);
}
// alias an array to a given pointer
template <typename ... Args>
mdarray(T *p, int *dims) {
alias_(p, dims);
}
// no copy constructors or assignment
mdarray(mdarray<T> &) = delete;
mdarray(const mdarray<T> &) = delete;
// clear all allocated data
void clear() {
if (owned && data) delete [] data;
data = 0;
total = 0;
fill = 0;
for (int i = 0; i < MAXRANK; i++) dims[i] = 0;
}
// allocate n elements
void allocate(int n) {
MDCHECK(!data);
data = new T[n];
total = n;
fill = 0;
owned = true;
}
// copy another array
void copy(mdarray<T> &other) {
clear();
allocate(other.total);
for (int i = 0; i < MAXRANK+1; i++) dims[i] = other.dims[i];
fill = other.fill;
for (int i = 0; i < fill; i++) data[i] = other.data[i];
}
// get the extent of dimension i
int dim(int i) {
MDCHECK(unsigned(i) < MAXRANK);
MDCHECK(dims[i] > 0);
return dims[i];
}
// get the rank of the array
int rank() {
for (int i = 0; i < MAXRANK+1; i++)
if (!dims[i]) return i;
THROW("bad rank");
}
// total number of elements in linearized array
int size() {
return fill;
}
// multidimensional subscripting
template <typename ... Args>
T &operator() (Args ... args) {
int indexes[] = {args ...};
int rank = sizeof indexes / sizeof indexes[0];
MDCHECK(rank > 0 && rank <= MAXRANK);
MDCHECK(dims[rank] == 0);
MDCHECK(unsigned(indexes[0]) < unsigned(dims[0]));
int index = indexes[0];
for (int i = 1; i < rank; i++) {
MDCHECK(unsigned(indexes[i]) < unsigned(dims[i]));
index = index * dims[i] + indexes[i];
}
return data[index];
}
// multidimensional subscripting
template <typename ... Args>
T &unsafe_at(Args ... args) {
int indexes[] = {args ...};
int rank = sizeof indexes / sizeof indexes[0];
int index = indexes[0];
for (int i = 1; i < rank; i++) {
index = index * dims[i] + indexes[i];
}
return data[index];
}
T &operator[] (int i) {
MDCHECK(unsigned(i) < unsigned(total));
return data[i];
}
// multidimensional subscripting
template <typename ... Args>
void getSlice(mdarray<T> &result, Args ... args) {
int indexes[] = {args ...};
int rank = sizeof indexes / sizeof indexes[0];
MDCHECK(rank > 0 && rank <= MAXRANK);
MDCHECK(dims[rank] != 0);
MDCHECK(unsigned(indexes[0]) < unsigned(dims[0]));
int index = indexes[0];
for (int i = 1; i < rank; i++) {
MDCHECK(unsigned(indexes[i]) < unsigned(dims[i]));
index = index * dims[i] + indexes[i];
}
result.alias_(data+index, dims+rank);
}
// reshape the array without changing its contents
template <typename ... Args>
mdarray &reshape(Args ... args) {
int indexes[MAXRANK+1] = {args ...};
int rank = sizeof indexes / sizeof indexes[0];
indexes[rank] = 0;
reshape_(indexes);
return *this;
}
// alias an array to given data; the pointer will not
// get deleted on destruction
template <typename ... Args>
mdarray &alias(T *p, Args ... args) {
int indexes[MAXRANK+1] = {args ...};
int rank = sizeof indexes / sizeof indexes[0];
indexes[rank] = 0;
alias_(p, indexes);
return *this;
}
// resize the array, destroying the data it contains
template <typename ... Args>
mdarray &resize(Args ... args) {
int indexes_[] = {args ...};
int rank = sizeof indexes_ / sizeof indexes_[0];
int indexes[MAXRANK+1] = {args ...};
indexes[rank] = 0;
resize_(indexes);
return *this;
}
// fill the array with the given value
template <class S>
mdarray &constant(S value) {
for (int i = 0; i < total; i++) data[i] = value;
return *this;
}
// equivalent shape-related functions using a pointer
// to an integral, zero-terminated array
template <typename INT>
void reshape_(INT *shape, bool exact=true) {
int nsize = unsigned(prod_(shape));
MDCHECK(nsize <= total);
if (exact) MDCHECK(nsize == prod_(dims));
int i;
for (i = 0; i < MAXRANK; i++) {
if (!shape[i]) break;
dims[i] = int(shape[i]);
}
MDCHECK(i <= MAXRANK);
for (; i <= MAXRANK; i++) dims[i] = 0;
fill = prod_(dims);
}
template <typename INT>
void resize_(INT *shape) {
int nsize = prod_(shape);
if (nsize < total) {
// nothing
} else {
clear();
allocate(nsize);
}
reshape_(shape, false);
}
template <typename INT>
void alias_(T *p, INT *shape) {
data = p;
owned = false;
total = prod_(shape);
reshape_(shape);
}
template <typename INT>
static inline int prod_(INT *p) {
if (p[0] == 0) return 0;
int total = 1;
for (int i = 0; p[i]; i++) total *= p[i];
return total;
}
// internal consistency check, invariants
void check_() {
MDCHECK(unsigned(rank()) <= MAXRANK);
MDCHECK(prod_(dims) == total);
}
// assignment and arithmetic
template <class O>
void operator = (mdarray<O> &other) {
copy(other);
}
template <class O>
void operator = (const mdarray<O> &other) {
copy(other);
}
template <class O, class P>
void clip(const O &lo, const P &hi) {
for (int i = 0; i < fill; i++) {
T value = data[i];
if (value < lo) data[i] = lo;
else if (value > hi) data[i] = hi;
}
}
T min() {
T result = data[0];
for (int i = 1; i < fill; i++) {
T value = data[i];
if (value < result) result = value;
}
return result;
}
T max() {
T result = data[0];
for (int i = 1; i < fill; i++) {
T value = data[i];
if (value > result) result = value;
}
return result;
}
double normsq() {
double result = 0.0;
for (int i = 0; i < fill; i++) {
T value = data[i];
result += value*value;
}
return result;
}
double norm() {
return sqrt(normsq());
}
void randomize() {
for (int i = 0; i < fill; i++) data[i] = drand48();
}
template <class O>
void operator += (const O &other) {
assert(fill == other.fill);
for (int i = 0; i < fill; i++) data[i] += other.data[i];
}
template <class O>
void operator -= (const O &other) {
assert(fill == other.fill);
for (int i = 0; i < fill; i++) data[i] += other.data[i];
}
template <class O>
void operator *= (const O &other) {
assert(fill == other.fill);
for (int i = 0; i < fill; i++) data[i] *= other.data[i];
}
template <class O>
void operator /= (const O &other) {
assert(fill == other.fill);
for (int i = 0; i < fill; i++) data[i] /= other.data[i];
}
template <class O>
void operator += (const mdarray<O> &other) {
assert(fill == other.fill);
for (int i = 0; i < fill; i++) data[i] += other.data[i];
}
template <class O>
void operator -= (const mdarray<O> &other) {
assert(fill == other.fill);
for (int i = 0; i < fill; i++) data[i] += other.data[i];
}
template <class O>
void operator *= (const mdarray<O> &other) {
assert(fill == other.fill);
for (int i = 0; i < fill; i++) data[i] *= other.data[i];
}
template <class O>
void operator /= (const mdarray<O> &other) {
assert(fill == other.fill);
for (int i = 0; i < fill; i++) data[i] /= other.data[i];
}
};
template <class T, class S>
inline void to_eigen_matrix(T result, mdarray<S> &a) {
MDCHECK(a.rank() == 2);
result.resize(a.dim(0), a.dim(1));
for (int i = 0; i < a.dim(0); i++)
for (int j = 0; j < a.dim(1); j++)
result(i, j) = a(i, j);
}
template <class T, class S>
inline void to_eigen_vector(T result, mdarray<S> &a) {
MDCHECK(a.rank() == 1);
result.resize(a.dim(0));
for (int i = 0; i < a.dim(0); i++)
result(i) = a(i);
}
template <class T, class S>
inline void from_eigen_matrix(mdarray<S> &result, T a) {
result.resize(ROWS(a), COLS(a));
for (int i = 0; i < ROWS(a); i++)
for (int j = 0; j < COLS(a); j++)
result(i, j) = a(i, j);
}
}
#endif