Embedded Template Library 1.0
Loading...
Searching...
No Matches
covariance.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_COVARIANCE_INCLUDED
32#define ETL_COVARIANCE_INCLUDED
33
34#include "platform.h"
35#include "functional.h"
36#include "type_traits.h"
37
38#include <stdint.h>
39
40namespace etl
41{
42 namespace private_covariance
43 {
44 //***************************************************************************
46 //***************************************************************************
47 template <typename TInput, typename TCalc>
49 {
50 typedef TCalc calc_t;
51 };
52
53 //***************************************************************************
55 //***************************************************************************
56 template <typename TCalc>
58 {
59 typedef float calc_t;
60 };
61
62 //***************************************************************************
64 //***************************************************************************
65 template <typename TCalc>
67 {
68 typedef double calc_t;
69 };
70 }
71
72 //***************************************************************************
74 //***************************************************************************
75 namespace private_covariance
76 {
77 template<typename T = void>
79 {
80 static ETL_CONSTANT bool Sample = false;
81 static ETL_CONSTANT bool Population = true;
82 };
83
84 template<typename T>
85 ETL_CONSTANT bool covariance_type_statics<T>::Sample;
86
87 template<typename T>
89 }
90
94
95 //***************************************************************************
97 //***************************************************************************
98 template <bool Covariance_Type, typename TInput, typename TCalc = TInput>
100 : public private_covariance::covariance_traits<TInput, TCalc>
101 , public etl::binary_function<TInput, TInput, void>
102 {
103 private:
104
105 static ETL_CONSTANT int Adjustment = (Covariance_Type == covariance_type::Population) ? 0 : 1;
106
108
109 public:
110
111 //*********************************
113 //*********************************
115 {
116 clear();
117 }
118
119 //*********************************
121 //*********************************
122 template <typename TIterator>
128
129 //*********************************
131 //*********************************
132 void add(TInput value1, TInput value2)
133 {
134 inner_product += TCalc(value1 * value2);
135 sum1 += TCalc(value1);
136 sum2 += TCalc(value2);
137 ++counter;
138 recalculate = true;
139 }
140
141 //*********************************
143 //*********************************
144 template <typename TIterator>
146 {
147 while (first1 != last1)
148 {
149 add(*first1, *first2);
150 ++first1;
151 ++first2;
152 }
153 }
154
155 //*********************************
158 //*********************************
159 void operator ()(TInput value1, TInput value2)
160 {
161 add(value1, value2);
162 }
163
164 //*********************************
167 //*********************************
168 template <typename TIterator>
173
174 //*********************************
176 //*********************************
177 double get_covariance() const
178 {
179 if (recalculate)
180 {
181 covariance_value = 0.0;
182
183 if (counter != 0)
184 {
185 double n = double(counter);
186 double adjustment = 1.0 / (n * (n - Adjustment));
187
188 covariance_value = ((n * inner_product) - (sum1 * sum2)) * adjustment;
189
190 recalculate = false;
191 }
192 }
193
194 return covariance_value;
195 }
196
197 //*********************************
199 //*********************************
200 operator double() const
201 {
202 return get_covariance();
203 }
204
205 //*********************************
207 //*********************************
208 size_t count() const
209 {
210 return size_t(counter);
211 }
212
213 //*********************************
215 //*********************************
216 void clear()
217 {
218 inner_product = calc_t(0);
219 sum1 = calc_t(0);
220 sum2 = calc_t(0);
221 counter = 0U;
222 covariance_value = 0.0;
223 recalculate = true;
224 }
225
226 private:
227
228 calc_t inner_product;
229 calc_t sum1;
230 calc_t sum2;
231 uint32_t counter;
232 mutable double covariance_value;
233 mutable bool recalculate;
234 };
235
236 template <bool Covariance_Type, typename TInput, typename TCalc>
237 ETL_CONSTANT int covariance<Covariance_Type, TInput, TCalc>::Adjustment;
238}
239
240#endif
Covariance.
Definition covariance.h:102
void clear()
Clear the covariance.
Definition covariance.h:216
double get_covariance() const
Get the covariance.
Definition covariance.h:177
size_t count() const
Get the total number added entries.
Definition covariance.h:208
covariance()
Constructor.
Definition covariance.h:114
covariance(TIterator first1, TIterator last1, TIterator first2)
Constructor.
Definition covariance.h:123
void add(TIterator first1, TIterator last1, TIterator first2)
Add a range.
Definition covariance.h:145
void operator()(TInput value1, TInput value2)
Definition covariance.h:159
void add(TInput value1, TInput value2)
Add a pair of values.
Definition covariance.h:132
bitset_ext
Definition absolute.h:38
Definition functional.h:126
Definition covariance.h:92
pair holds two objects of arbitrary type
Definition utility.h:164
Types for generic covariance.
Definition covariance.h:49