Embedded Template Library 1.0
Loading...
Searching...
No Matches
mean.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2021 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_MEAN_INCLUDED
32#define ETL_MEAN_INCLUDED
33
34#include "platform.h"
35#include "functional.h"
36#include "type_traits.h"
37
38//#include <math.h>
39#include <stdint.h>
40
41namespace etl
42{
43 namespace private_mean
44 {
45 //***************************************************************************
47 //***************************************************************************
48 template <typename TInput, typename TCalc>
50 {
51 typedef TCalc calc_t;
52 };
53
54 //***************************************************************************
56 //***************************************************************************
57 template <typename TCalc>
59 {
60 typedef float calc_t;
61 };
62
63 //***************************************************************************
65 //***************************************************************************
66 template <typename TCalc>
68 {
69 typedef double calc_t;
70 };
71 }
72
73 //***************************************************************************
75 //***************************************************************************
76 template <typename TInput, typename TCalc = TInput>
77 class mean
78 : public private_mean::mean_traits<TInput, TCalc>
79 , public etl::binary_function<TInput, TInput, void>
80 {
81 private:
82
84
85 public:
86
87 //*********************************
89 //*********************************
91 {
92 clear();
93 }
94
95 //*********************************
97 //*********************************
98 template <typename TIterator>
100 {
101 clear();
102 add(first, last);
103 }
104
105 //*********************************
107 //*********************************
108 void add(TInput value)
109 {
110 sum += TCalc(value);
111 ++counter;
112 recalculate = true;
113 }
114
115 //*********************************
117 //*********************************
118 template <typename TIterator>
119 void add(TIterator first, TIterator last)
120 {
121 while (first != last)
122 {
123 add(*first);
124 ++first;
125 }
126 }
127
128 //*********************************
131 //*********************************
132 void operator ()(TInput value)
133 {
134 add(value);
135 }
136
137 //*********************************
140 //*********************************
141 template <typename TIterator>
143 {
144 add(first, last);
145 }
146
147 //*********************************
149 //*********************************
150 double get_mean() const
151 {
152 if (recalculate)
153 {
154 mean_value = 0.0;
155
156 if (counter != 0)
157 {
158 double n = double(counter);
159 mean_value = sum / n;
160 }
161
162 recalculate = false;
163 }
164
165 return mean_value;
166 }
167
168 //*********************************
170 //*********************************
171 operator double() const
172 {
173 return get_mean();
174 }
175
176 //*********************************
178 //*********************************
179 size_t count() const
180 {
181 return size_t(counter);
182 }
183
184 //*********************************
186 //*********************************
187 void clear()
188 {
189 sum = calc_t(0);
190 counter = 0U;
191 mean_value = 0.0;
192 recalculate = true;
193 }
194
195 private:
196
197 calc_t sum;
198 uint32_t counter;
199 mutable double mean_value;
200 mutable bool recalculate;
201 };
202}
203
204#endif
Mean.
Definition mean.h:80
mean(TIterator first, TIterator last)
Constructor.
Definition mean.h:99
mean()
Constructor.
Definition mean.h:90
void clear()
Clear the correlation.
Definition mean.h:187
size_t count() const
Get the total number added entries.
Definition mean.h:179
void operator()(TInput value)
Definition mean.h:132
void add(TIterator first, TIterator last)
Add a range.
Definition mean.h:119
void add(TInput value)
Add a pair of values.
Definition mean.h:108
double get_mean() const
Get the mean.
Definition mean.h:150
bitset_ext
Definition absolute.h:38
Definition functional.h:126
pair holds two objects of arbitrary type
Definition utility.h:164
Types for generic mean.
Definition mean.h:50