///////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2018- Equinor 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 // for more details. // ///////////////////////////////////////////////////////////////////////////////// #include "cvfAssert.h" //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template RiaWeightedMeanCalculator::RiaWeightedMeanCalculator() : m_aggregatedValue( T{} ) , m_aggregatedWeight( 0.0 ) { } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template void RiaWeightedMeanCalculator::addValueAndWeight( T value, double weight ) { CVF_ASSERT( weight >= 0.0 ); m_aggregatedValue = m_aggregatedValue + value * weight; m_aggregatedWeight += weight; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template T RiaWeightedMeanCalculator::weightedMean() const { bool validWeights = validAggregatedWeight(); CVF_TIGHT_ASSERT( validWeights ); if ( validWeights ) { return m_aggregatedValue * ( 1.0 / m_aggregatedWeight ); } return T{}; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template double RiaWeightedMeanCalculator::aggregatedWeight() const { return m_aggregatedWeight; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template bool RiaWeightedMeanCalculator::validAggregatedWeight() const { return m_aggregatedWeight > 1.0e-12; }