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