MRMD 0.4.0
Multi Resolution Molecular Dynamics
Loading...
Searching...
No Matches
ExponentialMovingAverage.hpp
Go to the documentation of this file.
1// Copyright 2024 Sebastian Eibl
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include "datatypes.hpp"
18
19namespace mrmd
20{
21namespace util
22{
29{
30private:
31 real_t alpha_;
32 real_t beta_;
33
34 real_t val_ = 0_r;
35 bool isFirstVal_ = true;
36
37public:
38 explicit ExponentialMovingAverage(const real_t& weightingFactor)
39 : alpha_(weightingFactor), beta_(1_r - weightingFactor)
40 {
41 }
42
43 inline operator real_t() const { return toReal(); }
44 inline real_t toReal() const { return val_; }
45
46 void append(const real_t& val)
47 {
48 if (isFirstVal_)
49 {
50 isFirstVal_ = false;
51 val_ = val;
52 return;
53 }
54 val_ = val * alpha_ + val_ * beta_;
55 }
56};
57
59{
60 lhs.append(rhs);
61 return lhs;
62}
63
64} // namespace util
65} // namespace mrmd
Calculates the exponential moving average (EMA) for values added with append.
Definition ExponentialMovingAverage.hpp:29
ExponentialMovingAverage(const real_t &weightingFactor)
Definition ExponentialMovingAverage.hpp:38
void append(const real_t &val)
Definition ExponentialMovingAverage.hpp:46
real_t toReal() const
Definition ExponentialMovingAverage.hpp:44
ExponentialMovingAverage & operator<<(ExponentialMovingAverage &lhs, const real_t &rhs)
Definition ExponentialMovingAverage.hpp:58
Definition BerendsenBarostat.cpp:18
double real_t
Definition datatypes.hpp:99