Multi Resolution Molecular Dynamics 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{
32{
33private:
34 real_t alpha_;
35 real_t beta_;
36
37 real_t val_ = 0_r;
38 bool isFirstVal_ = true;
39
40public:
41 explicit ExponentialMovingAverage(const real_t& weightingFactor)
42 : alpha_(weightingFactor), beta_(1_r - weightingFactor)
43 {
44 }
45
46 inline operator real_t() const { return toReal(); }
47 inline real_t toReal() const { return val_; }
48
49 void append(const real_t& val)
50 {
51 if (isFirstVal_)
52 {
53 isFirstVal_ = false;
54 val_ = val;
55 return;
56 }
57 val_ = val * alpha_ + val_ * beta_;
58 }
59};
60
62{
63 lhs.append(rhs);
64 return lhs;
65}
66
67} // namespace util
68} // namespace mrmd
Calculates the exponential moving average (EMA) for values added with append.
Definition ExponentialMovingAverage.hpp:32
ExponentialMovingAverage(const real_t &weightingFactor)
Definition ExponentialMovingAverage.hpp:41
void append(const real_t &val)
Definition ExponentialMovingAverage.hpp:49
real_t toReal() const
Definition ExponentialMovingAverage.hpp:47
Utility functions used throughout the library.
ExponentialMovingAverage & operator<<(ExponentialMovingAverage &lhs, const real_t &rhs)
Definition ExponentialMovingAverage.hpp:61
Top-level namespace of MRMD.
double real_t
Definition datatypes.hpp:99