votca 2024-dev
Loading...
Searching...
No Matches
average.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18#ifndef VOTCA_TOOLS_AVERAGE_H
19#define VOTCA_TOOLS_AVERAGE_H
20
21// Standard includes
22#include <cmath>
23
24namespace votca {
25namespace tools {
26
27template <typename T>
28class Average {
29 public:
30 void Process(const T &value);
31 void Clear();
32 template <typename iterator_type>
33 void ProcessRange(const iterator_type &begin, const iterator_type &end);
34
35 T CalcDev() const;
36 T CalcSig2() const;
37 const T &getAvg() const;
38 const T getM2() const;
39 size_t getN() const;
40
41 private:
42 size_t n_ = 0;
43 T av_ = 0; // average
44 T m2_ = 0; // second moment
45};
46
47template <typename T>
48inline void Average<T>::Process(const T &value) {
49 av_ = av_ * (double)n_ / (double)(n_ + 1) + value / (double)(n_ + 1);
50 n_++;
51 m2_ += value * value;
52}
53
54template <typename T>
55inline void Average<T>::Clear() {
56 av_ = 0;
57 n_ = 0;
58 m2_ = 0;
59}
60
61template <typename T>
62template <typename iterator_type>
63void Average<T>::ProcessRange(const iterator_type &begin,
64 const iterator_type &end) {
65 for (iterator_type iter = begin; iter != end; ++iter) {
66 Process(*iter);
67 }
68}
69
70template <typename T>
72 return std::sqrt((m2_ - n_ * av_ * av_) / (n_ - 1));
73}
74
75template <typename T>
77 double dev = 0.0;
78 dev = m2_ / n_ - av_ * av_;
79 return dev;
80}
81
82template <typename T>
83const T &Average<T>::getAvg() const {
84 return av_;
85}
86
87template <typename T>
88const T Average<T>::getM2() const {
89 double m2 = 0.0;
90 m2 = m2_ / n_;
91 return m2;
92}
93
94template <typename T>
95size_t Average<T>::getN() const {
96 return n_;
97}
98
99} // namespace tools
100} // namespace votca
101
102#endif // VOTCA_TOOLS_AVERAGE_H
void Process(const T &value)
Definition average.h:48
void ProcessRange(const iterator_type &begin, const iterator_type &end)
Definition average.h:63
const T getM2() const
Definition average.h:88
const T & getAvg() const
Definition average.h:83
T CalcSig2() const
Definition average.h:76
size_t getN() const
Definition average.h:95
T CalcDev() const
Definition average.h:71
base class for all analysis tools
Definition basebead.h:33