Peano 4
Loading...
Searching...
No Matches
GlidingAverageMeasurement.cpp
Go to the documentation of this file.
1// This file is part of the Peano project. For conditions of distribution and
2// use, please see the copyright notice at www.peano-framework.org
3
5
6#include <cmath>
7#include <limits>
8#include <sstream>
9
10#include "tarch/Assertions.h"
11#include "tarch/la/Scalar.h"
13
14
15tarch::logging::Log tarch::timing::GlidingAverageMeasurement::_log("tarch::timing::GlidingAverageMeasurement");
16
17
19 _weight(weight),
20 _maxEntries(maxEntries) {
21 assertion1(weight <= 1.0, weight);
22 assertion1(maxEntries >= 1, weight);
23}
24
25
27
28
30 if (_values.empty()) {
31 return 0.0;
32 } else {
33 double totalWeight = 0.0;
34 double accumulatedWeight = 0.0;
35
36 for (int i = static_cast<int>(_values.size()) - 1; i >= 0; i--) {
37 double currentWeight = std::pow(_weight, static_cast<int>(_values.size()) - i - 1);
38 accumulatedWeight += currentWeight;
39 totalWeight += currentWeight * _values[i];
40 }
41 return totalWeight / accumulatedWeight;
42 }
43}
44
45
47 if (_values.empty()) {
48 return 0.0;
49 } else {
50 double totalWeight = 0.0;
51 double totalSquaredWeight = 0.0;
52 double accumulatedWeight = 0.0;
53
54 for (int i = static_cast<int>(_values.size()) - 1; i >= 0; i--) {
55 double currentWeight = std::pow(_weight, static_cast<int>(_values.size()) - i - 1);
56 accumulatedWeight += currentWeight;
57 totalSquaredWeight += currentWeight * currentWeight * _values[i];
58 totalWeight += currentWeight * _values[i];
59 }
60
61 double result = (totalSquaredWeight - totalWeight) / accumulatedWeight;
63 return std::sqrt(result);
64 } else {
65 return 0.0;
66 }
67 }
68}
69
70
72 if (_values.size() < 4) {
73 return false;
74 } else if (std::abs(getValue()) <= 1.0) {
75 return getStandardDeviation() < factor;
76 } else {
77 return getStandardDeviation() / std::abs(getValue()) < factor;
78 }
79}
80
81
83 if (static_cast<int>(_values.size()) < _maxEntries) {
84 _values.push_back(value);
85 } else {
86 for (int i = 0; i < _maxEntries - 1; i++) {
87 _values[i] = _values[i + 1];
88 }
89
90 _values[_maxEntries - 1] = value;
91 }
92}
93
94
96 std::ostringstream msg;
97 msg << "(" << getValue();
98 for (auto p : _values) {
99 msg << "," << p;
100 }
101 msg << ")";
102 return msg.str();
103}
104
105
107 double result = -std::numeric_limits<double>::max();
108 for (auto p : _values) {
109 result = result > p ? result : p;
110 }
111 return result;
112}
113
114
116 double result = std::numeric_limits<double>::max();
117 for (auto p : _values) {
118 result = result < p ? result : p;
119 }
120 return result;
121}
#define assertion1(expr, param)
Log Device.
Definition Log.h:516
bool isAccurateValue(double factor=0.1) const
Is value accurate.
GlidingAverageMeasurement(double weight=0.7, int maxEntries=16)
double getStandardDeviation() const
To compute the standard deviation, we rely on the formula.
void setValue(const double &value)
Set the value.
constexpr double NUMERICAL_ZERO_DIFFERENCE
Definition Scalar.h:17