mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#3210 Helper class used to compute weighted average
This commit is contained in:
parent
f6e6cacc36
commit
e195eed246
@ -33,6 +33,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaSCurveCalculator.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaArcCurveCalculator.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaJCurveCalculator.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaOffshoreSphericalCoords.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedAverageCalculator.h
|
||||
)
|
||||
|
||||
set (SOURCE_GROUP_SOURCE_FILES
|
||||
@ -68,6 +69,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaPolyArcLineSampler.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaSCurveCalculator.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaArcCurveCalculator.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaJCurveCalculator.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedAverageCalculator.cpp
|
||||
)
|
||||
|
||||
list(APPEND CODE_HEADER_FILES
|
||||
|
@ -0,0 +1,58 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "RiaWeightedAverageCalculator.h"
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RiaWeightedAverageCalculator::RiaWeightedAverageCalculator()
|
||||
: m_aggregatedValue(0.0)
|
||||
, m_aggregatedWeight(0.0)
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiaWeightedAverageCalculator::addValueAndWeight(double value, double weight)
|
||||
{
|
||||
m_aggregatedValue += value * weight;
|
||||
m_aggregatedWeight += weight;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
double RiaWeightedAverageCalculator::weightedAverage() const
|
||||
{
|
||||
if (m_aggregatedWeight > 1e-7)
|
||||
{
|
||||
return m_aggregatedValue / m_aggregatedWeight;
|
||||
}
|
||||
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
double RiaWeightedAverageCalculator::aggregatedWeight() const
|
||||
{
|
||||
return m_aggregatedWeight;
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma once
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
class RiaWeightedAverageCalculator
|
||||
{
|
||||
public:
|
||||
RiaWeightedAverageCalculator();
|
||||
|
||||
void addValueAndWeight(double value, double weight);
|
||||
|
||||
double weightedAverage() const;
|
||||
double aggregatedWeight() const;
|
||||
|
||||
private:
|
||||
double m_aggregatedValue;
|
||||
double m_aggregatedWeight;
|
||||
};
|
@ -47,6 +47,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaTimeHistoryCurveTools-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/SolveSpaceSolver-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaPolyArcLineSampler-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifEclipseDataTableFormatter-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedAverage-Test.cpp
|
||||
)
|
||||
|
||||
list(APPEND CODE_HEADER_FILES
|
||||
|
33
ApplicationCode/UnitTests/RiaWeightedAverage-Test.cpp
Normal file
33
ApplicationCode/UnitTests/RiaWeightedAverage-Test.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "RiaWeightedAverageCalculator.h"
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(RiaWeightedAverageCalculator, BasicUsage)
|
||||
{
|
||||
{
|
||||
RiaWeightedAverageCalculator calc;
|
||||
|
||||
EXPECT_DOUBLE_EQ(0.0, calc.aggregatedWeight());
|
||||
EXPECT_DOUBLE_EQ(0.0, calc.weightedAverage());
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
RiaWeightedAverageCalculator calc;
|
||||
|
||||
std::vector<double> values {3.0, 6.0};
|
||||
std::vector<double> weights {1.0, 2.0};
|
||||
|
||||
for (size_t i = 0; i< values.size(); i++)
|
||||
{
|
||||
calc.addValueAndWeight(values[i], weights[i]);
|
||||
}
|
||||
|
||||
EXPECT_DOUBLE_EQ(3.0, calc.aggregatedWeight());
|
||||
EXPECT_DOUBLE_EQ(5.0, calc.weightedAverage());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user