Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
mean.h
Go to the documentation of this file.
1
2
3
/******************************************************************************
4
The MIT License(MIT)
5
6
Embedded Template Library.
7
https://github.com/ETLCPP/etl
8
https://www.etlcpp.com
9
10
Copyright(c) 2021 John Wellbelove
11
12
Permission is hereby granted, free of charge, to any person obtaining a copy
13
of this software and associated documentation files(the "Software"), to deal
14
in the Software without restriction, including without limitation the rights
15
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16
copies of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions :
18
19
The above copyright notice and this permission notice shall be included in all
20
copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
SOFTWARE.
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
41
namespace
etl
42
{
43
namespace
private_mean
44
{
45
//***************************************************************************
47
//***************************************************************************
48
template
<
typename
TInput,
typename
TCalc>
49
struct
mean_traits
50
{
51
typedef
TCalc calc_t;
52
};
53
54
//***************************************************************************
56
//***************************************************************************
57
template
<
typename
TCalc>
58
struct
mean_traits
<float, TCalc>
59
{
60
typedef
float
calc_t;
61
};
62
63
//***************************************************************************
65
//***************************************************************************
66
template
<
typename
TCalc>
67
struct
mean_traits
<double, TCalc>
68
{
69
typedef
double
calc_t;
70
};
71
}
// namespace private_mean
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
83
typedef
typename
private_mean::mean_traits<TInput, TCalc>::calc_t calc_t;
84
85
public
:
86
87
//*********************************
89
//*********************************
90
mean
()
91
{
92
clear
();
93
}
94
95
//*********************************
97
//*********************************
98
template
<
typename
TIterator>
99
mean
(TIterator first, TIterator last)
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>
142
void
operator()
(TIterator first, TIterator last)
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
}
// namespace etl
203
204
#endif
etl::mean::mean
mean(TIterator first, TIterator last)
Constructor.
Definition
mean.h:99
etl::mean::mean
mean()
Constructor.
Definition
mean.h:90
etl::mean::clear
void clear()
Clear the correlation.
Definition
mean.h:187
etl::mean::count
size_t count() const
Get the total number added entries.
Definition
mean.h:179
etl::mean::operator()
void operator()(TIterator first, TIterator last)
Definition
mean.h:142
etl::mean::operator()
void operator()(TInput value)
Definition
mean.h:132
etl::mean::add
void add(TIterator first, TIterator last)
Add a range.
Definition
mean.h:119
etl::mean::add
void add(TInput value)
Add a pair of values.
Definition
mean.h:108
etl::mean::get_mean
double get_mean() const
Get the mean.
Definition
mean.h:150
functional.h
etl
Definition
absolute.h:40
platform.h
etl::binary_function
binary_function
Definition
functional.h:192
etl::private_mean::mean_traits
Types for generic mean.
Definition
mean.h:50
type_traits.h
include
etl
mean.h
Generated on
for Embedded Template Library by
1.17.0