mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Add utility for finding min/max of vector.
This commit is contained in:
@@ -21,7 +21,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <numeric>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
//==================================================================================================
|
||||
@@ -41,10 +41,37 @@ public:
|
||||
template <class NumberType>
|
||||
static bool isValidNumber( NumberType value )
|
||||
{
|
||||
if ( std::isinf( value ) ) return false;
|
||||
if ( std::isnan( value ) ) return false;
|
||||
return !std::isinf( value ) && !std::isnan( value );
|
||||
}
|
||||
|
||||
return true;
|
||||
template <class NumberType>
|
||||
static NumberType minimumValue( const std::vector<NumberType>& values )
|
||||
{
|
||||
NumberType minValue = std::numeric_limits<NumberType>::max();
|
||||
for ( NumberType value : values )
|
||||
{
|
||||
if ( RiaStatisticsTools::isValidNumber<NumberType>( value ) )
|
||||
{
|
||||
minValue = std::min( minValue, value );
|
||||
}
|
||||
}
|
||||
|
||||
return minValue;
|
||||
}
|
||||
|
||||
template <class NumberType>
|
||||
static NumberType maximumValue( const std::vector<NumberType>& values )
|
||||
{
|
||||
NumberType maxValue = -std::numeric_limits<NumberType>::max();
|
||||
for ( NumberType value : values )
|
||||
{
|
||||
if ( RiaStatisticsTools::isValidNumber<NumberType>( value ) )
|
||||
{
|
||||
maxValue = std::max( maxValue, value );
|
||||
}
|
||||
}
|
||||
|
||||
return maxValue;
|
||||
}
|
||||
|
||||
static double pearsonCorrelation( const std::vector<double>& xValues, const std::vector<double>& yValues );
|
||||
|
@@ -23,6 +23,7 @@
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <numeric>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// A function to do basic statistical calculations
|
||||
|
@@ -83,3 +83,33 @@ TEST( RiaStatisticsTools, NegativeCorrelation )
|
||||
double correlation = RiaStatisticsTools::pearsonCorrelation( a, b );
|
||||
EXPECT_NEAR( correlation, -1.0, 1.0e-2 );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RiaStatisticsTools, MinValue )
|
||||
{
|
||||
const int N = 1000;
|
||||
std::vector<double> a;
|
||||
for ( int i = 10; i < N; ++i )
|
||||
{
|
||||
a.push_back( static_cast<double>( i ) );
|
||||
}
|
||||
double minValue = RiaStatisticsTools::minimumValue( a );
|
||||
EXPECT_EQ( minValue, 10.0 );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RiaStatisticsTools, MaxValue )
|
||||
{
|
||||
const int N = 1000;
|
||||
std::vector<double> a;
|
||||
for ( int i = 10; i < N; ++i )
|
||||
{
|
||||
a.push_back( static_cast<double>( i ) );
|
||||
}
|
||||
double maxValue = RiaStatisticsTools::maximumValue( a );
|
||||
EXPECT_EQ( maxValue, 999.0 );
|
||||
}
|
||||
|
@@ -20,9 +20,12 @@
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "QElapsedTimer"
|
||||
#include "RigStatisticsMath.h"
|
||||
|
||||
#include "QElapsedTimer"
|
||||
|
||||
#include <numeric>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
Reference in New Issue
Block a user