ALPSCore reference
mean.cpp
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 #include <alps/alea/mean.hpp>
7 #include <alps/alea/util.hpp>
8 #include <alps/alea/computed.hpp>
10 
13 
14 namespace alps { namespace alea {
15 
16 template <typename T>
18 {
19  data_.fill(0);
20  count_ = 0;
21 }
22 
23 template <typename T>
25 {
26  data_ /= count_;
27 }
28 
29 template <typename T>
31 {
32  // Has to be handled explicitly because of the NaNs
33  if (count_ == 0) {
34  reset();
35  return;
36  }
37 
38  data_ *= count_;
39 }
40 
41 template class mean_data<double>;
42 template class mean_data<std::complex<double> >;
43 
44 
45 // We need an explicit copy constructor, as we need to copy the data
46 template <typename T>
48  : store_(other.store_ ? new mean_data<T>(*other.store_) : nullptr)
49  , size_(other.size_)
50 { }
51 
52 template <typename T>
54 {
55  store_.reset(other.store_ ? new mean_data<T>(*other.store_) : nullptr);
56  size_ = other.size_;
57  return *this;
58 }
59 
60 template <typename T>
61 void mean_acc<T>::add(const computed<T> &source, size_t count)
62 {
63  internal::check_valid(*this);
64  source.add_to(view<T>(store_->data().data(), size()));
65  store_->count() += count;
66 }
67 
68 template <typename T>
70 {
71  internal::check_valid(*this);
72  if (size() != other.size())
73  throw size_mismatch();
74 
75  // HACK we need this for "outwardly constant" manipulation
76  mean_data<T> &other_store = const_cast<mean_data<T> &>(other.store());
77  other_store.convert_to_sum();
78  store_->data() += other_store.data();
79  store_->count() += other_store.count();
80  other_store.convert_to_mean();
81  return *this;
82 }
83 
84 template <typename T>
86 {
87  if (valid())
88  store_->reset();
89  else
90  store_.reset(new mean_data<T>(size_));
91 }
92 
93 template <typename T>
95 {
96  size_ = size;
97  if (valid())
98  store_.reset(new mean_data<T>(size_));
99 }
100 
101 template <typename T>
103 {
104  internal::check_valid(*this);
105  mean_result<T> result(*store_);
106  result.store_->convert_to_mean();
107  return result;
108 }
109 
110 template <typename T>
112 {
114  finalize_to(result);
115  return result;
116 }
117 
118 template <typename T>
120 {
121  internal::check_valid(*this);
122  result.store_.reset();
123  result.store_.swap(store_);
124  result.store_->convert_to_mean();
125 }
126 
127 template class mean_acc<double>;
128 template class mean_acc<std::complex<double> >;
129 
130 
131 // We need an explicit copy constructor, as we need to copy the data
132 template <typename T>
134  : store_(other.store_ ? new mean_data<T>(*other.store_) : nullptr)
135 { }
136 
137 template <typename T>
139 {
140  store_.reset(other.store_ ? new mean_data<T>(*other.store_) : nullptr);
141  return *this;
142 }
143 
144 template <typename T>
145 bool operator==(const mean_result<T> &r1, const mean_result<T> &r2)
146 {
147  if (r1.count() == 0 && r2.count() == 0)
148  return true;
149 
150  return r1.count() == r2.count()
151  && r1.store().data() == r2.store().data();
152 }
153 
154 template bool operator==(const mean_result<double> &r1,
155  const mean_result<double> &r2);
156 template bool operator==(const mean_result<std::complex<double>> &r1,
157  const mean_result<std::complex<double>> &r2);
158 
159 template <typename T>
160 void mean_result<T>::reduce(const reducer &r, bool pre_commit, bool post_commit)
161 {
162  internal::check_valid(*this);
163  if (pre_commit) {
164  store_->convert_to_sum();
165  r.reduce(view<T>(store_->data().data(), store_->data().rows()));
166  r.reduce(view<size_t>(&store_->count(), 1));
167  }
168  if (pre_commit && post_commit) {
169  r.commit();
170  }
171  if (post_commit) {
172  reducer_setup setup = r.get_setup();
173  if (setup.have_result)
174  store_->convert_to_mean();
175  else
176  store_.reset(); // free data
177  }
178 }
179 
180 template class mean_result<double>;
181 template class mean_result<std::complex<double> >;
182 
183 
184 template <typename T>
185 void serialize(serializer &s, const std::string &key, const mean_result<T> &self)
186 {
187  internal::check_valid(self);
188  internal::serializer_sentry group(s, key);
189 
190  serialize(s, "@size", self.store_->data_.size());
191  serialize(s, "count", self.store_->count_);
192  s.enter("mean");
193  serialize(s, "value", self.store_->data_);
194  s.exit();
195 }
196 
197 template <typename T>
198 void deserialize(deserializer &s, const std::string &key, mean_result<T> &self)
199 {
200  internal::deserializer_sentry group(s, key);
201 
202  // first deserialize the fundamentals and make sure that the target fits
203  size_t new_size;
204  deserialize(s, "@size", new_size);
205  if (!self.valid() || self.size() != new_size)
206  self.store_.reset(new mean_data<T>(new_size));
207 
208  // deserialize data
209  deserialize(s, "count", self.store_->count_);
210  s.enter("mean");
211  deserialize(s, "value", self.store_->data_);
212  s.exit();
213 }
214 
215 template void serialize(serializer &, const std::string &, const mean_result<double> &);
216 template void serialize(serializer &, const std::string &, const mean_result<std::complex<double> > &);
217 
218 template void deserialize(deserializer &, const std::string &, mean_result<double> &);
219 template void deserialize(deserializer &, const std::string &, mean_result<std::complex<double> > &);
220 
221 
222 template <typename T>
223 std::ostream &operator<<(std::ostream &str, const mean_result<T> &self)
224 {
225  internal::check_valid(self);
226  internal::format_sentry sentry(str);
228 
229  if (verb == PRINT_VERBOSE)
230  str << "<X> = ";
231  str << self.mean();
232  return str;
233 }
234 
235 template std::ostream &operator<<(std::ostream &, const mean_result<double> &);
236 template std::ostream &operator<<(std::ostream &, const mean_result<std::complex<double>> &);
237 
238 
239 }} /* namespace alps::alea */
void add(const computed< T > &source, size_t count)
Definition: mean.cpp:61
bool valid() const
Definition: mean.hpp:178
void check_valid(const Acc &acc)
Definition: util.hpp:17
mean_acc & operator<<(const computed< T > &src)
Definition: mean.hpp:122
void convert_to_mean()
Definition: mean.cpp:24
virtual void exit()=0
const mean_data< T > & store() const
Definition: mean.hpp:190
void finalize_to(mean_result< T > &result)
Definition: mean.cpp:119
void set_size(size_t size)
Definition: mean.cpp:94
virtual void commit() const =0
size_t size() const
Definition: mean.hpp:119
size_t count() const
Definition: mean.hpp:56
void reset(accumulator_wrapper &arg)
friend void serialize(serializer &, const std::string &, const mean_result &)
Definition: mean.cpp:185
bool valid() const
Definition: mean.hpp:116
void convert_to_sum()
Definition: mean.cpp:30
T & get_format(std::ios_base &stream, T initial_value=T())
Definition: format.hpp:90
mean_result< T > finalize()
Definition: mean.cpp:111
mean_result< T > result() const
Definition: mean.cpp:102
size_t count() const
Definition: mean.hpp:184
virtual void add_to(view< T > out) const =0
mean_acc & operator=(const mean_acc &other)
Definition: mean.cpp:53
mean_result & operator=(const mean_result &other)
Definition: mean.cpp:138
bool operator==(const autocorr_result< T > &r1, const autocorr_result< T > &r2)
Definition: autocorr.cpp:136
friend void deserialize(deserializer &, const std::string &, mean_result &)
Definition: mean.cpp:198
size_t size() const
Definition: mean.hpp:181
const column< T > & data() const
Definition: mean.hpp:62
virtual reducer_setup get_setup() const =0
size_t count() const
Definition: mean.hpp:128
virtual void enter(const std::string &group)=0
T r(T x, T y=T(), T z=T())
virtual void reduce(view< double > data) const =0
virtual void enter(const std::string &group)=0
void reduce(const reducer &r)
Definition: mean.hpp:196
virtual void exit()=0
mean_acc(size_t size=1)
Definition: mean.hpp:103