From c426fc40b922e923cbebf02718c37187d5a3d8f9 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 10 Apr 2021 18:51:10 +0100 Subject: [PATCH] Changed traits implementation for maths algorithms --- include/etl/correlation.h | 100 ++++++++++--------------------- include/etl/covariance.h | 82 ++++++++----------------- include/etl/mean.h | 65 ++++++++------------ include/etl/rms.h | 79 ++++++++++-------------- include/etl/standard_deviation.h | 98 ++++++++++++------------------ include/etl/variance.h | 93 +++++++++++----------------- 6 files changed, 187 insertions(+), 330 deletions(-) diff --git a/include/etl/correlation.h b/include/etl/correlation.h index 3ba499e4..c22a5b7c 100644 --- a/include/etl/correlation.h +++ b/include/etl/correlation.h @@ -48,25 +48,7 @@ namespace etl template struct correlation_traits { - TCalc inner_product; - TCalc sum_of_squares1; - TCalc sum_of_squares2; - TCalc sum1; - TCalc sum2; - uint32_t counter; - - //********************************* - /// Clear the correlation. - //********************************* - void clear() - { - inner_product = TCalc(0); - sum_of_squares1 = TCalc(0); - sum_of_squares2 = TCalc(0); - sum1 = TCalc(0); - sum2 = TCalc(0); - counter = 0U; - } + typedef TCalc calc_t; }; //*************************************************************************** @@ -75,25 +57,7 @@ namespace etl template struct correlation_traits { - float inner_product; - float sum_of_squares1; - float sum_of_squares2; - float sum1; - float sum2; - uint32_t counter; - - //********************************* - /// Clear the correlation. - //********************************* - void clear() - { - inner_product = float(0); - sum_of_squares1 = float(0); - sum_of_squares2 = float(0); - sum1 = float(0); - sum2 = float(0); - counter = 0U; - } + typedef float calc_t; }; //*************************************************************************** @@ -102,25 +66,7 @@ namespace etl template struct correlation_traits { - double inner_product; - double sum_of_squares1; - double sum_of_squares2; - double sum1; - double sum2; - uint32_t counter; - - //********************************* - /// Clear the correlation. - //********************************* - void clear() - { - inner_product = double(0); - sum_of_squares1 = double(0); - sum_of_squares2 = double(0); - sum1 = double(0); - sum2 = double(0); - counter = 0U; - } + typedef double calc_t; }; } @@ -145,17 +91,16 @@ namespace etl static ETL_CONSTANT int Adjustment = (Correlation_Type == correlation_type::Population) ? 0 : 1; + typedef typename private_correlation::correlation_traits::calc_t calc_t; + public: //********************************* /// Constructor. //********************************* correlation() - : covariance_value(0.0) - , correlation_value(0.0) - , recalculate(true) { - this->clear(); + clear(); } //********************************* @@ -163,11 +108,8 @@ namespace etl //********************************* template correlation(TIterator first1, TIterator last1, TIterator first2) - : covariance_value(0.0) - , correlation_value(0.0) - , recalculate(true) { - this->clear(); + clear(); add(first1, last1, first2); } @@ -252,6 +194,22 @@ namespace etl return size_t(counter); } + //********************************* + /// Clear the correlation. + //********************************* + void clear() + { + inner_product = calc_t(0); + sum_of_squares1 = calc_t(0); + sum_of_squares2 = calc_t(0); + sum1 = calc_t(0); + sum2 = calc_t(0); + counter = 0U; + covariance_value = 0.0; + correlation_value = 0.0; + recalculate = true; + } + private: //********************************* @@ -300,9 +258,15 @@ namespace etl } } - double covariance_value; - double correlation_value; - bool recalculate; + calc_t inner_product; + calc_t sum_of_squares1; + calc_t sum_of_squares2; + calc_t sum1; + calc_t sum2; + uint32_t counter; + double covariance_value; + double correlation_value; + bool recalculate; }; } diff --git a/include/etl/covariance.h b/include/etl/covariance.h index bec97c6b..33619cc9 100644 --- a/include/etl/covariance.h +++ b/include/etl/covariance.h @@ -48,21 +48,7 @@ namespace etl template struct covariance_traits { - TCalc inner_product; - TCalc sum1; - TCalc sum2; - uint32_t counter; - - //********************************* - /// Clear the covariance. - //********************************* - void clear() - { - inner_product = TCalc(0); - sum1 = TCalc(0); - sum2 = TCalc(0); - counter = 0U; - } + typedef TCalc calc_t; }; //*************************************************************************** @@ -71,21 +57,7 @@ namespace etl template struct covariance_traits { - float inner_product; - float sum1; - float sum2; - uint32_t counter; - - //********************************* - /// Clear the covariance. - //********************************* - void clear() - { - inner_product = float(0); - sum1 = float(0); - sum2 = float(0); - counter = 0U; - } + typedef float calc_t; }; //*************************************************************************** @@ -94,21 +66,7 @@ namespace etl template struct covariance_traits { - double inner_product; - double sum1; - double sum2; - uint32_t counter; - - //********************************* - /// Clear the covariance. - //********************************* - void clear() - { - inner_product = double(0); - sum1 = double(0); - sum2 = double(0); - counter = 0U; - } + typedef double calc_t; }; } @@ -133,16 +91,16 @@ namespace etl static ETL_CONSTANT int Adjustment = (Covariance_Type == covariance_type::Population) ? 0 : 1; + typedef typename private_covariance::covariance_traits::calc_t calc_t; + public: //********************************* /// Constructor. //********************************* covariance() - : covariance_value(0.0) - , recalculate(true) { - this->clear(); + clear(); } //********************************* @@ -150,10 +108,8 @@ namespace etl //********************************* template covariance(TIterator first1, TIterator last1, TIterator first2) - : covariance_value(0.0) - , recalculate(true) { - this->clear(); + clear(); add(first1, last1, first2); } @@ -214,9 +170,6 @@ namespace etl double n = double(counter); double adjustment = 1.0 / (n * (n - Adjustment)); - double mean1 = sum1 / n; - double mean2 = sum2 / n; - covariance_value = ((n * inner_product) - (sum1 * sum2)) * adjustment; recalculate = false; @@ -242,10 +195,27 @@ namespace etl return size_t(counter); } + //********************************* + /// Clear the covariance. + //********************************* + void clear() + { + inner_product = calc_t(0); + sum1 = calc_t(0); + sum2 = calc_t(0); + counter = 0U; + covariance_value = 0.0; + recalculate = true; + } + private: - double covariance_value; - bool recalculate; + calc_t inner_product; + calc_t sum1; + calc_t sum2; + uint32_t counter; + double covariance_value; + bool recalculate; }; } diff --git a/include/etl/mean.h b/include/etl/mean.h index 30396fa2..e2a42fa8 100644 --- a/include/etl/mean.h +++ b/include/etl/mean.h @@ -48,17 +48,7 @@ namespace etl template struct mean_traits { - TCalc sum; - uint32_t counter; - - //********************************* - /// Clear the mean. - //********************************* - void clear() - { - sum = TCalc(0); - counter = 0U; - } + typedef TCalc calc_t; }; //*************************************************************************** @@ -67,17 +57,7 @@ namespace etl template struct mean_traits { - float sum; - uint32_t counter; - - //********************************* - /// Clear the mean. - //********************************* - void clear() - { - sum = float(0); - counter = 0U; - } + typedef float calc_t; }; //*************************************************************************** @@ -86,17 +66,7 @@ namespace etl template struct mean_traits { - double sum; - uint32_t counter; - - //********************************* - /// Clear the mean. - //********************************* - void clear() - { - sum = double(0); - counter = 0U; - } + typedef double calc_t; }; } @@ -108,16 +78,18 @@ namespace etl : public private_mean::mean_traits , public etl::binary_function { + private: + + typedef typename private_mean::mean_traits::calc_t calc_t; + public: //********************************* /// Constructor. //********************************* mean() - : mean_value(0.0) - , recalculate(true) { - this->clear(); + clear(); } //********************************* @@ -125,10 +97,8 @@ namespace etl //********************************* template mean(TIterator first, TIterator last) - : mean_value(0.0) - , recalculate(true) { - this->clear(); + clear(); add(first, last); } @@ -210,10 +180,23 @@ namespace etl return size_t(counter); } + //********************************* + /// Clear the correlation. + //********************************* + void clear() + { + sum = calc_t(0); + counter = 0U; + mean_value = 0.0; + recalculate = true; + } + private: - double mean_value; - bool recalculate; + calc_t sum; + uint32_t counter; + double mean_value; + bool recalculate; }; } diff --git a/include/etl/rms.h b/include/etl/rms.h index 3911ac68..d6f478d5 100644 --- a/include/etl/rms.h +++ b/include/etl/rms.h @@ -43,60 +43,30 @@ namespace etl namespace private_rms { //*************************************************************************** - /// Types for generic covariance. + /// Types for generic correlation. //*************************************************************************** template struct rms_traits { - TCalc sum_of_squares; - uint32_t counter; - - //********************************* - /// Clear the histogram. - //********************************* - void clear() - { - sum_of_squares = TCalc(0); - counter = 0U; - } + typedef TCalc calc_t; }; //*************************************************************************** - /// Types for float covariance. + /// Types for float correlation. //*************************************************************************** template struct rms_traits { - float sum_of_squares; - uint32_t counter; - - //********************************* - /// Clear the histogram. - //********************************* - void clear() - { - sum_of_squares = float(0); - counter = 0U; - } + typedef float calc_t; }; //*************************************************************************** - /// Types for double covariance. + /// Types for double correlation. //*************************************************************************** template struct rms_traits { - double sum_of_squares; - uint32_t counter; - - //********************************* - /// Clear the histogram. - //********************************* - void clear() - { - sum_of_squares = double(0); - counter = 0U; - } + typedef double calc_t; }; } @@ -108,6 +78,10 @@ namespace etl : public private_rms::rms_traits , public etl::binary_function { + private: + + typedef typename private_rms::rms_traits::calc_t calc_t; + public: //********************************* @@ -116,7 +90,7 @@ namespace etl rms() : recalculate(true) { - this->clear(); + clear(); } //********************************* @@ -126,7 +100,7 @@ namespace etl rms(TIterator first, TIterator last) : recalculate(true) { - this->clear(); + clear(); add(first, last); } @@ -135,8 +109,8 @@ namespace etl //********************************* void add(TInput value) { - this->sum_of_squares += TCalc(value * value); - ++this->counter; + sum_of_squares += TCalc(value * value); + ++counter; recalculate = true; } @@ -180,10 +154,10 @@ namespace etl { rms_value = 0.0; - if (this->counter != 0) + if (counter != 0) { - double n = double(this->counter); - double mean_of_squares = this->sum_of_squares / n; + double n = double(counter); + double mean_of_squares = sum_of_squares / n; if (mean_of_squares > 0) { @@ -210,13 +184,26 @@ namespace etl //********************************* size_t count() const { - return size_t(this->counter); + return size_t(counter); + } + + //********************************* + /// Clear the histogram. + //********************************* + void clear() + { + sum_of_squares = calc_t(0); + counter = 0U; + rms_value = 0.0; + recalculate = true; } private: - double rms_value; - bool recalculate; + calc_t sum_of_squares; + uint32_t counter; + double rms_value; + bool recalculate; }; } diff --git a/include/etl/standard_deviation.h b/include/etl/standard_deviation.h index a80c3a40..bcae237b 100644 --- a/include/etl/standard_deviation.h +++ b/include/etl/standard_deviation.h @@ -43,66 +43,30 @@ namespace etl namespace private_standard_deviation { //*************************************************************************** - /// Types for generic covariance. + /// Types for generic standard_deviation. //*************************************************************************** template struct standard_deviation_traits { - TCalc sum_of_squares; - TCalc sum; - uint32_t counter; - - //********************************* - /// Clear the histogram. - //********************************* - void clear() - { - sum_of_squares = TCalc(0); - sum = TCalc(0); - counter = 0U; - } + typedef TCalc calc_t; }; //*************************************************************************** - /// Types for float covariance. + /// Types for float standard_deviation. //*************************************************************************** template struct standard_deviation_traits { - float sum_of_squares; - float sum; - uint32_t counter; - - //********************************* - /// Clear the histogram. - //********************************* - void clear() - { - sum_of_squares = float(0); - sum = float(0); - counter = 0U; - } + typedef float calc_t; }; //*************************************************************************** - /// Types for double covariance. + /// Types for double standard_deviation. //*************************************************************************** template struct standard_deviation_traits { - double sum_of_squares; - double sum; - uint32_t counter; - - //********************************* - /// Clear the histogram. - //********************************* - void clear() - { - sum_of_squares = double(0); - sum = double(0); - counter = 0U; - } + typedef double calc_t; }; } @@ -127,17 +91,16 @@ namespace etl static ETL_CONSTANT int Adjustment = (Standard_Deviation_Type == standard_deviation_type::Population) ? 0 : 1; + typedef typename private_standard_deviation::standard_deviation_traits::calc_t calc_t; + public: //********************************* /// Constructor. //********************************* standard_deviation() - : variance_value(0.0) - , standard_deviation_value(0.0) - , recalculate(true) { - this->clear(); + clear(); } //********************************* @@ -145,11 +108,8 @@ namespace etl //********************************* template standard_deviation(TIterator first, TIterator last) - : variance_value(0.0) - , standard_deviation_value(0.0) - , recalculate(true) { - this->clear(); + clear(); add(first, last); } @@ -158,9 +118,9 @@ namespace etl //********************************* void add(TInput value) { - this->sum_of_squares += TCalc(value * value); - this->sum += TCalc(value); - ++this->counter; + sum_of_squares += TCalc(value * value); + sum += TCalc(value); + ++counter; recalculate = true; } @@ -228,7 +188,20 @@ namespace etl //********************************* size_t count() const { - return size_t(this->counter); + return size_t(counter); + } + + //********************************* + /// Clear the histogram. + //********************************* + void clear() + { + sum_of_squares = calc_t(0); + sum = calc_t(0); + counter = 0U; + variance_value = 0.0; + standard_deviation_value = 0.0; + recalculate = true; } private: @@ -243,14 +216,14 @@ namespace etl standard_deviation_value = 0.0; variance_value = 0.0; - if (this->counter != 0) + if (counter != 0) { - double n = double(this->counter); + double n = double(counter); double adjustment = 1.0 / (n * (n - Adjustment)); - double square_of_sum = (this->sum * this->sum); + double square_of_sum = (sum * sum); - variance_value = ((n * this->sum_of_squares) - square_of_sum) * adjustment; + variance_value = ((n * sum_of_squares) - square_of_sum) * adjustment; if (variance_value > 0) { @@ -262,9 +235,12 @@ namespace etl } } - double variance_value; - double standard_deviation_value; - bool recalculate; + calc_t sum_of_squares; + calc_t sum; + uint32_t counter; + double variance_value; + double standard_deviation_value; + bool recalculate; }; } diff --git a/include/etl/variance.h b/include/etl/variance.h index a1b3fe0e..b26fcc20 100644 --- a/include/etl/variance.h +++ b/include/etl/variance.h @@ -43,66 +43,30 @@ namespace etl namespace private_variance { //*************************************************************************** - /// Types for generic variance. + /// Types for generic correlation. //*************************************************************************** template struct variance_traits { - TCalc sum_of_squares; - TCalc sum; - uint32_t counter; - - //********************************* - /// Clear the variance. - //********************************* - void clear() - { - sum_of_squares = TCalc(0); - sum = TCalc(0); - counter = 0U; - } + typedef TCalc calc_t; }; //*************************************************************************** - /// Types for float variance. + /// Types for float correlation. //*************************************************************************** template struct variance_traits { - float sum_of_squares; - float sum; - uint32_t counter; - - //********************************* - /// Clear the variance. - //********************************* - void clear() - { - sum_of_squares = float(0); - sum = float(0); - counter = 0U; - } + typedef float calc_t; }; //*************************************************************************** - /// Types for double variance. + /// Types for double correlation. //*************************************************************************** template struct variance_traits { - double sum_of_squares; - double sum; - uint32_t counter; - - //********************************* - /// Clear the variance. - //********************************* - void clear() - { - sum_of_squares = double(0); - sum = double(0); - counter = 0U; - } + typedef double calc_t; }; } @@ -127,16 +91,16 @@ namespace etl static ETL_CONSTANT int Adjustment = (Variance_Type == variance_type::Population) ? 0 : 1; + typedef typename private_variance::variance_traits::calc_t calc_t; + public: //********************************* /// Constructor. //********************************* variance() - : variance_value(0.0) - , recalculate(true) { - this->clear(); + clear(); } //********************************* @@ -144,10 +108,8 @@ namespace etl //********************************* template variance(TIterator first, TIterator last) - : variance_value(0.0) - , recalculate(true) { - this->clear(); + clear(); add(first, last); } @@ -156,9 +118,9 @@ namespace etl //********************************* void add(TInput value) { - this->sum_of_squares += TCalc(value * value); - this->sum += TCalc(value); - ++this->counter; + sum_of_squares += TCalc(value * value); + sum += TCalc(value); + ++counter; recalculate = true; } @@ -202,14 +164,14 @@ namespace etl { variance_value = 0.0; - if (this->counter != 0) + if (counter != 0) { - double n = double(this->counter); + double n = double(counter); double adjustment = 1.0 / (n * (n - Adjustment)) ; - double square_of_sum = this->sum * this->sum; + double square_of_sum = sum * sum; - variance_value = (n * this->sum_of_squares - square_of_sum) * adjustment; + variance_value = (n * sum_of_squares - square_of_sum) * adjustment; } recalculate = false; @@ -231,13 +193,28 @@ namespace etl //********************************* size_t count() const { - return size_t(this->counter); + return size_t(counter); + } + + //********************************* + /// Clear the variance. + //********************************* + void clear() + { + sum_of_squares = calc_t(0); + sum = calc_t(0); + counter = 0U; + variance_value = 0.0; + recalculate = true; } private: - double variance_value; - bool recalculate; + calc_t sum_of_squares; + calc_t sum; + uint32_t counter; + double variance_value; + bool recalculate; }; }