MADNESS  version 0.9
basetensor.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id$
33 */
34 
35 
36 #ifndef MADNESS_TENSOR_BASETENSOR_H__INCLUDED
37 #define MADNESS_TENSOR_BASETENSOR_H__INCLUDED
38 
41 
42 #include <complex>
43 
44 typedef std::complex<float> float_complex;
45 typedef std::complex<double> double_complex;
46 
47 // These probably have to be included in this order
50 #include <madness/tensor/slice.h>
52 
53 #include <madness/madness_config.h>
54 #include <madness/tensor/slice.h>
56 
57 #ifdef TENSOR_INSTANCE_COUNT
59 #endif
60 
61 namespace madness {
85  class BaseTensor {
86  private:
87 #ifdef TENSOR_INSTANCE_COUNT
88  static madness::AtomicInt instance_count;
89 #endif
90 
91  protected:
92 
93  long _size;
94  long _ndim;
95  long _id;
98 
99  void set_dims_and_size(long nd, const long d[]) {
100  _ndim = nd;
101  _size = 1;
102  if (_ndim < 0) _size=0;
103  for (long i=_ndim-1; i>=0; --i) {
104  _dim[i] = d[i];
105  _stride[i] = _size;
106  _size *= d[i];
107  }
108  for (long i=std::max(_ndim,0L); i<TENSOR_MAXDIM; ++i) { // So can iterate over missing dimensions
109  _dim[i] = 1;
110  _stride[i] = 0;
111  }
112  }
113 
114  public:
115 
116  BaseTensor() : _size(0), _ndim(-1) {
117 #ifdef TENSOR_INSTANCE_COUNT
118  instance_count++;
119 #endif
120  }
121 
122  virtual ~BaseTensor() {
123 #ifdef TENSOR_INSTANCE_COUNT
124  instance_count--;
125 #endif
126  }
127 
129  static inline int get_instance_count() {
130 #ifdef TENSOR_INSTANCE_COUNT
131  return instance_count;
132 #else
133  return 0;
134 #endif
135  }
136 
138  long size() const {return _size;}
139 
141  long id() const {return _id;}
142 
144  long ndim() const {return _ndim;}
145 
147  long dim(int i) const {return _dim[i];}
148 
150  long stride(int i) const {return _stride[i];}
151 
153  const long* dims() const {return _dim;}
154 
156  const long* strides() const {return _stride;}
157 
159  bool conforms(const BaseTensor *t) const {
160  if (_ndim != t->_ndim) return false;
161  for (long i=0; i<_ndim; ++i) {
162  if (_dim[i] != t->_dim[i]) return false;
163  }
164  return true;
165  }
166 
168  bool iscontiguous() const {
169  if (_size <= 0) return true;
170  long sz = 1;
171  for (long i=_ndim-1; i>=0; --i) {
172  if (_stride[i] != sz) return false;
173  sz *= _dim[i];
174  }
175  return true;
176  }
177 
178  protected:
179 
181  void reshape_inplace(const std::vector<long>& d);
182 
184  void reshape_inplace(int ndimnew, const long* d);
185 
187  void flat_inplace();
188 
190  void splitdim_inplace(long i, long dimi0, long dimi1);
191 
193  void fusedim_inplace(long i);
194 
196  void swapdim_inplace(long i, long j);
197 
199  void cycledim_inplace(long shift, long start, long end);
200 
202  void mapdim_inplace(const std::vector<long>& map);
203  };
204 
205 }
206 
207 #endif // MADNESS_TENSOR_BASETENSOR_H__INCLUDED
long _size
Number of elements in the tensor.
Definition: basetensor.h:93
std::complex< double > double_complex
Definition: basetensor.h:45
BaseTensor()
Definition: basetensor.h:116
void reshape_inplace(const std::vector< long > &d)
Reshapes the tensor inplace.
Definition: basetensor.cc:76
long ndim() const
Returns the number of dimensions in the tensor.
Definition: basetensor.h:144
Macros for easy and efficient iteration over tensors.
const double L
Definition: 3dharmonic.cc:123
static int get_instance_count()
Returns the count of all current instances of tensors & slice tensors of all types.
Definition: basetensor.h:129
long dim(int i) const
Returns the size of dmension i.
Definition: basetensor.h:147
long id() const
Returns the typeid of the tensor (c.f., TensorTypeData )
Definition: basetensor.h:141
const long * dims() const
Returns the array of tensor dimensions.
Definition: basetensor.h:153
void set_dims_and_size(long nd, const long d[])
Definition: basetensor.h:99
The base class for tensors defines generic capabilities.
Definition: basetensor.h:85
const long * strides() const
Returns the array of tensor strides.
Definition: basetensor.h:156
void mapdim_inplace(const std::vector< long > &map)
General permutation of dimensions.
Definition: basetensor.cc:156
#define max(a, b)
Definition: lda.h:53
Defines and implements TensorTypeData, a type traits class.
bool conforms(const BaseTensor *t) const
Returns true if this and *t are the same shape and size.
Definition: basetensor.h:159
void splitdim_inplace(long i, long dimi0, long dimi1)
Splits dimension i.
Definition: basetensor.cc:88
long _ndim
Number of dimensions (-1=invalid; 0=scalar; >0=tensor)
Definition: basetensor.h:94
Declares and implements factories for short vectors.
void cycledim_inplace(long shift, long start, long end)
Cyclic shift of dimensions.
Definition: basetensor.cc:134
#define TENSOR_MAXDIM
Definition: tensor_macros.h:194
bool iscontiguous() const
Returns true if the tensor refers to contiguous memory locations.
Definition: basetensor.h:168
void flat_inplace()
Reshapes the tensor inplace into 1D.
Definition: basetensor.cc:81
std::complex< float > float_complex
Definition: basetensor.h:44
An integer with atomic set, get, read+inc, read+dec, dec+test operations.
Definition: atomicint.h:73
long stride(int i) const
Returns the stride associated with dimension i.
Definition: basetensor.h:150
long _id
Id from TensorTypeData in type_data.h.
Definition: basetensor.h:95
long _stride[TENSOR_MAXDIM]
Increment between elements in each dimension.
Definition: basetensor.h:97
Declares and implements Slice.
long size() const
Returns the number of elements in the tensor.
Definition: basetensor.h:138
long _dim[TENSOR_MAXDIM]
Size of each dimension.
Definition: basetensor.h:96
virtual ~BaseTensor()
Definition: basetensor.h:122
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
void fusedim_inplace(long i)
Fuses dimensions i and i+1.
Definition: basetensor.cc:107
void swapdim_inplace(long i, long j)
Swaps the dimensions.
Definition: basetensor.cc:124