ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
nu1_tpl.h
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * ALPS MPS DMRG Project
4  *
5  * Copyright (C) 2013 Institute for Theoretical Physics, ETH Zurich
6  * 2011-2011 by Bela Bauer <bauerb@phys.ethz.ch>
7  *
8  * This software is part of the ALPS Applications, published under the ALPS
9  * Application License; you can use, redistribute it and/or modify it under
10  * the terms of the license, either version 1 or (at your option) any later
11  * version.
12  *
13  * You should have received a copy of the ALPS Application License along with
14  * the ALPS Applications; see the file LICENSE.txt. If not, the license is also
15  * available from http://alps.comp-phys.org/.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  *****************************************************************************/
26 
27 #ifndef SYMMETRY_NU1_TEMPLATE_H
28 #define SYMMETRY_NU1_TEMPLATE_H
29 
30 #include <iostream>
31 #include <vector>
32 #include <list>
33 
34 #include <boost/lexical_cast.hpp>
35 #include <boost/functional/hash.hpp>
36 
37 #include <boost/serialization/serialization.hpp>
38 #include <boost/serialization/array.hpp>
39 
40 
41 template<int N, class S = int>
42 class NU1Charge
43 {
44 public:
45  static const int static_size = N;
46 
47  NU1Charge(S init = 0)
48  {
49  for (S i = 0; i < N; ++i) (*this)[i] = init;
50  }
51 
52  NU1Charge(boost::array<S, N> const & rhs)
53  {
54  std::copy(rhs.begin(), rhs.end(), this->begin());
55  }
56 
57  S * begin() { return &data_[0]; }
58  S * end() { return &data_[N]; }
59 
60  S const * begin() const { return &data_[0]; }
61  S const * end() const { return &data_[N]; }
62 
63  S & operator[](std::size_t p) { return data_[p]; }
64  S const & operator[](std::size_t p) const { return data_[p]; }
65 
66  template<class Archive>
67  void save(Archive & ar) const
68  {
69  for (int i = 0; i < N; ++i)
70  ar[boost::lexical_cast<std::string>(i)] << (*this)[i];
71  }
72 
73  template<class Archive>
74  void load(Archive & ar)
75  {
76  for (int i = 0; i < N; ++i)
77  ar[boost::lexical_cast<std::string>(i)] >> (*this)[i];
78  }
79 
80  template <class Archive>
81  void serialize(Archive & ar, const unsigned int version)
82  {
83  ar & data_;
84  }
85 
86 private:
87  S data_[N];
88 };
89 
90 namespace boost {
91  template <int N, class S>
92  class hash<NU1Charge<N, S> >{
93  public :
94  size_t operator()(NU1Charge<N, S> const &Charge ) const {
95  return boost::hash_range(&Charge[0],&Charge[N]);
96  }
97  };
98 
99  template <int N, class S>
100  class hash<std::pair<NU1Charge<N, S>, NU1Charge<N, S> > >{
101  public :
102  size_t operator()(std::pair<NU1Charge<N, S>, NU1Charge<N, S> > const &Pair_of_charge ) const {
103  std::size_t seed = 0;
104  for(int i(0); i<N; i++){
105  boost::hash_combine(seed, (Pair_of_charge.first)[i]);
106  boost::hash_combine(seed, (Pair_of_charge.second)[i]);
107  }
108  return seed;
109  }
110  };
111 
112  template <>
113  class hash<std::pair<int, int> >{
114  public :
115  size_t operator()(std::pair<int, int> const &Pair_of_charge ) const {
116  return boost::hash_value(Pair_of_charge);
117  }
118  };
119 
120 };
121 
122 template<int N, class S>
123 std::ostream& operator<<(std::ostream& os, NU1Charge<N, S> const & c)
124 {
125  os << "<";
126  for (int i = 0; i < N; ++i) {
127  os << c[i];
128  if (i+1 < N)
129  os << ",";
130  }
131  os << ">";
132  return os;
133 }
134 
135 template<int N, int I>
136 struct tpl_ops_
137 {
138  template<typename T>
139  bool operator_lt(T const * a, T const * b) const
140  {
141  if (a[I] < b[I])
142  return true;
143  else if (a[I] > b[I])
144  return false;
145  else
146  return tpl_ops_<N, I+1>().operator_lt(a, b);
147  }
148 
149  template<typename T>
150  bool operator_gt(T const * a, T const * b) const
151  {
152  if (a[I] > b[I])
153  return true;
154  else if (a[I] < b[I])
155  return false;
156  else
157  return tpl_ops_<N, I+1>().operator_gt(a, b);
158  }
159 
160  template<typename T>
161  bool operator_eq(T const * a, T const * b) const
162  {
163  if (a[I] != b[I])
164  return false;
165  else
166  return tpl_ops_<N, I+1>().operator_eq(a, b);
167  }
168 
169  template<typename T>
170  void operator_plus(T const * a, T const * b, T * c) const
171  {
172  c[I] = a[I] + b[I];
173  tpl_ops_<N, I+1>().operator_plus(a, b, c);
174  }
175 
176  template<typename T>
177  void operator_uminus(T const * a, T * b) const
178  {
179  b[I] = -a[I];
181  }
182 
183  template<typename T>
184  void operator_div(T const * a, T * b, int n) const
185  {
186  b[I] = a[I]/n;
187  tpl_ops_<N, I+1>().operator_div(a, b, n);
188  }
189 };
190 
191 template<int N>
192 struct tpl_ops_<N, N>
193 {
194  template<typename T>
195  bool operator_lt(T const *, T const *) const { return false; }
196 
197  template<typename T>
198  bool operator_gt(T const *, T const *) const { return false; }
199 
200  template<typename T>
201  bool operator_eq(T const *, T const *) const { return true; }
202 
203  template<typename T>
204  void operator_plus(T const *, T const *, T *) const { }
205 
206  template<typename T>
207  void operator_uminus(T const *, T *) const { }
208 
209  template<typename T>
210  void operator_div(T const *, T *, int) const { }
211 };
212 
213 template<int N, class S>
214 inline bool operator<(NU1Charge<N, S> const & a, NU1Charge<N, S> const & b)
215 {
216  return tpl_ops_<N, 0>().operator_lt(a.begin(), b.begin());
217 }
218 
219 template<int N, class S>
220 inline bool operator>(NU1Charge<N, S> const & a, NU1Charge<N, S> const & b)
221 {
222  return tpl_ops_<N, 0>().operator_gt(a.begin(), b.begin());
223 }
224 
225 template<int N, class S>
226 inline bool operator==(NU1Charge<N, S> const & a, NU1Charge<N, S> const & b)
227 {
228  return tpl_ops_<N, 0>().operator_eq(a.begin(), b.begin());
229 }
230 
231 template<int N, class S>
232 inline bool operator!=(NU1Charge<N, S> const & a, NU1Charge<N, S> const & b)
233 {
234  return !(a==b);
235 }
236 
237 template<int N, class S>
239  NU1Charge<N, S> const & b)
240 {
241  NU1Charge<N, S> ret;
242  tpl_ops_<N, 0>().operator_plus(a.begin(), b.begin(), ret.begin());
243  return ret;
244 }
245 
246 template<int N, class S>
248 {
249  NU1Charge<N, S> ret;
250  tpl_ops_<N, 0>().operator_uminus(rhs.begin(), ret.begin());
251  return ret;
252 }
253 
254 template<int N, class S>
256 {
257  NU1Charge<N, S> ret;
258  tpl_ops_<N, 0>().operator_div(a.begin(), ret.begin(), n);
259  return ret;
260 }
261 template<int N, class S>
262 NU1Charge<N, S> operator/(int n, NU1Charge<N, S> const & a) { return a/n; }
263 
264 
265 template<int N, class S = int>
267 {
268 public:
269  typedef S subcharge;
271  typedef std::vector<charge> charge_v;
272 
273  static const charge IdentityCharge;
274  static const bool finite = false;
275 
276  static charge fuse(charge a, charge b)
277  {
278  return a+b;
279  }
280 
281  template<int R> static charge fuse(boost::array<charge, R> const & v)
282  {
283  charge ret = v[0];
284  for (int i = 1; i < R; ++i)
285  ret = fuse(ret, v[i]);
286  return ret;
287  }
288 };
289 
290 
291 template<int N, class S> const typename NU1_template<N,S>::charge NU1_template<N,S>::IdentityCharge = typename NU1_template<N,S>::charge();
292 
293 #endif
void save(Archive &ar) const
Definition: nu1_tpl.h:67
bool operator_lt(T const *, T const *) const
Definition: nu1_tpl.h:195
void operator_plus(T const *, T const *, T *) const
Definition: nu1_tpl.h:204
static const bool finite
Definition: nu1_tpl.h:274
bool operator_eq(T const *, T const *) const
Definition: nu1_tpl.h:201
size_t operator()(std::pair< int, int > const &Pair_of_charge) const
Definition: nu1_tpl.h:115
static charge fuse(charge a, charge b)
Definition: nu1_tpl.h:276
bool operator_lt(T const *a, T const *b) const
Definition: nu1_tpl.h:139
void operator_div(T const *, T *, int) const
Definition: nu1_tpl.h:210
bool operator>(NU1Charge< N, S > const &a, NU1Charge< N, S > const &b)
Definition: nu1_tpl.h:220
S const * begin() const
Definition: nu1_tpl.h:60
S * end()
Definition: nu1_tpl.h:58
void operator_plus(T const *a, T const *b, T *c) const
Definition: nu1_tpl.h:170
static charge fuse(boost::array< charge, R > const &v)
Definition: nu1_tpl.h:281
NU1Charge(boost::array< S, N > const &rhs)
Definition: nu1_tpl.h:52
S const & operator[](std::size_t p) const
Definition: nu1_tpl.h:64
std::vector< charge > charge_v
Definition: nu1_tpl.h:271
NU1Charge< N, S > operator-(NU1Charge< N, S > const &rhs)
Definition: nu1_tpl.h:247
NU1Charge(S init=0)
Definition: nu1_tpl.h:47
bool operator_gt(T const *, T const *) const
Definition: nu1_tpl.h:198
static const int static_size
Definition: nu1_tpl.h:45
S const * end() const
Definition: nu1_tpl.h:61
void operator_uminus(T const *a, T *b) const
Definition: nu1_tpl.h:177
NU1Charge< N, S > operator+(NU1Charge< N, S > const &a, NU1Charge< N, S > const &b)
Definition: nu1_tpl.h:238
bool operator!=(NU1Charge< N, S > const &a, NU1Charge< N, S > const &b)
Definition: nu1_tpl.h:232
bool operator_eq(T const *a, T const *b) const
Definition: nu1_tpl.h:161
bool operator_gt(T const *a, T const *b) const
Definition: nu1_tpl.h:150
S * begin()
Definition: nu1_tpl.h:57
size_t operator()(std::pair< NU1Charge< N, S >, NU1Charge< N, S > > const &Pair_of_charge) const
Definition: nu1_tpl.h:102
S & operator[](std::size_t p)
Definition: nu1_tpl.h:63
NU1Charge< N, S > charge
Definition: nu1_tpl.h:270
static const charge IdentityCharge
Definition: nu1_tpl.h:273
void operator_uminus(T const *, T *) const
Definition: nu1_tpl.h:207
void operator_div(T const *a, T *b, int n) const
Definition: nu1_tpl.h:184
bool operator==(NU1Charge< N, S > const &a, NU1Charge< N, S > const &b)
Definition: nu1_tpl.h:226
size_t operator()(NU1Charge< N, S > const &Charge) const
Definition: nu1_tpl.h:94
void load(Archive &ar)
Definition: nu1_tpl.h:74
void serialize(Archive &ar, const unsigned int version)
Definition: nu1_tpl.h:81
NU1Charge< N, S > operator/(NU1Charge< N, S > const &a, int n)
Definition: nu1_tpl.h:255