QMCPACK
BlockHistogram.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
9 //
10 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 /** @file BlockHistogram.h
15  * @brief Define a accumulator whose average is evaluated for a moving block of a fixed steps
16  *
17  * Use deque for the buffer but will use boost::circular_buffer with the update
18  */
19 #ifndef QMCPLUSPLUS_BLOCKHISTOGRAM_H
20 #define QMCPLUSPLUS_BLOCKHISTOGRAM_H
21 #include <deque>
22 
23 template<class T>
25 {
26  int maxSize;
27  T mySum;
29  std::deque<T> myData;
30 
31  //typedef for iterator
32  using value_type = T;
33  using reference = T&;
34  using size_type = typename std::deque<T>::size_type;
35  using iterator = typename std::deque<T>::iterator;
36  using const_iterator = typename std::deque<T>::const_iterator;
37 
38  /** default constructor
39  * @param n size of the samples defult=100
40  */
41  explicit inline BlockHistogram(size_type n = 100) : maxSize(n), mySum(T()), myWeightInv(1) {}
42 
43  /** resize the histogram
44  */
45  inline void resize(size_type n, const T& x)
46  {
47  myData.resize(n, x);
48  mySum = n * x;
49  myWeightInv = 1 / static_cast<T>(n);
50  }
51 
52  inline void reserve(size_type n)
53  {
54  maxSize = n;
55  this->clear();
56  }
57 
58  inline void clear()
59  {
60  myData.clear();
61  mySum = T();
62  myWeightInv = 1;
63  }
64 
65  inline value_type mean() const { return mySum * myWeightInv; }
66 
67  inline value_type result() const { return mySum; }
68 
69  inline size_type size() const { return myData.size(); }
70 
71  /** add a value x
72  */
73  inline void operator()(const T& x)
74  {
75  //add to the end
76  myData.push_back(x);
77  if (myData.size() < maxSize)
78  mySum += x;
79  else
80  {
81  mySum += x - myData.front();
82  myData.pop_front();
83  }
84  myWeightInv = 1 / static_cast<T>(myData.size());
85  }
86 };
87 #endif
typename std::deque< T >::iterator iterator
BlockHistogram(size_type n=100)
default constructor
void reserve(size_type n)
size_type size() const
typename std::deque< T >::const_iterator const_iterator
std::deque< T > myData
typename std::deque< T >::size_type size_type
void resize(size_type n, const T &x)
resize the histogram
value_type result() const
value_type mean() const
void operator()(const T &x)
add a value x