mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-30 06:18:50 +08:00
commit c217b3ec12b26104e8f1027c1766cc9d49b93a29
Author: John Wellbelove <john.wellbelove@asterconsulting.co.uk>
Date: Thu Apr 21 11:57:55 2022 +0200
Added mutex traits
Added transparent comparator test to multimap
commit b6487b869e5599ea067d45af1778d5c0f90c6a52
Author: John Wellbelove <john.wellbelove@asterconsulting.co.uk>
Date: Thu Apr 21 10:42:06 2022 +0200
Modified char8_t, char16_t and char32_t macros.
Added conditional compilation on the presense of native char8_t, char16_t and char32_t types.
commit 2c2bd86ce3d5d5d698e922518421a93f70a9cc1e
Author: John Wellbelove <john.wellbelove@asterconsulting.co.uk>
Date: Thu Apr 21 10:29:04 2022 +0200
Modified char8_t, char16_t and char32_t macros.
Added conditional compilation on the presense of native char8_t, char16_t and char32_t types.
commit d16242d20e9b6df6752a926d862d0016a452bada
Author: John Wellbelove <john.wellbelove@asterconsulting.co.uk>
Date: Wed Apr 20 13:53:37 2022 +0200
Expanded constexpr test
commit fad097e6656cd4639c2d81bc8cad2467cae4b4ce
Author: John Wellbelove <john.wellbelove@asterconsulting.co.uk>
Date: Wed Apr 20 13:53:05 2022 +0200
Added ETL_OVERRIDE to state chart process_event()
commit 25403c2225f3aff0b99105a54cfc44f1cf88d527
Author: John Wellbelove <john.wellbelove@asterconsulting.co.uk>
Date: Wed Apr 20 13:41:00 2022 +0200
Added test_etl_traits
commit e42c778cf645cb8fc06e5930336e7f1183004dfe
Author: John Wellbelove <john.wellbelove@asterconsulting.co.uk>
Date: Wed Apr 20 13:38:22 2022 +0200
Removed unused code.
commit 5a25c0c1973f7094ef3aa5b6f29529e96450451c
Author: John Wellbelove <john.wellbelove@asterconsulting.co.uk>
Date: Sun Apr 17 13:17:29 2022 +0200
Large and small character support macros
commit 6a463fb65ef655b700a8dea381265a3c1b622658
Author: Melg Eight <public.melg8@gmail.com>
Date: Sun Apr 17 13:54:34 2022 +0300
Fix spelling (#535)
Signed-off-by: Melg Eight <public.melg8@gmail.com>
commit 5468eb659c8b4ecdb3d08e8b8f0442c5a2549a48
Author: Gabriel Arjones <arjones@arjones.com>
Date: Thu Apr 14 17:55:27 2022 -0300
cmake: write an arch independent version file (#534)
225 lines
6.3 KiB
C++
225 lines
6.3 KiB
C++
///\file
|
|
|
|
/******************************************************************************
|
|
The MIT License(MIT)
|
|
|
|
Embedded Template Library.
|
|
https://github.com/ETLCPP/etl
|
|
https://www.etlcpp.com
|
|
|
|
Copyright(c) 2021 jwellbelove
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files(the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions :
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
******************************************************************************/
|
|
|
|
#ifndef ETL_COVARIANCE_INCLUDED
|
|
#define ETL_COVARIANCE_INCLUDED
|
|
|
|
#include "platform.h"
|
|
#include "functional.h"
|
|
#include "type_traits.h"
|
|
|
|
#include <math.h>
|
|
#include <stdint.h>
|
|
|
|
namespace etl
|
|
{
|
|
namespace private_covariance
|
|
{
|
|
//***************************************************************************
|
|
/// Types for generic covariance.
|
|
//***************************************************************************
|
|
template <typename TInput, typename TCalc>
|
|
struct covariance_traits
|
|
{
|
|
typedef TCalc calc_t;
|
|
};
|
|
|
|
//***************************************************************************
|
|
/// Types for float covariance.
|
|
//***************************************************************************
|
|
template <typename TCalc>
|
|
struct covariance_traits<float, TCalc>
|
|
{
|
|
typedef float calc_t;
|
|
};
|
|
|
|
//***************************************************************************
|
|
/// Types for double covariance.
|
|
//***************************************************************************
|
|
template <typename TCalc>
|
|
struct covariance_traits<double, TCalc>
|
|
{
|
|
typedef double calc_t;
|
|
};
|
|
}
|
|
|
|
//***************************************************************************
|
|
/// Covariance Type.
|
|
//***************************************************************************
|
|
struct covariance_type
|
|
{
|
|
static ETL_CONSTANT bool Sample = false;
|
|
static ETL_CONSTANT bool Population = true;
|
|
};
|
|
|
|
//***************************************************************************
|
|
/// Covariance.
|
|
//***************************************************************************
|
|
template <bool Covariance_Type, typename TInput, typename TCalc = TInput>
|
|
class covariance
|
|
: public private_covariance::covariance_traits<TInput, TCalc>
|
|
, public etl::binary_function<TInput, TInput, void>
|
|
{
|
|
private:
|
|
|
|
static ETL_CONSTANT int Adjustment = (Covariance_Type == covariance_type::Population) ? 0 : 1;
|
|
|
|
typedef typename private_covariance::covariance_traits<TInput, TCalc>::calc_t calc_t;
|
|
|
|
public:
|
|
|
|
//*********************************
|
|
/// Constructor.
|
|
//*********************************
|
|
covariance()
|
|
{
|
|
clear();
|
|
}
|
|
|
|
//*********************************
|
|
/// Constructor.
|
|
//*********************************
|
|
template <typename TIterator>
|
|
covariance(TIterator first1, TIterator last1, TIterator first2)
|
|
{
|
|
clear();
|
|
add(first1, last1, first2);
|
|
}
|
|
|
|
//*********************************
|
|
/// Add a pair of values.
|
|
//*********************************
|
|
void add(TInput value1, TInput value2)
|
|
{
|
|
inner_product += TCalc(value1 * value2);
|
|
sum1 += TCalc(value1);
|
|
sum2 += TCalc(value2);
|
|
++counter;
|
|
recalculate = true;
|
|
}
|
|
|
|
//*********************************
|
|
/// Add a range.
|
|
//*********************************
|
|
template <typename TIterator>
|
|
void add(TIterator first1, TIterator last1, TIterator first2)
|
|
{
|
|
while (first1 != last1)
|
|
{
|
|
add(*first1, *first2);
|
|
++first1;
|
|
++first2;
|
|
}
|
|
}
|
|
|
|
//*********************************
|
|
/// operator ()
|
|
/// Add a pair of values.
|
|
//*********************************
|
|
void operator ()(TInput value1, TInput value2)
|
|
{
|
|
add(value1, value2);
|
|
}
|
|
|
|
//*********************************
|
|
/// operator ()
|
|
/// Add a range.
|
|
//*********************************
|
|
template <typename TIterator>
|
|
void operator ()(TIterator first1, TIterator last1, TIterator first2)
|
|
{
|
|
add(first1, last1, first2);
|
|
}
|
|
|
|
//*********************************
|
|
/// Get the covariance.
|
|
//*********************************
|
|
double get_covariance() const
|
|
{
|
|
if (recalculate)
|
|
{
|
|
covariance_value = 0.0;
|
|
|
|
if (counter != 0)
|
|
{
|
|
double n = double(counter);
|
|
double adjustment = 1.0 / (n * (n - Adjustment));
|
|
|
|
covariance_value = ((n * inner_product) - (sum1 * sum2)) * adjustment;
|
|
|
|
recalculate = false;
|
|
}
|
|
}
|
|
|
|
return covariance_value;
|
|
}
|
|
|
|
//*********************************
|
|
/// Get the covariance.
|
|
//*********************************
|
|
operator double() const
|
|
{
|
|
return get_covariance();
|
|
}
|
|
|
|
//*********************************
|
|
/// Get the total number added entries.
|
|
//*********************************
|
|
size_t count() const
|
|
{
|
|
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:
|
|
|
|
calc_t inner_product;
|
|
calc_t sum1;
|
|
calc_t sum2;
|
|
uint32_t counter;
|
|
mutable double covariance_value;
|
|
mutable bool recalculate;
|
|
};
|
|
}
|
|
|
|
#endif
|