Add utility for finding min/max of vector.

This commit is contained in:
Kristian Bendiksen
2024-11-08 11:45:43 +01:00
parent 8e6d8ee317
commit c2ec023720
4 changed files with 66 additions and 5 deletions

View File

@@ -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 );

View File

@@ -23,6 +23,7 @@
#include <algorithm>
#include <cassert>
#include <cmath>
#include <numeric>
//--------------------------------------------------------------------------------------------------
/// A function to do basic statistical calculations

View File

@@ -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 );
}

View File

@@ -20,9 +20,12 @@
#include "gtest/gtest.h"
#include "QElapsedTimer"
#include "RigStatisticsMath.h"
#include "QElapsedTimer"
#include <numeric>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------