QNANO
Tensor3.h
1 #ifndef QNANO_NEW_TENSOR3_DEFINED_H
2 #define QNANO_NEW_TENSOR3_DEFINED_H
3 
4 #include <complex>
5 #include <iostream>
6 #include <cstdlib>
7 
9 template <typename T> class template_Tensor3{
10 public:
11  //sizes:
12  size_t size[3];
13  //data block:
14  T *data;
15 
16  inline size_t get_index(size_t i, size_t j, size_t k)const{
17  return (i*size[1]+j)*size[2]+k;
18  }
19 
20 public:
21  inline size_t get_totalsize()const{ return size[0]*size[1]*size[2]; }
22 
23 protected:
24 
25  void allocate(){ data=new T[get_totalsize()]; }
26  void deallocate(){ delete[] data; }
27 public:
28  inline size_t get_size(size_t i)const{return size[i];}
29  void resize(size_t i, size_t j, size_t k){
30  size[0]=i; size[1]=j; size[2]=k;
31  deallocate();
32  allocate();
33  }
34 
35  const T &operator()(size_t i, size_t j, size_t k)const{
36  return data[get_index(i,j,k)];
37  }
38  T &operator()(size_t i, size_t j, size_t k){
39  return data[get_index(i,j,k)];
40  }
41  const T &get_check(int i, int j, int k){
42  if(i<0 || i>=(int)size[0] || j<0 || j>=(int)size[1] || k<0 || k>=(int)size[2] ){
43  std::cerr<<"Tensor3: out of bounds!"<<std::endl;
44  exit(1);
45  }
46  return data[get_index(i,j,k)];
47  }
48 
49 
50  //Constructors and copy operators
51  void copy(const template_Tensor3 &other){
52  resize(other.get_size(0),other.get_size(1),other.get_size(2));
53  for(size_t i=0; i<get_totalsize(); i++){
54  data[i]=other.data[i];
55  }
56  }
57  template_Tensor3 &operator=(const template_Tensor3 &other){
58  copy(other);
59  return *this;
60  }
61  template_Tensor3(const template_Tensor3 &other){
62  copy(other);
63  }
65  size[0]=size[1]=size[2]=0;
66  allocate();
67  }
68  template_Tensor3(size_t i, size_t j, size_t k){
69  size[0]=i; size[1]=j; size[2]=k;
70  allocate();
71  }
73  deallocate();
74  }
75 
76 
77 };
78 
79 
82 
83 #endif
84 
85 
86 
Class to store a rank-3 (complex) tensor, i.e., a matrix:
Definition: Tensor3.h:9