00001
00002 #ifndef MORE_TENSOR_H
00003 #define MORE_TENSOR_H
00004
00005 #include <algorithm>
00006 #include <more/num/tensin.h>
00007
00008 namespace more {
00009
00010 template<typename T, int Rank> struct tensor {};
00011
00012 template<typename T>
00013 struct tensor<T, 1> {
00014 static const int CTC_rank = 1;
00015
00016 tensor() : x(0) {}
00017 tensor(tensor const& t)
00018 : x(new T[t.strides[0]*t.extents[0]]),
00019 strides(t.strides), extents(t.extents) {
00020 std::copy(t.storage_begin(), t.storage_end(), storage_begin());
00021 }
00022 tensor(int i0)
00023 : x(new T[i0]) {
00024 strides[0] = 1;
00025 extents[0] = i0;
00026
00027 }
00028 ~tensor() { delete[] x; }
00029
00030 typedef T* storage_iterator;
00031 typedef const T* const_storage_iterator;
00032 storage_iterator storage_begin() { return x; }
00033 storage_iterator storage_end() {
00034 return x+strides[0]*extents[0];
00035 }
00036 const_storage_iterator storage_begin() const { return x; }
00037 const_storage_iterator storage_end() const {
00038 return x+strides[0]*extents[0];
00039 }
00040
00041 tensin<int, 1> const& size() const { return extents; }
00042 int size(int i) const { return extents[i]; }
00043 int storage_size() const {
00044 return stride[0]*extents[0];
00045 }
00046
00047 T& operator()(int i0) {
00048 return x[strides[0]*i0
00049 ];
00050 }
00051 T const& operator()(int i0) const {
00052 return x[strides[0]*i0
00053 ];
00054 }
00055 private:
00056 T* x;
00057 vectin<int, 1> strides;
00058 vectin<int, 1> extents;
00059 };
00060 template<typename T>
00061 struct tensor<T, 2> {
00062 static const int CTC_rank = 2;
00063
00064 tensor() : x(0) {}
00065 tensor(tensor const& t)
00066 : x(new T[t.strides[1]*t.extents[1]]),
00067 strides(t.strides), extents(t.extents) {
00068 std::copy(t.storage_begin(), t.storage_end(), storage_begin());
00069 }
00070 tensor(int i0, int i1)
00071 : x(new T[i0*i1]) {
00072 strides[0] = 1;
00073 extents[0] = i0;
00074 strides[1] = i0;
00075 extents[1] = i1;
00076
00077 }
00078 ~tensor() { delete[] x; }
00079
00080 typedef T* storage_iterator;
00081 typedef const T* const_storage_iterator;
00082 storage_iterator storage_begin() { return x; }
00083 storage_iterator storage_end() {
00084 return x+strides[1]*extents[1];
00085 }
00086 const_storage_iterator storage_begin() const { return x; }
00087 const_storage_iterator storage_end() const {
00088 return x+strides[1]*extents[1];
00089 }
00090
00091 tensin<int, 2> const& size() const { return extents; }
00092 int size(int i) const { return extents[i]; }
00093 int storage_size() const {
00094 return stride[1]*extents[1];
00095 }
00096
00097 T& operator()(int i0, int i1) {
00098 return x[strides[0]*i0
00099 + strides[1]*i1
00100 ];
00101 }
00102 T const& operator()(int i0, int i1) const {
00103 return x[strides[0]*i0
00104 + strides[1]*i1
00105 ];
00106 }
00107 private:
00108 T* x;
00109 vectin<int, 2> strides;
00110 vectin<int, 2> extents;
00111 };
00112 template<typename T>
00113 struct tensor<T, 3> {
00114 static const int CTC_rank = 3;
00115
00116 tensor() : x(0) {}
00117 tensor(tensor const& t)
00118 : x(new T[t.strides[2]*t.extents[2]]),
00119 strides(t.strides), extents(t.extents) {
00120 std::copy(t.storage_begin(), t.storage_end(), storage_begin());
00121 }
00122 tensor(int i0, int i1, int i2)
00123 : x(new T[i0*i1*i2]) {
00124 strides[0] = 1;
00125 extents[0] = i0;
00126 strides[1] = i0;
00127 extents[1] = i1;
00128 strides[2] = strides[1]*i1 ;
00129 extents[2] = i2;
00130
00131 }
00132 ~tensor() { delete[] x; }
00133
00134 typedef T* storage_iterator;
00135 typedef const T* const_storage_iterator;
00136 storage_iterator storage_begin() { return x; }
00137 storage_iterator storage_end() {
00138 return x+strides[2]*extents[2];
00139 }
00140 const_storage_iterator storage_begin() const { return x; }
00141 const_storage_iterator storage_end() const {
00142 return x+strides[2]*extents[2];
00143 }
00144
00145 tensin<int, 3> const& size() const { return extents; }
00146 int size(int i) const { return extents[i]; }
00147 int storage_size() const {
00148 return stride[2]*extents[2];
00149 }
00150
00151 T& operator()(int i0, int i1, int i2) {
00152 return x[strides[0]*i0
00153 + strides[1]*i1
00154 + strides[2]*i2
00155 ];
00156 }
00157 T const& operator()(int i0, int i1, int i2) const {
00158 return x[strides[0]*i0
00159 + strides[1]*i1
00160 + strides[2]*i2
00161 ];
00162 }
00163 private:
00164 T* x;
00165 vectin<int, 3> strides;
00166 vectin<int, 3> extents;
00167 };
00168 template<typename T>
00169 struct tensor<T, 4> {
00170 static const int CTC_rank = 4;
00171
00172 tensor() : x(0) {}
00173 tensor(tensor const& t)
00174 : x(new T[t.strides[3]*t.extents[3]]),
00175 strides(t.strides), extents(t.extents) {
00176 std::copy(t.storage_begin(), t.storage_end(), storage_begin());
00177 }
00178 tensor(int i0, int i1, int i2, int i3)
00179 : x(new T[i0*i1*i2*i3]) {
00180 strides[0] = 1;
00181 extents[0] = i0;
00182 strides[1] = i0;
00183 extents[1] = i1;
00184 strides[2] = strides[1]*i1 ;
00185 extents[2] = i2;
00186 strides[3] = strides[2]*i2 ;
00187 extents[3] = i3;
00188
00189 }
00190 ~tensor() { delete[] x; }
00191
00192 typedef T* storage_iterator;
00193 typedef const T* const_storage_iterator;
00194 storage_iterator storage_begin() { return x; }
00195 storage_iterator storage_end() {
00196 return x+strides[3]*extents[3];
00197 }
00198 const_storage_iterator storage_begin() const { return x; }
00199 const_storage_iterator storage_end() const {
00200 return x+strides[3]*extents[3];
00201 }
00202
00203 tensin<int, 4> const& size() const { return extents; }
00204 int size(int i) const { return extents[i]; }
00205 int storage_size() const {
00206 return stride[3]*extents[3];
00207 }
00208
00209 T& operator()(int i0, int i1, int i2, int i3) {
00210 return x[strides[0]*i0
00211 + strides[1]*i1
00212 + strides[2]*i2
00213 + strides[3]*i3
00214 ];
00215 }
00216 T const& operator()(int i0, int i1, int i2, int i3) const {
00217 return x[strides[0]*i0
00218 + strides[1]*i1
00219 + strides[2]*i2
00220 + strides[3]*i3
00221 ];
00222 }
00223 private:
00224 T* x;
00225 vectin<int, 4> strides;
00226 vectin<int, 4> extents;
00227 };
00228 template<typename T>
00229 struct tensor<T, 5> {
00230 static const int CTC_rank = 5;
00231
00232 tensor() : x(0) {}
00233 tensor(tensor const& t)
00234 : x(new T[t.strides[4]*t.extents[4]]),
00235 strides(t.strides), extents(t.extents) {
00236 std::copy(t.storage_begin(), t.storage_end(), storage_begin());
00237 }
00238 tensor(int i0, int i1, int i2, int i3, int i4)
00239 : x(new T[i0*i1*i2*i3*i4]) {
00240 strides[0] = 1;
00241 extents[0] = i0;
00242 strides[1] = i0;
00243 extents[1] = i1;
00244 strides[2] = strides[1]*i1 ;
00245 extents[2] = i2;
00246 strides[3] = strides[2]*i2 ;
00247 extents[3] = i3;
00248 strides[4] = strides[3]*i3 ;
00249 extents[4] = i4;
00250
00251 }
00252 ~tensor() { delete[] x; }
00253
00254 typedef T* storage_iterator;
00255 typedef const T* const_storage_iterator;
00256 storage_iterator storage_begin() { return x; }
00257 storage_iterator storage_end() {
00258 return x+strides[4]*extents[4];
00259 }
00260 const_storage_iterator storage_begin() const { return x; }
00261 const_storage_iterator storage_end() const {
00262 return x+strides[4]*extents[4];
00263 }
00264
00265 tensin<int, 5> const& size() const { return extents; }
00266 int size(int i) const { return extents[i]; }
00267 int storage_size() const {
00268 return stride[4]*extents[4];
00269 }
00270
00271 T& operator()(int i0, int i1, int i2, int i3, int i4) {
00272 return x[strides[0]*i0
00273 + strides[1]*i1
00274 + strides[2]*i2
00275 + strides[3]*i3
00276 + strides[4]*i4
00277 ];
00278 }
00279 T const& operator()(int i0, int i1, int i2, int i3, int i4) const {
00280 return x[strides[0]*i0
00281 + strides[1]*i1
00282 + strides[2]*i2
00283 + strides[3]*i3
00284 + strides[4]*i4
00285 ];
00286 }
00287 private:
00288 T* x;
00289 vectin<int, 5> strides;
00290 vectin<int, 5> extents;
00291 };
00292 template<typename T>
00293 struct tensor<T, 6> {
00294 static const int CTC_rank = 6;
00295
00296 tensor() : x(0) {}
00297 tensor(tensor const& t)
00298 : x(new T[t.strides[5]*t.extents[5]]),
00299 strides(t.strides), extents(t.extents) {
00300 std::copy(t.storage_begin(), t.storage_end(), storage_begin());
00301 }
00302 tensor(int i0, int i1, int i2, int i3, int i4, int i5)
00303 : x(new T[i0*i1*i2*i3*i4*i5]) {
00304 strides[0] = 1;
00305 extents[0] = i0;
00306 strides[1] = i0;
00307 extents[1] = i1;
00308 strides[2] = strides[1]*i1 ;
00309 extents[2] = i2;
00310 strides[3] = strides[2]*i2 ;
00311 extents[3] = i3;
00312 strides[4] = strides[3]*i3 ;
00313 extents[4] = i4;
00314 strides[5] = strides[4]*i4 ;
00315 extents[5] = i5;
00316
00317 }
00318 ~tensor() { delete[] x; }
00319
00320 typedef T* storage_iterator;
00321 typedef const T* const_storage_iterator;
00322 storage_iterator storage_begin() { return x; }
00323 storage_iterator storage_end() {
00324 return x+strides[5]*extents[5];
00325 }
00326 const_storage_iterator storage_begin() const { return x; }
00327 const_storage_iterator storage_end() const {
00328 return x+strides[5]*extents[5];
00329 }
00330
00331 tensin<int, 6> const& size() const { return extents; }
00332 int size(int i) const { return extents[i]; }
00333 int storage_size() const {
00334 return stride[5]*extents[5];
00335 }
00336
00337 T& operator()(int i0, int i1, int i2, int i3, int i4, int i5) {
00338 return x[strides[0]*i0
00339 + strides[1]*i1
00340 + strides[2]*i2
00341 + strides[3]*i3
00342 + strides[4]*i4
00343 + strides[5]*i5
00344 ];
00345 }
00346 T const& operator()(int i0, int i1, int i2, int i3, int i4, int i5) const {
00347 return x[strides[0]*i0
00348 + strides[1]*i1
00349 + strides[2]*i2
00350 + strides[3]*i3
00351 + strides[4]*i4
00352 + strides[5]*i5
00353 ];
00354 }
00355 private:
00356 T* x;
00357 vectin<int, 6> strides;
00358 vectin<int, 6> extents;
00359 };
00360 template<typename T>
00361 struct tensor<T, 7> {
00362 static const int CTC_rank = 7;
00363
00364 tensor() : x(0) {}
00365 tensor(tensor const& t)
00366 : x(new T[t.strides[6]*t.extents[6]]),
00367 strides(t.strides), extents(t.extents) {
00368 std::copy(t.storage_begin(), t.storage_end(), storage_begin());
00369 }
00370 tensor(int i0, int i1, int i2, int i3, int i4, int i5, int i6)
00371 : x(new T[i0*i1*i2*i3*i4*i5*i6]) {
00372 strides[0] = 1;
00373 extents[0] = i0;
00374 strides[1] = i0;
00375 extents[1] = i1;
00376 strides[2] = strides[1]*i1 ;
00377 extents[2] = i2;
00378 strides[3] = strides[2]*i2 ;
00379 extents[3] = i3;
00380 strides[4] = strides[3]*i3 ;
00381 extents[4] = i4;
00382 strides[5] = strides[4]*i4 ;
00383 extents[5] = i5;
00384 strides[6] = strides[5]*i5 ;
00385 extents[6] = i6;
00386
00387 }
00388 ~tensor() { delete[] x; }
00389
00390 typedef T* storage_iterator;
00391 typedef const T* const_storage_iterator;
00392 storage_iterator storage_begin() { return x; }
00393 storage_iterator storage_end() {
00394 return x+strides[6]*extents[6];
00395 }
00396 const_storage_iterator storage_begin() const { return x; }
00397 const_storage_iterator storage_end() const {
00398 return x+strides[6]*extents[6];
00399 }
00400
00401 tensin<int, 7> const& size() const { return extents; }
00402 int size(int i) const { return extents[i]; }
00403 int storage_size() const {
00404 return stride[6]*extents[6];
00405 }
00406
00407 T& operator()(int i0, int i1, int i2, int i3, int i4, int i5, int i6) {
00408 return x[strides[0]*i0
00409 + strides[1]*i1
00410 + strides[2]*i2
00411 + strides[3]*i3
00412 + strides[4]*i4
00413 + strides[5]*i5
00414 + strides[6]*i6
00415 ];
00416 }
00417 T const& operator()(int i0, int i1, int i2, int i3, int i4, int i5, int i6) const {
00418 return x[strides[0]*i0
00419 + strides[1]*i1
00420 + strides[2]*i2
00421 + strides[3]*i3
00422 + strides[4]*i4
00423 + strides[5]*i5
00424 + strides[6]*i6
00425 ];
00426 }
00427 private:
00428 T* x;
00429 vectin<int, 7> strides;
00430 vectin<int, 7> extents;
00431 };
00432 template<typename T>
00433 struct tensor<T, 8> {
00434 static const int CTC_rank = 8;
00435
00436 tensor() : x(0) {}
00437 tensor(tensor const& t)
00438 : x(new T[t.strides[7]*t.extents[7]]),
00439 strides(t.strides), extents(t.extents) {
00440 std::copy(t.storage_begin(), t.storage_end(), storage_begin());
00441 }
00442 tensor(int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7)
00443 : x(new T[i0*i1*i2*i3*i4*i5*i6*i7]) {
00444 strides[0] = 1;
00445 extents[0] = i0;
00446 strides[1] = i0;
00447 extents[1] = i1;
00448 strides[2] = strides[1]*i1 ;
00449 extents[2] = i2;
00450 strides[3] = strides[2]*i2 ;
00451 extents[3] = i3;
00452 strides[4] = strides[3]*i3 ;
00453 extents[4] = i4;
00454 strides[5] = strides[4]*i4 ;
00455 extents[5] = i5;
00456 strides[6] = strides[5]*i5 ;
00457 extents[6] = i6;
00458 strides[7] = strides[6]*i6 ;
00459 extents[7] = i7;
00460
00461 }
00462 ~tensor() { delete[] x; }
00463
00464 typedef T* storage_iterator;
00465 typedef const T* const_storage_iterator;
00466 storage_iterator storage_begin() { return x; }
00467 storage_iterator storage_end() {
00468 return x+strides[7]*extents[7];
00469 }
00470 const_storage_iterator storage_begin() const { return x; }
00471 const_storage_iterator storage_end() const {
00472 return x+strides[7]*extents[7];
00473 }
00474
00475 tensin<int, 8> const& size() const { return extents; }
00476 int size(int i) const { return extents[i]; }
00477 int storage_size() const {
00478 return stride[7]*extents[7];
00479 }
00480
00481 T& operator()(int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
00482 return x[strides[0]*i0
00483 + strides[1]*i1
00484 + strides[2]*i2
00485 + strides[3]*i3
00486 + strides[4]*i4
00487 + strides[5]*i5
00488 + strides[6]*i6
00489 + strides[7]*i7
00490 ];
00491 }
00492 T const& operator()(int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7) const {
00493 return x[strides[0]*i0
00494 + strides[1]*i1
00495 + strides[2]*i2
00496 + strides[3]*i3
00497 + strides[4]*i4
00498 + strides[5]*i5
00499 + strides[6]*i6
00500 + strides[7]*i7
00501 ];
00502 }
00503 private:
00504 T* x;
00505 vectin<int, 8> strides;
00506 vectin<int, 8> extents;
00507 };
00508
00509 }
00510 #endif