2018-08-15 08:38:54 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2018- Statoil ASA
|
|
|
|
|
//
|
|
|
|
|
// ResInsight is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
//
|
|
|
|
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
|
|
|
|
// for more details.
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2018-08-15 15:52:00 +02:00
|
|
|
#include "cvfAssert.h"
|
|
|
|
|
|
2018-08-15 08:38:54 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2018-09-06 19:58:28 +02:00
|
|
|
template<class T>
|
2018-09-14 09:12:05 +02:00
|
|
|
RiaWeightedMeanCalculator<T>::RiaWeightedMeanCalculator()
|
2018-09-06 19:58:28 +02:00
|
|
|
: m_aggregatedValue(T{})
|
2018-08-15 08:38:54 +02:00
|
|
|
, m_aggregatedWeight(0.0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2018-09-06 19:58:28 +02:00
|
|
|
template<class T>
|
2018-09-14 09:12:05 +02:00
|
|
|
void RiaWeightedMeanCalculator<T>::addValueAndWeight(T value, double weight)
|
2018-08-15 08:38:54 +02:00
|
|
|
{
|
2018-08-15 15:52:00 +02:00
|
|
|
CVF_ASSERT(weight >= 0.0);
|
|
|
|
|
|
2018-09-06 19:58:28 +02:00
|
|
|
m_aggregatedValue = m_aggregatedValue + value * weight;
|
2018-08-15 08:38:54 +02:00
|
|
|
m_aggregatedWeight += weight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2018-09-06 19:58:28 +02:00
|
|
|
template<class T>
|
2018-09-14 09:12:05 +02:00
|
|
|
T RiaWeightedMeanCalculator<T>::weightedMean() const
|
2018-08-15 08:38:54 +02:00
|
|
|
{
|
2018-09-06 19:58:28 +02:00
|
|
|
bool validWeights = validAggregatedWeight();
|
|
|
|
|
CVF_TIGHT_ASSERT(validWeights);
|
|
|
|
|
if (validWeights)
|
2018-08-15 08:38:54 +02:00
|
|
|
{
|
2018-09-06 19:58:28 +02:00
|
|
|
return m_aggregatedValue * (1.0 / m_aggregatedWeight);
|
2018-08-15 08:38:54 +02:00
|
|
|
}
|
2018-09-06 19:58:28 +02:00
|
|
|
return T{};
|
2018-08-15 08:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2018-09-06 19:58:28 +02:00
|
|
|
template<class T>
|
2018-09-14 09:12:05 +02:00
|
|
|
double RiaWeightedMeanCalculator<T>::aggregatedWeight() const
|
2018-08-15 08:38:54 +02:00
|
|
|
{
|
|
|
|
|
return m_aggregatedWeight;
|
|
|
|
|
}
|
2018-09-06 19:58:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
template<class T>
|
2018-09-14 09:12:05 +02:00
|
|
|
bool RiaWeightedMeanCalculator<T>::validAggregatedWeight() const
|
2018-09-06 19:58:28 +02:00
|
|
|
{
|
|
|
|
|
return m_aggregatedWeight > 1.0e-12;
|
|
|
|
|
}
|