forked from sanshar/Block
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultiarray.h
507 lines (464 loc) · 16.4 KB
/
multiarray.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
Developed by Sandeep Sharma and Garnet K.-L. Chan, 2012
Copyright (c) 2012, Garnet K.-L. Chan
This program is integrated in Molpro with the permission of
Sandeep Sharma and Garnet K.-L. Chan
*/
#ifndef multi_array_h
#define multi_array_h
#include <vector>
#include <string>
#include <cassert>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/serialization.hpp>
using namespace std;
template<class T> class multiarray
{
public:
virtual T& operator() (const vector<int>& indices) = 0;
virtual T operator() (const vector<int>& indices) const = 0;
virtual string reflect_type() const = 0;
};
template<class T> class array_2d : public vector<T>, public multiarray<T>
{
public:
array_2d () : dim1_d (0), dim2_d (0), vector<T> () { }
array_2d (const int d1, const int d2) : dim1_d (d1), dim2_d (d2), vector<T> (d1 * d2) { }
void Clear()
{
for (int i=0; i<this->size(); ++i)
(*this)[i]=0.;
}
T& operator() (const int i, const int j)
{
assert ((0 <= i) && (i < dim1_d)); assert ((0 <= j) && (j < dim2_d));
return vector<T>::operator[] (i * dim2_d + j);
}
T operator() (const int i, const int j) const
{
assert ((0 <= i) && (i < dim1_d)); assert ((0 <= j) && (j < dim2_d));
return vector<T>::operator[] (i * dim2_d + j);
}
T& operator() (const vector<int>& indices)
{
assert(indices.size() == 2);
return operator()(indices[0], indices[1]);
}
T operator() (const vector<int>& indices) const
{
assert(indices.size() == 2);
return operator()(indices[0], indices[1]);
}
void resize (const int i, const int j) { vector<T>::resize (i * j); dim1_d = i; dim2_d = j; }
int dim1() const { return dim1_d; }
int dim2() const { return dim2_d; }
string reflect_type() const { return string("array_2d"); }
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & dim1_d & dim2_d;
ar & boost::serialization::base_object<std::vector<T> >(*this);
}
int dim1_d;
int dim2_d;
};
template<class T> class array_3d : public vector<T>, public multiarray<T>
{
public:
array_3d() : dim1_d (0), dim2_d (0), dim3_d (0), dim2_times_dim3_d (0), vector<T> () { }
array_3d(const int d1, const int d2, const int d3) : dim1_d (d1), dim2_d (d2), dim3_d (d3), dim2_times_dim3_d (d2 * d3), vector<T> (d1 * d2 * d3) { }
T& operator() (const int i, const int j, const int k)
{
assert((0 <= i) && (i < dim1_d)); assert((0 <= j) && (j < dim2_d)); assert((0 <= k) && (k < dim3_d));
return vector<T>::operator[](i * dim2_times_dim3_d + j * dim3_d + k);
}
void Clear()
{
for (int i=0; i<this->size(); ++i)
(*this)[i]=0.;
}
T operator() (const int i, const int j, const int k) const
{
assert((0 <= i) && (i < dim1_d)); assert((0 <= j) && (j < dim2_d)); assert((0 <= k) && (k < dim3_d));
return vector<T>::operator[](i * dim2_times_dim3_d + j * dim3_d + k);
}
T& operator() (const vector<int>& indices)
{
assert(indices.size() == 3);
return operator()(indices[0], indices[1], indices[2]);
}
T operator() (const vector<int>& indices) const
{
assert(indices.size() == 3);
return operator()(indices[0], indices[1], indices[2]);
}
void resize(const int i, const int j, const int k) { vector<T>::resize(i * j * k); dim1_d = i; dim2_d = j; dim3_d = k; dim2_times_dim3_d = dim2_d * dim3_d; }
int dim1() const { return dim1_d; }
int dim2() const { return dim2_d; }
int dim3() const { return dim3_d; }
string reflect_type() const { return string("array_3d"); }
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & dim1_d & dim2_d & dim3_d & dim2_times_dim3_d;
ar & boost::serialization::base_object<vector<T> >(*this);
}
int dim1_d;
int dim2_d;
int dim3_d;
int dim2_times_dim3_d;
};
template<class T> class array_4d : public vector<T>, public multiarray<T>
{
public:
array_4d() : dim1_d(0), dim2_d(0), dim3_d(0), dim4_d(0), dim3_times_dim4_d(0), dim2_times_dim3_times_dim4_d (0), vector<T>() { }
array_4d(const int d1, const int d2, const int d3, const int d4) : dim1_d (d1), dim2_d (d2), dim3_d (d3), dim4_d(d4),
dim3_times_dim4_d(d3 * d4), dim2_times_dim3_times_dim4_d(d2 * d3 * d4), vector<T> (d1 * d2 * d3 * d4) { }
T& operator()(const int i, const int j, const int k, const int l)
{
assert((0 <= i) && (i < dim1_d)); assert((0 <= j) && (j < dim2_d)); assert((0 <= k) && (k < dim3_d)); assert((0 <= l) && (l < dim4_d));
return vector<T>::operator[](i * dim2_times_dim3_times_dim4_d + j * dim3_times_dim4_d + k * dim4_d + l);
}
T operator()(const int i, const int j, const int k, const int l) const
{
assert((0 <= i) && (i < dim1_d)); assert((0 <= j) && (j < dim2_d)); assert((0 <= k) && (k < dim3_d)); assert((0 <= l) && (l < dim4_d));
return vector<T>::operator[](i * dim2_times_dim3_times_dim4_d + j * dim3_times_dim4_d + k * dim4_d + l);
}
array_4d<T>& operator+=(const array_4d<T>& C)
{
assert(dim1() == C.dim1() &&
dim2() == C.dim2() &&
dim3() == C.dim3() &&
dim4() == C.dim4());
for (int i=0; i<C.size(); ++i)
(*this)[i]+=C[i];
return *this;
}
void Clear()
{
for (int i=0; i<this->size(); ++i)
(*this)[i]=0.;
}
T& operator() (const vector<int>& indices)
{
assert(indices.size() == 4);
return operator()(indices[0], indices[1], indices[2], indices[3]);
}
T operator() (const vector<int>& indices) const
{
assert(indices.size() == 4);
return operator()(indices[0], indices[1], indices[2], indices[3]);
}
void resize (const int i, const int j, const int k, const int l)
{
vector<T>::resize (i * j * k * l); dim1_d = i; dim2_d = j; dim3_d = k; dim4_d = l;
dim2_times_dim3_times_dim4_d = dim2_d * dim3_d * dim4_d; dim3_times_dim4_d = dim3_d * dim4_d;
}
string reflect_type() const { return string("array_4d"); }
int dim1() const { return dim1_d; }
int dim2() const { return dim2_d; }
int dim3() const { return dim3_d; }
int dim4() const { return dim4_d; }
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & dim1_d & dim2_d & dim3_d & dim4_d & dim2_times_dim3_times_dim4_d & dim3_times_dim4_d;
ar & boost::serialization::base_object<std::vector<T> >(*this);
}
int dim1_d;
int dim2_d;
int dim3_d;
int dim4_d;
int dim2_times_dim3_times_dim4_d;
int dim3_times_dim4_d;
};
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
template<class T> class array_6d : public vector<T>, public multiarray<T>
{
public:
array_6d() : dim1_d(0), dim2_d(0), dim3_d(0), dim4_d(0), dim5_d(0), dim6_d(0),
dim5xdim6_d(0),
dim4xdim5xdim6_d(0),
dim3xdim4xdim5xdim6_d(0),
dim2xdim3xdim4xdim5xdim6_d(0),
vector<T> () { }
array_6d(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6) :
dim1_d (d1), dim2_d (d2), dim3_d (d3), dim4_d(d4), dim5_d(d5), dim6_d(d6),
dim5xdim6_d(d5*d6),
dim4xdim5xdim6_d(d4*d5*d6),
dim3xdim4xdim5xdim6_d(d3*d4*d5*d6),
dim2xdim3xdim4xdim5xdim6_d(d2*d3*d4*d5*d6),
vector<T> (d1 * d2 * d3 * d4 * d5 * d6) { }
//std::cout << "array_6d: " << d1 << " "
//<< d2 << " "
//<< d3 << " "
//<< d4 << " "
//<< d5 << " "
//<< d6 << std::endl;
// }
T& operator()(const int i, const int j, const int k, const int l, const int m, const int n)
{
assert((0 <= i) && (i < dim1_d));
assert((0 <= j) && (j < dim2_d));
assert((0 <= k) && (k < dim3_d));
assert((0 <= l) && (l < dim4_d));
assert((0 <= m) && (m < dim5_d));
assert((0 <= n) && (n < dim6_d));
int p = i * dim2xdim3xdim4xdim5xdim6_d
+ j * dim3xdim4xdim5xdim6_d
+ k * dim4xdim5xdim6_d
+ l * dim5xdim6_d
+ m * dim6_d
+ n;
return vector<T>::operator[](p);
}
T operator()(const int i, const int j, const int k, const int l, const int m, const int n) const
{
assert((0 <= i) && (i < dim1_d));
assert((0 <= j) && (j < dim2_d));
assert((0 <= k) && (k < dim3_d));
assert((0 <= l) && (l < dim4_d));
assert((0 <= m) && (m < dim5_d));
assert((0 <= n) && (n < dim6_d));
int p = i * dim2xdim3xdim4xdim5xdim6_d
+ j * dim3xdim4xdim5xdim6_d
+ k * dim4xdim5xdim6_d
+ l * dim5xdim6_d
+ m * dim6_d
+ n;
return vector<T>::operator[](p);
}
array_6d<T>& operator+=(const array_6d<T>& C)
{
assert(dim1() == C.dim1() &&
dim2() == C.dim2() &&
dim3() == C.dim3() &&
dim4() == C.dim4() &&
dim5() == C.dim5() &&
dim6() == C.dim6());
for (int i=0; i<C.size(); ++i)
(*this)[i]+=C[i];
return *this;
}
void Clear()
{
for (int i=0; i<this->size(); ++i)
(*this)[i]=0.;
}
int get_size()
{
return vector<T>::size();
}
T& operator() (const vector<int>& indices)
{
assert(indices.size() == 6);
return operator()(indices[0], indices[1], indices[2], indices[3], indices[4], indices[5]);
}
T operator() (const vector<int>& indices) const
{
assert(indices.size() == 6);
return operator()(indices[0], indices[1], indices[2], indices[3], indices[4], indices[5]);
}
void resize (const int i, const int j, const int k, const int l, const int m, const int n)
{
vector<T>::resize (i * j * k * l * m * n);
dim1_d = i;
dim2_d = j;
dim3_d = k;
dim4_d = l;
dim5_d = m;
dim6_d = n;
dim5xdim6_d = m*n;
dim4xdim5xdim6_d = l*m*n;
dim3xdim4xdim5xdim6_d = k*l*m*n;
dim2xdim3xdim4xdim5xdim6_d = j*k*l*m*n;
}
string reflect_type() const { return string("array_6d"); }
int dim1() const { return dim1_d; }
int dim2() const { return dim2_d; }
int dim3() const { return dim3_d; }
int dim4() const { return dim4_d; }
int dim5() const { return dim5_d; }
int dim6() const { return dim6_d; }
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & dim1_d & dim2_d & dim3_d & dim4_d & dim5_d & dim6_d
& dim2xdim3xdim4xdim5xdim6_d
& dim3xdim4xdim5xdim6_d
& dim4xdim5xdim6_d
& dim5xdim6_d;
ar & boost::serialization::base_object<std::vector<T> >(*this);
}
int dim1_d;
int dim2_d;
int dim3_d;
int dim4_d;
int dim5_d;
int dim6_d;
int dim2xdim3xdim4xdim5xdim6_d;
int dim3xdim4xdim5xdim6_d;
int dim4xdim5xdim6_d;
int dim5xdim6_d;
};
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
template<class T> class array_8d : public vector<T>, public multiarray<T>
{
public:
array_8d() :
dim1_d (0), dim2_d (0), dim3_d (0), dim4_d(0), dim5_d(0), dim6_d(0), dim7_d(0), dim8_d(0),
dim7xdim8_d(0),
dim6xdim7xdim8_d(0),
dim5xdim6xdim7xdim8_d(0),
dim4xdim5xdim6xdim7xdim8_d(0),
dim3xdim4xdim5xdim6xdim7xdim8_d(0),
dim2xdim3xdim4xdim5xdim6xdim7xdim8_d(0),
vector<T> () { }
array_8d(const int d1, const int d2, const int d3, const int d4, const int d5, const int d6, const int d7, const int d8) :
dim1_d (d1), dim2_d (d2), dim3_d (d3), dim4_d(d4), dim5_d(d5), dim6_d(d6), dim7_d(d7), dim8_d(d8),
dim7xdim8_d( d7*d8 ),
dim6xdim7xdim8_d( d6*d7*d8 ),
dim5xdim6xdim7xdim8_d( d5*d6*d7*d8 ),
dim4xdim5xdim6xdim7xdim8_d( d4*d5*d6*d7*d8 ),
dim3xdim4xdim5xdim6xdim7xdim8_d( d3*d4*d5*d6*d7*d8 ),
dim2xdim3xdim4xdim5xdim6xdim7xdim8_d( d2*d3*d4*d5*d6*d7*d8 ),
vector<T> ( d1*d2*d3*d4*d5*d6*d7*d8 ) { }
T& operator()(const int i, const int j, const int k, const int l, const int m, const int n, const int p, const int q)
{
assert((0 <= i) && (i < dim1_d));
assert((0 <= j) && (j < dim2_d));
assert((0 <= k) && (k < dim3_d));
assert((0 <= l) && (l < dim4_d));
assert((0 <= m) && (m < dim5_d));
assert((0 <= n) && (n < dim6_d));
assert((0 <= p) && (p < dim7_d));
assert((0 <= q) && (q < dim8_d));
int u = i * dim2xdim3xdim4xdim5xdim6xdim7xdim8_d
+ j * dim3xdim4xdim5xdim6xdim7xdim8_d
+ k * dim4xdim5xdim6xdim7xdim8_d
+ l * dim5xdim6xdim7xdim8_d
+ m * dim6xdim7xdim8_d
+ n * dim7xdim8_d
+ p * dim8_d
+ q;
return vector<T>::operator[](u);
}
T operator()(const int i, const int j, const int k, const int l, const int m, const int n, const int p, const int q) const
{
assert((0 <= i) && (i < dim1_d));
assert((0 <= j) && (j < dim2_d));
assert((0 <= k) && (k < dim3_d));
assert((0 <= l) && (l < dim4_d));
assert((0 <= m) && (m < dim5_d));
assert((0 <= n) && (n < dim6_d));
assert((0 <= p) && (p < dim7_d));
assert((0 <= q) && (q < dim8_d));
int u = i * dim2xdim3xdim4xdim5xdim6xdim7xdim8_d
+ j * dim3xdim4xdim5xdim6xdim7xdim8_d
+ k * dim4xdim5xdim6xdim7xdim8_d
+ l * dim5xdim6xdim7xdim8_d
+ m * dim6xdim7xdim8_d
+ n * dim7xdim8_d
+ p * dim8_d
+ q;
return vector<T>::operator[](u);
}
array_8d<T>& operator+=(const array_8d<T>& C)
{
assert(dim1() == C.dim1() &&
dim2() == C.dim2() &&
dim3() == C.dim3() &&
dim4() == C.dim4() &&
dim5() == C.dim5() &&
dim6() == C.dim6() &&
dim7() == C.dim7() &&
dim8() == C.dim8());
for (int i=0; i<C.size(); ++i)
(*this)[i]+=C[i];
return *this;
}
void Clear()
{
for (int i=0; i<this->size(); ++i)
(*this)[i]=0.;
}
int get_size()
{
return vector<T>::size();
}
T& operator() (const vector<int>& indices)
{
assert(indices.size() == 8);
return operator()(indices[0], indices[1], indices[2], indices[3], indices[4], indices[5], indices[6], indices[7]);
}
T operator() (const vector<int>& indices) const
{
assert(indices.size() == 8);
return operator()(indices[0], indices[1], indices[2], indices[3], indices[4], indices[5], indices[6], indices[7]);
}
void resize (const int i, const int j, const int k, const int l, const int m, const int n, const int p, const int q)
{
vector<T>::resize (i * j * k * l * m * n * p * q);
dim1_d = i;
dim2_d = j;
dim3_d = k;
dim4_d = l;
dim5_d = m;
dim6_d = n;
dim7_d = p;
dim8_d = q;
dim7xdim8_d = p*q;
dim6xdim7xdim8_d = n*p*q;
dim5xdim6xdim7xdim8_d = m*n*p*q;
dim4xdim5xdim6xdim7xdim8_d = l*m*n*p*q;
dim3xdim4xdim5xdim6xdim7xdim8_d = k*l*m*n*p*q;
dim2xdim3xdim4xdim5xdim6xdim7xdim8_d = j*k*l*m*n*p*q;
}
string reflect_type() const { return string("array_8d"); }
int dim1() const { return dim1_d; }
int dim2() const { return dim2_d; }
int dim3() const { return dim3_d; }
int dim4() const { return dim4_d; }
int dim5() const { return dim5_d; }
int dim6() const { return dim6_d; }
int dim7() const { return dim7_d; }
int dim8() const { return dim8_d; }
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & dim1_d & dim2_d & dim3_d & dim4_d & dim5_d & dim6_d & dim7_d & dim8_d
& dim2xdim3xdim4xdim5xdim6xdim7xdim8_d
& dim3xdim4xdim5xdim6xdim7xdim8_d
& dim4xdim5xdim6xdim7xdim8_d
& dim5xdim6xdim7xdim8_d
& dim6xdim7xdim8_d
& dim7xdim8_d;
ar & boost::serialization::base_object<std::vector<T> >(*this);
}
int dim1_d;
int dim2_d;
int dim3_d;
int dim4_d;
int dim5_d;
int dim6_d;
int dim7_d;
int dim8_d;
int dim2xdim3xdim4xdim5xdim6xdim7xdim8_d;
int dim3xdim4xdim5xdim6xdim7xdim8_d;
int dim4xdim5xdim6xdim7xdim8_d;
int dim5xdim6xdim7xdim8_d;
int dim6xdim7xdim8_d;
int dim7xdim8_d;
};
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
#endif