ALPSCore reference
mean.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 #pragma once
7 
8 #include <alps/alea/core.hpp>
9 #include <alps/alea/util.hpp>
10 #include <alps/alea/computed.hpp>
11 
12 #include <memory>
13 
14 // Forward declarations
15 
16 namespace alps { namespace alea {
17  template <typename T> class mean_data;
18  template <typename T> class mean_acc;
19  template <typename T> class mean_result;
20 
21  template <typename T>
22  void serialize(serializer &, const std::string &, const mean_result<T> &);
23 
24  template <typename T>
25  void deserialize(deserializer &, const std::string &, mean_result<T> &);
26 
27  template <typename T>
28  std::ostream &operator<<(std::ostream &, const mean_result<T> &);
29 }}
30 
31 // Actual declarations
32 
33 namespace alps { namespace alea {
34 
42 template <typename T>
43 class mean_data
44 {
45 public:
47  mean_data(size_t size) : data_(size) { reset(); }
48 
50  void reset();
51 
53  size_t size() const { return data_.rows(); }
54 
56  size_t count() const { return count_; }
57 
59  size_t &count() { return count_; }
60 
62  const column<T> &data() const { return data_; }
63 
65  column<T> &data() { return data_; }
66 
68  void convert_to_mean();
69 
71  void convert_to_sum();
72 
73 private:
74  column<T> data_;
75  size_t count_;
76 
77  friend class mean_acc<T>;
78  friend class mean_result<T>;
79  friend void serialize<>(serializer &, const std::string &, const mean_result<T> &);
80  friend void deserialize<>(deserializer &, const std::string &, mean_result<T> &);
81 };
82 
83 template <typename T>
84 struct traits< mean_data<T> >
85 {
86  typedef T value_type;
87 };
88 
89 extern template class mean_data<double>;
90 extern template class mean_data<std::complex<double> >;
91 
92 
96 template <typename T>
97 class mean_acc
98 {
99 public:
100  using value_type = T;
101 
102 public:
103  mean_acc(size_t size=1) : store_(new mean_data<T>(size)), size_(size) { }
104 
105  mean_acc(const mean_acc &other);
106 
107  mean_acc &operator=(const mean_acc &other);
108 
110  void reset();
111 
113  void set_size(size_t size);
114 
116  bool valid() const { return (bool)store_; }
117 
119  size_t size() const { return size_; }
120 
122  mean_acc &operator<<(const computed<T> &src) { add(src, 1); return *this; }
123 
125  mean_acc &operator<<(const mean_result<T> &result);
126 
128  size_t count() const { return store_->count(); }
129 
131  mean_result<T> result() const;
132 
135 
137  const mean_data<T> &store() const { return *store_; }
138 
139 protected:
140  void add(const computed<T> &source, size_t count);
141 
142  void finalize_to(mean_result<T> &result);
143 
144 private:
145  std::unique_ptr< mean_data<T> > store_;
146  size_t size_;
147 };
148 
149 template <typename T>
150 struct traits< mean_acc<T> >
151 {
152  typedef T value_type;
155 };
156 
157 extern template class mean_acc<double>;
158 extern template class mean_acc<std::complex<double> >;
159 
163 template <typename T>
164 class mean_result
165 {
166 public:
168 
169  mean_result(const mean_data<T> &acc_data)
170  : store_(new mean_data<T>(acc_data))
171  { }
172 
173  mean_result(const mean_result &other);
174 
175  mean_result &operator=(const mean_result &other);
176 
178  bool valid() const { return (bool)store_; }
179 
181  size_t size() const { return store_->size(); }
182 
184  size_t count() const { return store_->count(); }
185 
187  const column<T> &mean() const { return store_->data(); }
188 
190  const mean_data<T> &store() const { return *store_; }
191 
193  mean_data<T> &store() { return *store_; }
194 
196  void reduce(const reducer &r) { return reduce(r, true, true); }
197 
199  friend void serialize<>(serializer &, const std::string &, const mean_result &);
200 
202  friend void deserialize<>(deserializer &, const std::string &, mean_result &);
203 
205  friend std::ostream &operator<< <>(std::ostream &, const mean_result &);
206 
207 protected:
208  void reduce(const reducer &, bool do_pre_commit, bool do_post_commit);
209 
210 private:
211  std::unique_ptr< mean_data<T> > store_;
212 
213  friend class mean_acc<T>;
214 };
215 
217 template <typename T>
218 bool operator==(const mean_result<T> &r1, const mean_result<T> &r2);
219 template <typename T>
220 bool operator!=(const mean_result<T> &r1, const mean_result<T> &r2)
221 {
222  return !operator==(r1, r2);
223 }
224 
225 template<typename T> struct is_alea_acc<mean_acc<T>> : std::true_type {};
226 template<typename T> struct is_alea_result<mean_result<T>> : std::true_type {};
227 
228 template <typename T>
229 struct traits< mean_result<T> >
230 {
231  typedef T value_type;
232 
233  const static bool HAVE_MEAN = true;
234  const static bool HAVE_VAR = false;
235  const static bool HAVE_COV = false;
236  const static bool HAVE_TAU = false;
237  const static bool HAVE_BATCH = false;
238 };
239 
240 extern template class mean_result<double>;
241 extern template class mean_result<std::complex<double> >;
242 
243 }}
mean_result< T > result_type
Definition: mean.hpp:153
bool valid() const
Definition: mean.hpp:178
size_t & count()
Definition: mean.hpp:59
mean_result(const mean_data< T > &acc_data)
Definition: mean.hpp:169
std::enable_if<!is_sequence< T >::value, std::size_t >::type size(T const &)
Definition: size.hpp:20
const column< T > & mean() const
Definition: mean.hpp:187
mean_data< T > & store()
Definition: mean.hpp:193
const mean_data< T > & store() const
Definition: mean.hpp:190
void deserialize(deserializer &, const std::string &, autocorr_result< T > &)
Definition: autocorr.cpp:262
column< T > & data()
Definition: mean.hpp:65
const mean_data< T > & store() const
Definition: mean.hpp:137
void serialize(serializer &, const std::string &, const autocorr_result< T > &)
Definition: autocorr.cpp:243
size_t size() const
Definition: mean.hpp:119
size_t count() const
Definition: mean.hpp:56
void reset(accumulator_wrapper &arg)
mean_data(size_t size)
Definition: mean.hpp:47
bool valid() const
Definition: mean.hpp:116
size_t size() const
Definition: mean.hpp:53
size_t count() const
Definition: mean.hpp:184
bool operator==(const autocorr_result< T > &r1, const autocorr_result< T > &r2)
Definition: autocorr.cpp:136
traits< Acc >::result_type result(const Acc &acc)
Definition: util.hpp:53
size_t size() const
Definition: mean.hpp:181
const column< T > & data() const
Definition: mean.hpp:62
size_t count() const
Definition: mean.hpp:128
T r(T x, T y=T(), T z=T())
traits< Acc >::result_type finalize(Acc &acc)
Definition: util.hpp:45
void reduce(const reducer &r)
Definition: mean.hpp:196
bool operator!=(const autocorr_result< T > &r1, const autocorr_result< T > &r2)
Definition: autocorr.hpp:220
mean_acc(size_t size=1)
Definition: mean.hpp:103
count_type< T >::type count(T const &arg)
Definition: count.hpp:39