Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
standard_deviation.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_STANDARD_DEVIATION_INCLUDED
32
#define ETL_STANDARD_DEVIATION_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_standard_deviation
44
{
45
//***************************************************************************
47
//***************************************************************************
48
template
<
typename
TInput,
typename
TCalc>
49
struct
standard_deviation_traits
50
{
51
typedef
TCalc calc_t;
52
};
53
54
//***************************************************************************
56
//***************************************************************************
57
template
<
typename
TCalc>
58
struct
standard_deviation_traits
<float, TCalc>
59
{
60
typedef
float
calc_t;
61
};
62
63
//***************************************************************************
65
//***************************************************************************
66
template
<
typename
TCalc>
67
struct
standard_deviation_traits
<double, TCalc>
68
{
69
typedef
double
calc_t;
70
};
71
}
// namespace private_standard_deviation
72
73
//***************************************************************************
75
//***************************************************************************
76
namespace
private_standard_deviation
77
{
78
template
<
typename
T =
void
>
79
struct
standard_deviation_type_helper
80
{
81
static
ETL_CONSTANT
bool
Sample =
false
;
82
static
ETL_CONSTANT
bool
Population =
true
;
83
};
84
85
template
<
typename
T>
86
ETL_CONSTANT
bool
standard_deviation_type_helper<T>::Sample;
87
88
template
<
typename
T>
89
ETL_CONSTANT
bool
standard_deviation_type_helper<T>::Population;
90
}
// namespace private_standard_deviation
91
92
struct
standard_deviation_type
:
public
private_standard_deviation::standard_deviation_type_helper
<>
93
{
94
};
95
96
//***************************************************************************
98
//***************************************************************************
99
template
<
bool
Standard_Deviation_Type,
typename
TInput,
typename
TCalc = TInput>
100
class
standard_deviation
101
:
public
private_standard_deviation::standard_deviation_traits
<TInput, TCalc>
102
,
public
etl::binary_function
<TInput, TInput, void>
103
{
104
private
:
105
106
static
ETL_CONSTANT
int
Adjustment = (Standard_Deviation_Type == standard_deviation_type::Population) ? 0 : 1;
107
108
typedef
typename
private_standard_deviation::standard_deviation_traits< TInput, TCalc>::calc_t calc_t;
109
110
public
:
111
112
//*********************************
114
//*********************************
115
standard_deviation
()
116
{
117
clear
();
118
}
119
120
//*********************************
122
//*********************************
123
template
<
typename
TIterator>
124
standard_deviation
(TIterator first, TIterator last)
125
{
126
clear
();
127
add
(first, last);
128
}
129
130
//*********************************
132
//*********************************
133
void
add
(TInput value)
134
{
135
sum_of_squares += TCalc(value * value);
136
sum += TCalc(value);
137
++counter;
138
recalculate =
true
;
139
}
140
141
//*********************************
143
//*********************************
144
template
<
typename
TIterator>
145
void
add
(TIterator first, TIterator last)
146
{
147
while
(first != last)
148
{
149
add
(*first);
150
++first;
151
}
152
}
153
154
//*********************************
157
//*********************************
158
void
operator()
(TInput value)
159
{
160
add
(value);
161
}
162
163
//*********************************
166
//*********************************
167
template
<
typename
TIterator>
168
void
operator()
(TIterator first, TIterator last)
169
{
170
add
(first, last);
171
}
172
173
//*********************************
175
//*********************************
176
double
get_variance
()
const
177
{
178
calculate();
179
180
return
variance_value;
181
}
182
183
//*********************************
185
//*********************************
186
double
get_standard_deviation
()
const
187
{
188
calculate();
189
190
return
standard_deviation_value;
191
}
192
193
//*********************************
195
//*********************************
196
operator
double()
const
197
{
198
return
get_standard_deviation
();
199
}
200
201
//*********************************
203
//*********************************
204
size_t
count
()
const
205
{
206
return
size_t(counter);
207
}
208
209
//*********************************
211
//*********************************
212
void
clear
()
213
{
214
sum_of_squares = calc_t(0);
215
sum = calc_t(0);
216
counter = 0U;
217
variance_value = 0.0;
218
standard_deviation_value = 0.0;
219
recalculate =
true
;
220
}
221
222
private
:
223
224
//*********************************
226
//*********************************
227
void
calculate()
const
228
{
229
if
(recalculate)
230
{
231
standard_deviation_value = 0.0;
232
variance_value = 0.0;
233
234
if
(counter != 0)
235
{
236
double
n = double(counter);
237
double
adjustment = 1.0 / (n * (n - Adjustment));
238
239
double
square_of_sum = (sum * sum);
240
241
variance_value = ((n * sum_of_squares) - square_of_sum) * adjustment;
242
243
if
(variance_value > 0)
244
{
245
standard_deviation_value =
sqrt
(variance_value);
246
}
247
}
248
249
recalculate =
false
;
250
}
251
}
252
253
calc_t sum_of_squares;
254
calc_t sum;
255
uint32_t counter;
256
mutable
double
variance_value;
257
mutable
double
standard_deviation_value;
258
mutable
bool
recalculate;
259
};
260
}
// namespace etl
261
262
#endif
etl::standard_deviation::operator()
void operator()(TInput value)
Definition
standard_deviation.h:158
etl::standard_deviation::count
size_t count() const
Get the total number added entries.
Definition
standard_deviation.h:204
etl::standard_deviation::get_standard_deviation
double get_standard_deviation() const
Get the standard_deviation.
Definition
standard_deviation.h:186
etl::standard_deviation::clear
void clear()
Clear the histogram.
Definition
standard_deviation.h:212
etl::standard_deviation::add
void add(TInput value)
Add a pair of values.
Definition
standard_deviation.h:133
etl::standard_deviation::get_variance
double get_variance() const
Get the variance.
Definition
standard_deviation.h:176
etl::standard_deviation::standard_deviation
standard_deviation(TIterator first, TIterator last)
Constructor.
Definition
standard_deviation.h:124
etl::standard_deviation::standard_deviation
standard_deviation()
Constructor.
Definition
standard_deviation.h:115
etl::standard_deviation::add
void add(TIterator first, TIterator last)
Add a range.
Definition
standard_deviation.h:145
etl::standard_deviation::operator()
void operator()(TIterator first, TIterator last)
Definition
standard_deviation.h:168
functional.h
math.h
etl::private_standard_deviation
Standard Deviation Type.
Definition
standard_deviation.h:44
etl
Definition
absolute.h:40
platform.h
etl::binary_function
binary_function
Definition
functional.h:192
etl::private_standard_deviation::standard_deviation_traits
Types for generic standard_deviation.
Definition
standard_deviation.h:50
etl::private_standard_deviation::standard_deviation_type_helper
Definition
standard_deviation.h:80
etl::sqrt
Definition
sqrt.h:48
etl::standard_deviation_type
Definition
standard_deviation.h:93
type_traits.h
include
etl
standard_deviation.h
Generated on
for Embedded Template Library by
1.17.0